From cebf55cf8283c74c6c4dba0ea19759e9a2438673 Mon Sep 17 00:00:00 2001 From: Patrick Klitzke Date: Mon, 21 Aug 2023 11:03:56 +0900 Subject: [PATCH] Add a unix timestamp This pr adds tests to support the @ unix timestamp. --- README.md | 2 +- src/parse_datetime.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 92dcf13..05e955f 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ cannot be parsed as a relative time. The `from_str` function returns: -- `Ok(DateTime)` - If the input string can be prsed as a datetime +- `Ok(DateTime)` - If the input string can be parsed as a datetime - `Err(ParseDurationError::InvalidInput)` - If the input string cannot be parsed ## Fuzzer diff --git a/src/parse_datetime.rs b/src/parse_datetime.rs index 3b6ba9c..7b1f536 100644 --- a/src/parse_datetime.rs +++ b/src/parse_datetime.rs @@ -243,6 +243,25 @@ mod tests { } } + #[cfg(test)] + mod timestamp { + use crate::parse_datetime::from_str; + use chrono::{TimeZone, Utc}; + + #[test] + fn test_positive_offsets() { + let offsets: Vec = vec![ + 0, 1, 2, 10, 100, 150, 2000, 1234400000, 1334400000, 1692582913, 2092582910, + ]; + + for offset in offsets { + let time = Utc.timestamp(offset, 0); + let dt = from_str(format!("@{}", offset)); + assert_eq!(dt.unwrap(), time); + } + } + } + /// Used to test example code presented in the README. mod readme_test { use crate::parse_datetime::from_str;