diff options
author | iximeow <me@iximeow.net> | 2017-12-12 00:59:28 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-12 00:59:28 -0800 |
commit | 0363571507f9798dd5af47ed14c71c6208c5fd3b (patch) | |
tree | 03d64742a3dab7d18792814f62650d9758b1610e /src | |
parent | 658f7252f95d24f172e537313923715876d0c88f (diff) | |
parent | ec71ba2bf8a502ccfc215c1f2ab3785a40c1474f (diff) |
Merge pull request #1 from iximeow/fix-timezone-mismatch
Fix timezone mismatch
Diffstat (limited to 'src')
-rw-r--r-- | src/tw/mod.rs | 66 |
1 files changed, 55 insertions, 11 deletions
diff --git a/src/tw/mod.rs b/src/tw/mod.rs index 0a073c9..a8db62f 100644 --- a/src/tw/mod.rs +++ b/src/tw/mod.rs @@ -230,8 +230,48 @@ mod tests { } #[test] - fn test_tweet_retrieval() { + fn test_display_id() { + /* + * ... I think this test only works if Local is -0800 or further from UTC. + * I'm not even sure how to make any of this work in a TZ-invariant way. + */ + // THIS NOW ONLY WORKS FOR DAYS OTHER THAN THE LAST OF A MONTH. FML let today = Local::now(); + let pst_1630_but_utc: DateTime<Utc> = DateTime::parse_from_rfc3339( + &format!("{:04}-{:02}-{:02}T00:30:00Z", today.year(), today.month(), today.day() + 1)).unwrap().with_timezone(&Utc); + let local_pst_1630: DateTime<Local> = DateTime::parse_from_rfc3339( + &format!("{:04}-{:02}-{:02}T16:30:00-08:00", today.year(), today.month(), today.day())).unwrap().with_timezone(&Local); + let tweet = Tweet { + id: "manual_tweet_1".to_owned(), + author_id: "you, dummy".to_owned(), + text: "test tweet please ignore".to_owned(), + created_at: "1234 not real".to_owned(), + recieved_at: pst_1630_but_utc, + urls: HashMap::new(), + quoted_tweet_id: None, + rt_tweet: None, + reply_to_tweet: None, + internal_id: 0 + }; + + let mut tweeter = TwitterCache::new(); + + tweeter.number_and_insert_tweet(tweet.clone()); + + let retrieved = tweeter.retrieve_tweet(&TweetId::Bare(1)); + assert_eq!(retrieved.is_some(), true); + + let retrieved = tweeter.retrieve_tweet(&TweetId::Dated(format!("{:04}{:02}{:02}", today.year(), today.month(), today.day()), 0)); + assert_eq!(retrieved.is_some(), true); + + let display_id = tweeter.display_id_for_tweet(&retrieved.unwrap()); + + assert_eq!(display_id, TweetId::Today(0)); + } + + #[test] + fn test_tweet_retrieval() { + let today = Utc::now(); let yesterday = today - chrono::Duration::days(1); let tweets = vec![ Tweet { @@ -274,8 +314,12 @@ mod tests { tweeter.retrieve_tweet(&TweetId::Today(0)).map(|x| x.id.to_owned()), Some(tweets[1].clone()).map(|x| x.id) ); + + let local_yesterday = yesterday.with_timezone(&Local); + let date = format!("{:04}{:02}{:02}", local_yesterday.year(), local_yesterday.month(), local_yesterday.day()); + assert_eq!( - tweeter.retrieve_tweet(&TweetId::Dated(format!("{:04}{:02}{:02}", yesterday.year(), yesterday.month(), yesterday.day()), 0)).map(|x| x.id.to_owned()), + tweeter.retrieve_tweet(&TweetId::Dated(date, 0)).map(|x| x.id.to_owned()), Some(tweets[0].clone()).map(|x| x.id) ); } @@ -342,26 +386,26 @@ impl IdConversions { id @ &TweetId::Bare(_) => { tweeter.retrieve_tweet(id).and_then(|tweet| { let now = Local::now(); - let tweet_date = tweet.recieved_at.with_timezone(&now.timezone()); + let tweet_date = tweet.recieved_at.with_timezone(&Local); if now.year() == tweet_date.year() && now.month() == tweet_date.month() && now.day() == tweet_date.day() { - let date_string = format!("{:04}{:02}{:02}", tweet.recieved_at.year(), tweet.recieved_at.month(), tweet.recieved_at.day()); + let date_string = format!("{:04}{:02}{:02}", tweet_date.year(), tweet_date.month(), tweet_date.day()); let today_id = self.tweets_by_date_and_tweet_id.get(&date_string).and_then(|m| m.get(&tweet.internal_id)); today_id.map(|x| TweetId::Today(*x)) } else { - None + Some(TweetId::Bare(tweet.internal_id)) } }).unwrap_or(id.to_owned()) }, id @ &TweetId::Twitter(_) => { tweeter.retrieve_tweet(id).and_then(|tweet| { let now = Local::now(); - let tweet_date = tweet.recieved_at.with_timezone(&now.timezone()); + let tweet_date = tweet.recieved_at.with_timezone(&Local); if now.year() == tweet_date.year() && now.month() == tweet_date.month() && now.day() == tweet_date.day() { - let date_string = format!("{:04}{:02}{:02}", tweet.recieved_at.year(), tweet.recieved_at.month(), tweet.recieved_at.day()); + let date_string = format!("{:04}{:02}{:02}", tweet_date.year(), tweet_date.month(), tweet_date.day()); let today_id = self.tweets_by_date_and_tweet_id.get(&date_string).and_then(|m| m.get(&tweet.internal_id)); today_id.map(|x| TweetId::Today(*x)) } else { - None + Some(TweetId::Bare(tweet.internal_id)) } }).unwrap_or(id.to_owned()) } @@ -703,7 +747,7 @@ impl TwitterCache { if tw.internal_id == 0 { tw.internal_id = (self.tweets.len() as u64) + 1; self.id_conversions.id_to_tweet_id.insert(tw.internal_id, tw.id.to_owned()); - let local_recv_time = tw.recieved_at.with_timezone(&Local::now().timezone()); + let local_recv_time = tw.recieved_at.with_timezone(&Local); let tweet_date = format!("{:04}{:02}{:02}", local_recv_time.year(), local_recv_time.month(), local_recv_time.day()); if !self.id_conversions.tweets_by_date.contains_key(&tweet_date) { self.id_conversions.tweets_by_date.insert(tweet_date.clone(), HashMap::new()); @@ -726,10 +770,10 @@ impl TwitterCache { } pub fn display_id_for_tweet(&self, tweet: &Tweet) -> TweetId { let now = Local::now(); - let tweet_date = tweet.recieved_at.with_timezone(&now.timezone()); + let tweet_date = tweet.recieved_at.with_timezone(&Local); let bare_id = TweetId::Bare(tweet.internal_id); let maybe_dated_id = if now.year() == tweet_date.year() && now.month() == tweet_date.month() && now.day() == tweet_date.day() { - let date_string = format!("{:04}{:02}{:02}", tweet.recieved_at.year(), tweet.recieved_at.month(), tweet.recieved_at.day()); + let date_string = format!("{:04}{:02}{:02}", tweet_date.year(), tweet_date.month(), tweet_date.day()); let today_id = self.id_conversions.tweets_by_date_and_tweet_id.get(&date_string).and_then(|m| m.get(&tweet.internal_id)); today_id.map(|x| TweetId::Today(*x)) } else { |