Skip to content

Commit

Permalink
Datetime extension: Allow timezone offsets with a duration of less th…
Browse files Browse the repository at this point in the history
…an 24 hours (#1449)

Signed-off-by: Adrian Palacios <accorell@amazon.com>
  • Loading branch information
adpaco-aws authored Feb 5, 2025
1 parent 898e15b commit 868c759
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions cedar-policy-core/src/extensions/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ enum DateTimeParseError {
#[help("A valid datetime should end with Z|.SSSZ|(+|-)hhmm|.SSS(+|-)hhmm")]
InvalidMSOffsetPattern,
#[error("invalid offset range: {}{}", ._0.0, ._0.1)]
#[help("A valid offset hour range should be [0,12) and minute range should be [0, 60)")]
#[help("A valid offset hour range should be [0,24) and minute range should be [0, 60)")]
InvalidOffset((u32, u32)),
}

Expand All @@ -508,16 +508,9 @@ struct UTCOffset {
}

impl UTCOffset {
const MIN: Self = Self {
positive: false,
hh: 12,
mm: 0,
};
const MAX: Self = Self {
positive: true,
hh: 14,
mm: 0,
};
const MAX_HH: u32 = 24;
const MAX_MM: u32 = 60;

fn to_seconds(&self) -> i64 {
let offset_in_seconds_unsigned = (self.hh * 3600 + self.mm * 60) as i64;
if self.positive {
Expand All @@ -527,9 +520,8 @@ impl UTCOffset {
}
}

// Reference: https://en.wikipedia.org/wiki/List_of_UTC_offsets
fn is_valid(&self) -> bool {
self.mm < 60 && self >= &Self::MIN && self <= &Self::MAX
self.hh < Self::MAX_HH && self.mm < Self::MAX_MM
}
}

Expand Down Expand Up @@ -790,6 +782,16 @@ mod tests {
parse_datetime(s).unwrap(),
NaiveDateTime::from_str("2024-10-15T23:12:02").unwrap()
);
let s = "2024-10-15T23:59:00+2359";
assert_eq!(
parse_datetime(s).unwrap(),
NaiveDateTime::from_str("2024-10-15T00:00:00").unwrap()
);
let s = "2024-10-15T00:00:00-2359";
assert_eq!(
parse_datetime(s).unwrap(),
NaiveDateTime::from_str("2024-10-15T23:59:00").unwrap()
);
}

#[test]
Expand Down Expand Up @@ -924,6 +926,10 @@ mod tests {
parse_datetime("2016-12-31T00:00:00+1199"),
Err(DateTimeParseError::InvalidOffset((11, 99)))
);
assert_matches!(
parse_datetime("2016-12-31T00:00:00+2400"),
Err(DateTimeParseError::InvalidOffset((24, 0)))
);
}

#[track_caller]
Expand Down

0 comments on commit 868c759

Please sign in to comment.