diff options
author | Andy Wortman <ixineeringeverywhere@gmail.com> | 2017-12-12 00:59:28 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-12 00:59:28 -0800 |
commit | 1b1a3b9154cb7f80126e622b9de87f9a5feccc98 (patch) | |
tree | 03d64742a3dab7d18792814f62650d9758b1610e /src | |
parent | 402e45f1e1c173643e485f692984bf49ad278f23 (diff) | |
parent | 3e322317ad9547ae76f887d0156dc8c813228fca (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 { |