Skip to content

Commit

Permalink
Merge pull request #1697 from EliahKagan/run-ci/duration
Browse files Browse the repository at this point in the history
Reveal local-time DST ambiguity where it occurs
  • Loading branch information
Byron authored Nov 23, 2024
2 parents 197d31a + ac17b62 commit 438ee47
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion gix-date/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ thiserror = "2.0.0"
document-features = { version = "0.2.0", optional = true }

[dev-dependencies]
gix-hash = { path = "../gix-hash" }
gix-testtools = { path = "../tests/tools" }
once_cell = "1.12.0"
gix-hash = { path = "../gix-hash" }
pretty_assertions = "1.4.1"

[package.metadata.docs.rs]
all-features = true
Expand Down
53 changes: 35 additions & 18 deletions gix-date/tests/time/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ mod relative {

use gix_date::time::Sign;
use jiff::{ToSpan, Zoned};
use pretty_assertions::assert_eq;

#[test]
fn large_offsets() {
Expand All @@ -179,24 +180,40 @@ mod relative {
#[test]
fn various() {
let now = SystemTime::now();
let two_weeks_ago = gix_date::parse("2 weeks ago", Some(now)).unwrap();
assert_eq!(Sign::Plus, two_weeks_ago.sign);
assert_eq!(0, two_weeks_ago.offset);
let expected = Zoned::try_from(now)
.unwrap()
// account for the loss of precision when creating `Time` with seconds
.round(
jiff::ZonedRound::new()
.smallest(jiff::Unit::Second)
.mode(jiff::RoundMode::Trunc),
)
.unwrap()
.saturating_sub(2.weeks());
assert_eq!(
jiff::Timestamp::from_second(two_weeks_ago.seconds).unwrap(),
expected.timestamp(),
"relative times differ"
);

let cases = [
("2 weeks ago", 2.weeks()),
("14 weeks ago", 14.weeks()),
("26 weeks ago", 26.weeks()),
("38 weeks ago", 38.weeks()),
("50 weeks ago", 50.weeks()),
("20160 minutes ago", 20_160.minutes()), // 2 weeks
("141120 minutes ago", 141_120.minutes()), // 14 weeks
("262080 minutes ago", 262_080.minutes()), // 26 weeks
("383040 minutes ago", 383_040.minutes()), // 38 weeks
("504000 minutes ago", 504_000.minutes()), // 50 weeks
];

let times = cases.map(|(input, _)| gix_date::parse(input, Some(now)).unwrap());

assert_eq!(times.map(|_| Sign::Plus), times.map(|time| time.sign));
assert_eq!(times.map(|_| 0), times.map(|time| time.offset));

let expected = cases.map(|(_, span)| {
Zoned::try_from(now)
.unwrap()
// account for the loss of precision when creating `Time` with seconds
.round(
jiff::ZonedRound::new()
.smallest(jiff::Unit::Second)
.mode(jiff::RoundMode::Trunc),
)
.unwrap()
.saturating_sub(span)
.timestamp()
});
let actual = times.map(|time| jiff::Timestamp::from_second(time.seconds).unwrap());
assert_eq!(actual, expected, "relative times differ");
}
}

Expand Down

0 comments on commit 438ee47

Please sign in to comment.