Skip to content

Commit

Permalink
restrict month day numbers in date formats (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal authored Dec 30, 2024
1 parent 643e2e1 commit dbe1735
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions parser/src/json/formats.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
pub fn lookup_format(name: &str) -> Option<&str> {
let r = match name {
"date-time" => {
r"^(?P<date>[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01]))[tT](?P<time>(?:[01][0-9]|2[0-3]):[0-5][0-9]:(?:[0-5][0-9]|60)(?P<time_fraction>\.[0-9]+)?(?P<time_zone>[zZ]|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9]))$"
}
"date-time" => concat!(
r"^(?P<date>",
r"[0-9]{4}-(?:",
r"(?:0[13578]|1[02])-(?:0[1-9]|[12][0-9]|3[01])|", // 31-day months
r"(?:0[469]|11)-(?:0[1-9]|[12][0-9]|30)|", // 30-day months
r"(?:02)-(?:0[1-9]|1[0-9]|2[0-9])", // February up to 29 days
r"))",
r"[tT](?P<time>",
r"(?:[01][0-9]|2[0-3]):[0-5][0-9]:", // Hours, Minutes
r"(?:[0-5][0-9]|60)", // Seconds (including leap second 60)
r"(?P<time_fraction>\.[0-9]+)?", // Optional fractional seconds
r"(?P<time_zone>",
r"[zZ]|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9]", // Time zone
r")",
r")$"
),
"time" => {
r"^(?:[01][0-9]|2[0-3]):[0-5][0-9]:(?:[0-5][0-9]|60)(?P<time_fraction>\.[0-9]+)?(?P<time_zone>[zZ]|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9])$"
}
"date" => r"^[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])$",
"date" => concat!(
r"^(?:",
r"(?:[0-9]{4}-(?:0[13578]|1[02])-(?:0[1-9]|[12][0-9]|3[01]))|", // Months with 31 days
r"(?:[0-9]{4}-(?:0[469]|11)-(?:0[1-9]|[12][0-9]|30))|", // Months with 30 days
r"(?:[0-9]{4}-02-(?:0[1-9]|1[0-9]|2[0-9]))", // February with up to 29 days
r")$"
),
"duration" => {
r"^P(?:(?P<dur_date>(?:(?P<dur_year>[0-9]+Y(?:[0-9]+M(?:[0-9]+D)?)?)|(?P<dur_month>[0-9]+M(?:[0-9]+D)?)|(?P<dur_day>[0-9]+D))(?:T(?:(?P<dur_hour>[0-9]+H(?:[0-9]+M(?:[0-9]+S)?)?)|(?P<dur_minute>[0-9]+M(?:[0-9]+S)?)|(?P<dur_second>[0-9]+S)))?)|(?P<dur_time>T(?:(?P<dur_hour2>[0-9]+H(?:[0-9]+M(?:[0-9]+S)?)?)|(?P<dur_minute2>[0-9]+M(?:[0-9]+S)?)|(?P<dur_second2>[0-9]+S)))|(?P<dur_week>[0-9]+W))$"
}
Expand Down

0 comments on commit dbe1735

Please sign in to comment.