From 412aecad562cc1ca80d4ffc33473da0c6ab6e93a Mon Sep 17 00:00:00 2001 From: Lucas Colombo Date: Tue, 16 Apr 2024 07:29:54 -0300 Subject: [PATCH] =?UTF-8?q?feat(sched):=20=E2=9C=A8=20accept=20UTC-=20and?= =?UTF-8?q?=20UTC+=20format=20in=20tz=5Fto=5Fs=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/sched/utils/mod.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/sched/utils/mod.rs b/lib/sched/utils/mod.rs index d4e1001..efe6409 100644 --- a/lib/sched/utils/mod.rs +++ b/lib/sched/utils/mod.rs @@ -9,7 +9,7 @@ const INVALID_OFFSET_MIN_ERR: &str = "invalid timezone offset (minute should be const INVALID_TIME_ERR: &str = "invalid time format, expected `hh:mm{{:ss}}?`"; /// 🧉 » parses a time string into hours, minutes and seconds -/// +/// /// e.g. `12:30` -> `(12, 30, 0)` pub fn parse_time(time: &str) -> Result<(u32, u32, u32)> { let parts: Vec<&str> = time.split(':').collect(); @@ -52,10 +52,19 @@ pub fn hm_to_s(h: i32, m: i32) -> i32 { pub fn tz_to_s(offset: &str) -> Result { // if it doesn't start with '+' or '-', it's invalid ensure!( - offset.starts_with('+') || offset.starts_with('-'), + offset.starts_with('+') + || offset.starts_with('-') + || offset.starts_with("UTC-") + || offset.starts_with("UTC+"), NO_SIGN_ERR ); + let offset = if offset.starts_with("UTC") { + offset[3..].to_string() + } else { + offset.to_string() + }; + let sign = if offset.starts_with('+') { 1 } else { -1 }; let parts: Vec<&str> = offset[1..].split(':').collect(); @@ -108,6 +117,16 @@ mod tests { assert_eq!(tz_to_s("-3")?, -10800); assert_eq!(tz_to_s("-3:30")?, -12600); + // UTC+ and UTC- are also valid + + assert_eq!(tz_to_s("UTC+01:00")?, 3600); + assert_eq!(tz_to_s("UTC-03:00")?, -10800); + assert_eq!(tz_to_s("UTC+03")?, 10800); + assert_eq!(tz_to_s("UTC+00:00")?, 0); + assert_eq!(tz_to_s("UTC-00:00")?, 0); + assert_eq!(tz_to_s("UTC-3")?, -10800); + assert_eq!(tz_to_s("UTC-3:30")?, -12600); + Ok(()) }