Skip to content
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

Use offset when getting time duration #1307

Closed
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/info/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
spenserblack marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

@o2sh o2sh Apr 14, 2024

Choose a reason for hiding this comment

The 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): now (since epoch in UTC+0) - commit.time (since epoch in UTC+0.

Sorry if I'm mistaken.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 commit.time.seconds is UTC+0 or if it's a naive time that needs offset to get the real time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I also didn't catch it. commit.time.seconds is UTC. The doc-string there unfortunately only implies it ("time in seconds since epoch."). I have fixed this here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gitoxide supports times before unix epoch, but of course, there are dates outside of the valid range of i64.

});
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>(
Expand Down Expand Up @@ -120,6 +126,21 @@ mod tests {
format_time(time, false);
}

#[test]
fn test_timezone_awareness() {
let current_time = SystemTime::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(SystemTime::now(), also_now).unwrap();
spenserblack marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(diff.as_secs(), 0);
}

#[test]
fn display_time_before_epoch() {
let time = Time::new(gix::date::SecondsSinceUnixEpoch::MIN, 0);
Expand Down