Skip to content
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

Fix #1393 - Parse datetime to spec without panic #1550

Merged
merged 7 commits into from
Jan 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 45 additions & 36 deletions lib/src/sql/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use crate::sql::duration::Duration;
use crate::sql::error::IResult;
use crate::sql::escape::escape_str;
use crate::sql::serde::is_internal_serialization;
use chrono::{DateTime, FixedOffset, SecondsFormat, TimeZone, Utc};
use chrono::{DateTime, FixedOffset, NaiveDate, Offset, SecondsFormat, TimeZone, Utc};
use nom::branch::alt;
use nom::character::complete::char;
use nom::combinator::map;
use nom::error::ErrorKind;
use nom::sequence::delimited;
use nom::{error_position, Err};
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Formatter};
use std::ops;
Expand Down Expand Up @@ -120,8 +122,7 @@ fn date(i: &str) -> IResult<&str, Datetime> {
let (i, _) = char('-')(i)?;
let (i, day) = day(i)?;

let d = Utc.ymd(year, mon, day).and_hms(0, 0, 0);
Ok((i, Datetime(d)))
convert(i, year, mon, day, 0, 0, 0, 0, Utc.fix())
}

fn time(i: &str) -> IResult<&str, Datetime> {
Expand All @@ -138,19 +139,7 @@ fn time(i: &str) -> IResult<&str, Datetime> {
let (i, sec) = second(i)?;
let (i, zone) = zone(i)?;

let v = match zone {
Some(z) => {
let d = z.ymd(year, mon, day).and_hms(hour, min, sec);
let d = d.with_timezone(&Utc);
Datetime(d)
}
None => {
let d = Utc.ymd(year, mon, day).and_hms(hour, min, sec);
Datetime(d)
}
};

Ok((i, v))
convert(i, year, mon, day, hour, min, sec, 0, zone)
}

fn nano(i: &str) -> IResult<&str, Datetime> {
Expand All @@ -168,19 +157,31 @@ fn nano(i: &str) -> IResult<&str, Datetime> {
let (i, nano) = nanosecond(i)?;
let (i, zone) = zone(i)?;

let v = match zone {
Some(z) => {
let d = z.ymd(year, mon, day).and_hms_nano(hour, min, sec, nano);
let d = d.with_timezone(&Utc);
Datetime(d)
}
None => {
let d = Utc.ymd(year, mon, day).and_hms_nano(hour, min, sec, nano);
Datetime(d)
}
};
convert(i, year, mon, day, hour, min, sec, nano, zone)
}

Ok((i, v))
fn convert(
i: &str,
year: i32,
mon: u32,
day: u32,
hour: u32,
min: u32,
sec: u32,
nano: u32,
zone: FixedOffset,
) -> IResult<&str, Datetime> {
let n = NaiveDate::from_ymd_opt(year, mon, day)
.ok_or(Err::Error(error_position!(i, ErrorKind::Verify)))?;

let d = zone
.from_local_date(&n)
.unwrap()
.and_hms_nano_opt(hour, min, sec, nano)
.ok_or(Err::Error(error_position!(i, ErrorKind::Verify)))?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the correct way to send errors, tbh GitHub CoPilot wrote it. It works in my testing though.

.with_timezone(&Utc);

Ok((i, Datetime(d)))
}

fn year(i: &str) -> IResult<&str, i32> {
Expand Down Expand Up @@ -226,28 +227,28 @@ fn nanosecond(i: &str) -> IResult<&str, u32> {
Ok((i, v))
}

fn zone(i: &str) -> IResult<&str, Option<FixedOffset>> {
fn zone(i: &str) -> IResult<&str, FixedOffset> {
alt((zone_utc, zone_all))(i)
}

fn zone_utc(i: &str) -> IResult<&str, Option<FixedOffset>> {
fn zone_utc(i: &str) -> IResult<&str, FixedOffset> {
let (i, _) = char('Z')(i)?;
Ok((i, None))
Ok((i, Utc.fix()))
}

fn zone_all(i: &str) -> IResult<&str, Option<FixedOffset>> {
fn zone_all(i: &str) -> IResult<&str, FixedOffset> {
let (i, s) = sign(i)?;
let (i, h) = hour(i)?;
let (i, _) = char(':')(i)?;
let (i, m) = minute(i)?;
if h == 0 && m == 0 {
Ok((i, None))
Ok((i, Utc.fix()))
} else if s < 0 {
Ok((i, { Some(FixedOffset::west((h * 3600 + m * 60) as i32)) }))
Ok((i, FixedOffset::west((h * 3600 + m * 60) as i32)))
} else if s > 0 {
Ok((i, { Some(FixedOffset::east((h * 3600 + m * 60) as i32)) }))
Ok((i, FixedOffset::east((h * 3600 + m * 60) as i32)))
} else {
Ok((i, None))
Ok((i, Utc.fix()))
}
}

Expand Down Expand Up @@ -307,4 +308,12 @@ mod tests {
let out = res.unwrap().1;
assert_eq!("'2012-04-24T02:55:43.511Z'", format!("{}", out));
}

#[test]
fn date_time_illegal_date() {
// Hey! There's not a 31st of November!
let sql = "2022-11-31T12:00:00.000Z";
let res = datetime_raw(sql);
assert!(res.is_err());
}
}