-
Notifications
You must be signed in to change notification settings - Fork 289
Use offset when getting time duration #1307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,20 +24,26 @@ where | |
} | ||
|
||
fn to_human_time(time: Time) -> String { | ||
let since_epoch_duration = SystemTime::now() | ||
.duration_since(SystemTime::UNIX_EPOCH) | ||
.unwrap(); | ||
let duration = match diff_gix_time(SystemTime::now(), time) { | ||
Ok(d) => d, | ||
Err(s) => return s, | ||
}; | ||
let ht = HumanTime::from(-(duration.as_secs() as i64)); | ||
ht.to_string() | ||
} | ||
|
||
let ts = Duration::from_secs(match time.seconds.try_into() { | ||
/// Gets the duration between `now` and `time`. Returns `Err` if this cannot be calculated. | ||
fn diff_gix_time(now: SystemTime, time: Time) -> Result<Duration, String> { | ||
let since_epoch_duration = now.duration_since(SystemTime::UNIX_EPOCH).unwrap(); | ||
let ts = Duration::from_secs(match (time.seconds + i64::from(time.offset)).try_into() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused. Why do we need to consider the offset here? This method should simply return the difference between two UTC timestamps (without timezone): Sorry if I'm mistaken. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made this with the assumption that #1306 was caused by differing timezones, which was an incorrect assumption, which made me assume that we were comparing a timezone-aware time to a naive time. Right now, personally, I am not sure if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I also didn't catch it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I guess this PR should be closed, then. |
||
Ok(s) => s, | ||
Err(_) => return "<before UNIX epoch>".into(), | ||
Err(_) => return Err("<before UNIX epoch>".into()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); | ||
let duration = since_epoch_duration.checked_sub(ts).expect( | ||
"Achievement unlocked: time travel! \ | ||
Check your system clock and commit dates.", | ||
); | ||
let ht = HumanTime::from(-(duration.as_secs() as i64)); | ||
ht.to_string() | ||
Ok(duration) | ||
} | ||
|
||
pub fn format_number<T: ToFormattedString + std::fmt::Display>( | ||
|
@@ -120,6 +126,20 @@ mod tests { | |
format_time(time, false); | ||
} | ||
|
||
#[test] | ||
fn test_timezone_awareness() { | ||
let now = SystemTime::now(); | ||
let current_time = now.duration_since(SystemTime::UNIX_EPOCH).unwrap(); | ||
let hour_offset: i32 = 1; | ||
let offset = 60 * 60 * hour_offset; | ||
let also_now = Time::new( | ||
(current_time.as_secs() as gix::date::SecondsSinceUnixEpoch) + i64::from(offset), | ||
-offset, | ||
); | ||
let diff = diff_gix_time(now, also_now).unwrap(); | ||
assert_eq!(diff.as_secs(), 0); | ||
} | ||
|
||
#[test] | ||
fn display_time_before_epoch() { | ||
let time = Time::new(gix::date::SecondsSinceUnixEpoch::MIN, 0); | ||
|
Uh oh!
There was an error while loading. Please reload this page.