Skip to content

Fixes Negative numbers for @ not being recognized #43

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

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ readme = "README.md"
[dependencies]
regex = "1.9"
chrono = { version="0.4", default-features=false, features=["std", "alloc", "clock"] }
nom = "7.1.3"
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ use std::fmt::{self, Display};

// Expose parse_datetime
mod parse_relative_time;
mod parse_timestamp;

use chrono::{DateTime, FixedOffset, Local, LocalResult, NaiveDateTime, TimeZone};

use parse_relative_time::parse_relative_time;
use parse_timestamp::parse_timestamp;

#[derive(Debug, PartialEq)]
pub enum ParseDateTimeError {
Expand Down Expand Up @@ -169,9 +171,9 @@ pub fn parse_datetime_at_date<S: AsRef<str> + Clone>(
}

// Parse epoch seconds
if s.as_ref().bytes().next() == Some(b'@') {
if let Ok(parsed) = NaiveDateTime::parse_from_str(&s.as_ref()[1..], "%s") {
if let Ok(dt) = naive_dt_to_fixed_offset(date, parsed) {
if let Ok(timestamp) = parse_timestamp(s.as_ref()) {
if let Some(timestamp_date) = NaiveDateTime::from_timestamp_opt(timestamp, 0) {
if let Ok(dt) = naive_dt_to_fixed_offset(date, timestamp_date) {
return Ok(dt);
}
}
Expand Down Expand Up @@ -359,15 +361,21 @@ mod tests {
use chrono::{TimeZone, Utc};

#[test]
fn test_positive_offsets() {
fn test_positive_and_negative_offsets() {
let offsets: Vec<i64> = vec![
0, 1, 2, 10, 100, 150, 2000, 1234400000, 1334400000, 1692582913, 2092582910,
];

for offset in offsets {
// positive offset
let time = Utc.timestamp_opt(offset, 0).unwrap();
let dt = parse_datetime(format!("@{}", offset));
assert_eq!(dt.unwrap(), time);

// negative offset
let time = Utc.timestamp_opt(-offset, 0).unwrap();
let dt = parse_datetime(format!("@-{}", offset));
assert_eq!(dt.unwrap(), time);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/parse_relative_time.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use crate::ParseDateTimeError;
use chrono::{Duration, Local, NaiveDate, Utc};
use regex::Regex;
Expand Down
111 changes: 111 additions & 0 deletions src/parse_timestamp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use core::fmt;
use std::error::Error;
use std::fmt::Display;
use std::num::ParseIntError;

use nom::branch::alt;
Copy link
Contributor

Choose a reason for hiding this comment

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

please add the license header

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

use nom::character::complete::{char, digit1};
use nom::combinator::all_consuming;
use nom::multi::fold_many0;
use nom::sequence::preceded;
use nom::sequence::tuple;
use nom::{self, IResult};

#[derive(Debug, PartialEq)]
pub enum ParseTimestampError {
InvalidNumber(ParseIntError),
InvalidInput,
}

impl Display for ParseTimestampError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidInput => {
write!(f, "Invalid input string: cannot be parsed as a timestamp")
}
Self::InvalidNumber(err) => {
write!(f, "Invalid timestamp number: {err}")
}
}
}
}

impl Error for ParseTimestampError {}

// TODO is this necessary
impl From<ParseIntError> for ParseTimestampError {
fn from(err: ParseIntError) -> Self {
Self::InvalidNumber(err)
}
}

type NomError<'a> = nom::Err<nom::error::Error<&'a str>>;

impl<'a> From<NomError<'a>> for ParseTimestampError {
fn from(_err: NomError<'a>) -> Self {
Self::InvalidInput
}
}

pub(crate) fn parse_timestamp(s: &str) -> Result<i64, ParseTimestampError> {
let s = s.trim().to_lowercase();
let s = s.as_str();

let res: IResult<&str, (char, &str)> = all_consuming(preceded(
char('@'),
tuple((
// Note: to stay compatible with gnu date this code allows
// multiple + and - and only considers the last one
fold_many0(
// parse either + or -
alt((char('+'), char('-'))),
// start with a +
|| '+',
// whatever we get (+ or -), update the accumulator to that value
|_, c| c,
),
digit1,
)),
))(s);

let (_, (sign, number_str)) = res?;

let mut number = number_str.parse::<i64>()?;

if sign == '-' {
number *= -1;
}

Ok(number)
}

#[cfg(test)]
mod tests {

use crate::parse_timestamp::parse_timestamp;

#[test]
fn test_valid_timestamp() {
assert_eq!(parse_timestamp("@1234"), Ok(1234));
assert_eq!(parse_timestamp("@99999"), Ok(99999));
assert_eq!(parse_timestamp("@-4"), Ok(-4));
assert_eq!(parse_timestamp("@-99999"), Ok(-99999));
assert_eq!(parse_timestamp("@+4"), Ok(4));
assert_eq!(parse_timestamp("@0"), Ok(0));

// gnu date accepts numbers signs and uses the last sign
assert_eq!(parse_timestamp("@---+12"), Ok(12));
assert_eq!(parse_timestamp("@+++-12"), Ok(-12));
assert_eq!(parse_timestamp("@+----+12"), Ok(12));
assert_eq!(parse_timestamp("@++++-123"), Ok(-123));
}

#[test]
fn test_invalid_timestamp() {
assert!(parse_timestamp("@").is_err());
assert!(parse_timestamp("@+--+").is_err());
assert!(parse_timestamp("@+1ab2").is_err());
}
}