Skip to content

Commit

Permalink
support some timezone abbreviation
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Sep 24, 2024
1 parent 48e521f commit b8f3799
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "test-with"
version = "0.14.0"
version = "0.14.1"
authors = ["Antonio Yang <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion examples/runner/Cargo.lock

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

54 changes: 52 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2879,12 +2879,26 @@ pub fn lock(attr: TokenStream, stream: TokenStream) -> TokenStream {
/// assert!(true);
/// }
///
/// // UTC is GMT+0
/// #[test_with::timezone(UTC)]
/// #[test]
/// fn test_works_too() {
/// assert!(true);
/// }
///
/// // +8 means GMT+8
/// #[test_with::timezone(+8)]
/// #[test]
/// fn test_ignored() {
/// panic!("should be ignored")
/// }
///
/// // HKT GMT+8
/// #[test_with::timezone(HKT)]
/// #[test]
/// fn test_ignored_too() {
/// panic!("should be ignored")
/// }
/// }
/// ```
#[cfg(feature = "timezone")]
Expand All @@ -2907,17 +2921,53 @@ pub fn timezone(attr: TokenStream, stream: TokenStream) -> TokenStream {
}

#[cfg(feature = "timezone")]
fn check_tz_condition(attr_str: String) -> (bool, String) {
fn check_timezone(attr_str: &String) -> (bool, Vec<&str>) {
let mut incorrect_tzs = vec![];
let mut match_tz = false;
let current_tz = chrono::Local::now().offset().local_minus_utc() / 60;

for tz in attr_str.split(',') {
if let Ok(parsed_tz) = tz.parse::<i32>() {
let parsed_tz = match tz {
"NZDT" => Ok(13),
"NZST" => Ok(12),
"AEDT" => Ok(11),
// "ACDT" => Ok(10.5)
"AEST" => Ok(10),
// "ACST" => Ok(9.5)
"KST" | "JST" => Ok(9),
"HKT" | "WITA" | "AWST" => Ok(8), // Duplicate PST
"WIB" => Ok(7),
// "CST" => Ok(8), // Duplicate CST
// "IST" => Ok(5.5)

Check warning on line 2941 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

"IST" should be "IS" or "IT" or "ITS" or "SIT" or "LIST".
"PKT" => Ok(5),
"EAT" | "EEST" | "IDT" | "MSK" => Ok(3),
"CAT" | "EET" | "CEST" | "SAST" => Ok(2), // Duplicate IST

Check warning on line 2944 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

"IST" should be "IS" or "IT" or "ITS" or "SIT" or "LIST".
"CET" | "WAT" | "WEST" | "BST" => Ok(1),
"UTC" | "GMT" | "WET" => Ok(0),
// "NDT" => Ok(-2.5)
// "NST" => Ok(-3.5)
"ADT" => Ok(-3),
"AST" | "EDT" => Ok(-4),
"EST" | "CDT" => Ok(-5),
"MDT" => Ok(-6), // Another CST here
"MST" | "PDT" => Ok(-7),
"AKDT" => Ok(-8), // Another PST here
"HDT" | "AKST" => Ok(-9),
"HST" => Ok(-10),
_ => tz.parse::<i32>(),
};
if let Ok(parsed_tz) = parsed_tz {
match_tz |= current_tz == parsed_tz;
} else {
incorrect_tzs.push(tz);
}
}
(match_tz, incorrect_tzs)
}

#[cfg(feature = "timezone")]
fn check_tz_condition(attr_str: String) -> (bool, String) {
let (match_tz, incorrect_tzs) = check_timezone(&attr_str);

// Generate ignore message
if incorrect_tzs.len() == 1 {
Expand Down

0 comments on commit b8f3799

Please sign in to comment.