Skip to content

Commit 66d1e8a

Browse files
committed
test_date: Expand on test_date_utc_time
Using the current time requires a bit of care, but it's nice to have a test that doesn't use a fixed date as input.
1 parent 5d75e28 commit 66d1e8a

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

tests/by-util/test_date.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,39 @@ fn test_date_tz_with_relative_time() {
657657
#[test]
658658
fn test_date_utc_time() {
659659
// Test that -u flag shows correct UTC time
660-
new_ucmd!().arg("-u").arg("+%H:%M").succeeds();
660+
// We get 2 UTC times just in case we're really unlucky and this runs around
661+
// an hour change.
662+
let utc_hour_1: i32 = new_ucmd!()
663+
.env("TZ", "Asia/Taipei")
664+
.arg("-u")
665+
.arg("+%-H")
666+
.succeeds()
667+
.stdout_str()
668+
.trim_end()
669+
.parse()
670+
.unwrap();
671+
let tpe_hour: i32 = new_ucmd!()
672+
.env("TZ", "Asia/Taipei")
673+
.arg("+%-H")
674+
.succeeds()
675+
.stdout_str()
676+
.trim_end()
677+
.parse()
678+
.unwrap();
679+
let utc_hour_2: i32 = new_ucmd!()
680+
.env("TZ", "Asia/Taipei")
681+
.arg("-u")
682+
.arg("+%-H")
683+
.succeeds()
684+
.stdout_str()
685+
.trim_end()
686+
.parse()
687+
.unwrap();
688+
// Taipei is always 8 hours ahead of UTC (no daylight savings)
689+
assert!(
690+
(tpe_hour - utc_hour_1 + 24) % 24 == 8 || (tpe_hour - utc_hour_2 + 24) % 24 == 8,
691+
"TPE: {tpe_hour} UTC: {utc_hour_1}/{utc_hour_2}"
692+
);
661693

662694
// Test that -u flag shows UTC timezone
663695
new_ucmd!()

0 commit comments

Comments
 (0)