Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Use system timezone database when available #7849

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

Closed
wants to merge 8 commits into from
Closed
Prev Previous commit
Next Next commit
uucore: custom_tz_fmt: Add abbreviation tests
This function will still need some love, but at least with this,
we have basic tests.
  • Loading branch information
drinkcat committed Apr 27, 2025
commit 508ba25e33b187ea0600080430481eb7cb8d8947
39 changes: 39 additions & 0 deletions 39 src/uucore/src/lib/features/custom_tz_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (misc) WARST zoneinfo

use chrono::{TimeZone, Utc};
use chrono_tz::{OffsetName, Tz};
use iana_time_zone::get_timezone;
Expand Down Expand Up @@ -70,4 +72,41 @@ mod tests {
);
assert_eq!(custom_time_format("%Z"), timezone_abbreviation());
}

#[test]
fn test_timezone_abbreviation() {
// Test if a timezone abbreviation is one of the values in ok_abbr.
// TODO(#7659): We should modify this test to 2 fixed dates, one that falls in
// daylight savings, and the other not. But right now the abbreviation depends
// on the current time.
fn test_zone(zone: &str, ok_abbr: &[&str]) {
unsafe {
std::env::set_var("TZ", zone);
}
let abbr = timezone_abbreviation();
assert!(
ok_abbr.contains(&abbr.as_str()),
"Timezone {zone} abbreviation {abbr} is not contained within [{}].",
ok_abbr.join(", ")
)
}

// Test a few random timezones.
test_zone("US/Pacific", &["PST", "PDT"]);
test_zone("Europe/Zurich", &["CEST", "CET"]);
test_zone("Africa/Cairo", &["EET", "EEST"]); // spell-checker:disable-line
test_zone("Asia/Taipei", &["CST"]);
test_zone("Australia/Sydney", &["AEDT", "AEST"]); // spell-checker:disable-line
// Looks like Pacific/Tahiti is provided in /usr/share/zoneinfo, but not in chrono-tz (yet).
//test_zone("Pacific/Tahiti", &["-10"]); // No abbreviation?
test_zone("Antarctica/South_Pole", &["NZDT", "NZST"]); // spell-checker:disable-line

// TODO: This is not fully exhaustive, we should understand how to handle
// invalid TZ values and more complex POSIX-specified values:
// https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
// Examples:
//test_zone("WART4WARST,J1/0,J365/25", &["WART", "WARST"])
//test_zone(":Europe/Zurich", &["CEST", "CET"]);
//test_zone("invalid", &["invalid"]);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.