Skip to content
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
3 changes: 3 additions & 0 deletions crates/winnow-datetime/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.2.2 - 2015-05-14
* Added convert::jiff for jiff support of date and time

## 0.2.1 - 2015-05-11
* Made the convert modules public so that they can be used outside of the crate.
* Added TryInto for time::OffsetDateTime
Expand Down
4 changes: 3 additions & 1 deletion crates/winnow-datetime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "winnow_datetime"
version = "0.2.1"
version = "0.2.2"
description = "Parsing dates using winnow"
keywords = [ "iso8601", "date-time", "parser", "winnow" ]
categories = [ "parser-implementations", "date-and-time" ]
Expand All @@ -15,6 +15,7 @@ edition = "2021"
[dependencies]
winnow = "0.7"
chrono = { version = "0.4", default-features = false, optional = true }
jiff = { version = "0.2.13", optional = true }
time = { version = "0.3.37", default-features = false, optional = true }
num-traits = { version = "0.2", optional = true }
paste = "1.0.15"
Expand All @@ -24,5 +25,6 @@ serde = { version = "1.0", features = ["derive"], optional = true }
default = ["std"]
std = ["winnow/std"]
chrono = ["dep:chrono", "dep:num-traits"]
jiff = ["dep:jiff", "dep:num-traits"]
time = ["dep:time", "dep:num-traits"]
serde = ["dep:serde"]
6 changes: 6 additions & 0 deletions crates/winnow-datetime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ is the most common format used on the internet.
dates, times, durations, and intervals. [ISO8601](https://en.wikipedia.org/wiki/ISO_8601) is a very ambitious format
that can represent a wide range of date and time concepts.

### Conversion
[winnow-datetime] provides a set of TryInto implementations to convert to common rust date/time libraries. Currently
chrono, jiff, and time are supported. Each have a feature flag of the same name as the lib to enable support for the
conversions. The TryInto implementations are available with the features and so try_into() could be called to convert to
any of the compatible types.

## Parsing Something Strange
Despite there being countless specifications some people will still come up with their own way to poetically express a
datetime. So if you are looking to parse those you can build the provided structs with any combination of the pieces
Expand Down
44 changes: 22 additions & 22 deletions crates/winnow-datetime/src/convert/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use num_traits::FromPrimitive;
impl TryFrom<crate::Date> for chrono::NaiveDate {
type Error = ();

fn try_from(iso: crate::Date) -> Result<Self, Self::Error> {
let maybe = match iso {
fn try_from(d: crate::Date) -> Result<Self, Self::Error> {
let maybe = match d {
crate::Date::YMD { year, month, day } => {
chrono::NaiveDate::from_ymd_opt(year, month, day)
}
Expand Down Expand Up @@ -37,37 +37,37 @@ mod test_date {

#[test]
fn naivedate_from_ymd() {
let iso = crate::Date::YMD {
let d = crate::Date::YMD {
year: 2023,
month: 2,
day: 8,
};
let naive = chrono::NaiveDate::try_from(iso).unwrap();
let naive = chrono::NaiveDate::try_from(d).unwrap();
assert_eq!(naive.year(), 2023);
assert_eq!(naive.month(), 2);
assert_eq!(naive.day(), 8);
}

#[test]
fn naivedate_from_ywd() {
let iso = Date::Week {
let d = Date::Week {
year: 2023,
week: 6,
day: 2,
};
let naive = chrono::NaiveDate::try_from(iso).unwrap();
let naive = chrono::NaiveDate::try_from(d).unwrap();
assert_eq!(naive.year(), 2023);
assert_eq!(naive.month(), 2);
assert_eq!(naive.day(), 8);
}

#[test]
fn naivedate_from_ordinal() {
let iso = crate::Date::Ordinal {
let d = crate::Date::Ordinal {
year: 2023,
day: 39,
};
let naive = chrono::NaiveDate::try_from(iso).unwrap();
let naive = chrono::NaiveDate::try_from(d).unwrap();
assert_eq!(naive.year(), 2023);
assert_eq!(naive.month(), 2);
assert_eq!(naive.day(), 8);
Expand All @@ -76,8 +76,8 @@ mod test_date {

impl TryFrom<crate::Time> for chrono::NaiveTime {
type Error = ();
fn try_from(iso: crate::Time) -> Result<Self, Self::Error> {
chrono::NaiveTime::from_hms_opt(iso.hour, iso.minute, iso.second).ok_or(())
fn try_from(t: crate::Time) -> Result<Self, Self::Error> {
chrono::NaiveTime::from_hms_opt(t.hour, t.minute, t.second).ok_or(())
}
}

Expand All @@ -91,17 +91,17 @@ impl crate::Time {
impl TryFrom<crate::DateTime> for chrono::DateTime<chrono::FixedOffset> {
type Error = ();

fn try_from(iso: crate::DateTime) -> Result<Self, Self::Error> {
let offset = iso.time.offset.unwrap_or(crate::Offset {
fn try_from(dt: crate::DateTime) -> Result<Self, Self::Error> {
let offset = dt.time.offset.unwrap_or(crate::Offset {
offset_hours: 0,
offset_minutes: 0,
});

let offset_minutes = offset.offset_hours * 3600 + offset.offset_minutes;
let offset = chrono::FixedOffset::east_opt(offset_minutes).ok_or(())?;

let naive_time = chrono::NaiveTime::try_from(iso.time)?;
let naive_date_time = chrono::NaiveDate::try_from(iso.date)?.and_time(naive_time);
let naive_time = chrono::NaiveTime::try_from(dt.time)?;
let naive_date_time = chrono::NaiveDate::try_from(dt.date)?.and_time(naive_time);

offset
.from_local_datetime(&naive_date_time)
Expand Down Expand Up @@ -129,7 +129,7 @@ mod test_datetime {

#[test]
fn datetime_from_iso_ymd_offset() {
let iso = crate::DateTime {
let dt = crate::DateTime {
date: crate::Date::YMD {
year: 2023,
month: 2,
Expand All @@ -146,7 +146,7 @@ mod test_datetime {
}),
},
};
let datetime = chrono::DateTime::try_from(iso).unwrap();
let datetime = chrono::DateTime::try_from(dt).unwrap();

assert_eq!(datetime.year(), 2023);
assert_eq!(datetime.month(), 2);
Expand All @@ -159,7 +159,7 @@ mod test_datetime {

#[test]
fn datetime_from_iso_ymd_utc() {
let iso = crate::DateTime {
let dt = crate::DateTime {
date: crate::Date::YMD {
year: 2023,
month: 2,
Expand All @@ -176,7 +176,7 @@ mod test_datetime {
}),
},
};
let datetime = chrono::DateTime::try_from(iso).unwrap();
let datetime = chrono::DateTime::try_from(dt).unwrap();

assert_eq!(datetime.year(), 2023);
assert_eq!(datetime.month(), 2);
Expand All @@ -189,7 +189,7 @@ mod test_datetime {

#[test]
fn datetime_from_iso_ymd_no_offset() {
let iso = crate::DateTime {
let dt = crate::DateTime {
date: crate::Date::YMD {
year: 2023,
month: 2,
Expand All @@ -206,7 +206,7 @@ mod test_datetime {
}),
},
};
let datetime = chrono::DateTime::try_from(iso).unwrap();
let datetime = chrono::DateTime::try_from(dt).unwrap();

assert_eq!(datetime.year(), 2023);
assert_eq!(datetime.month(), 2);
Expand All @@ -219,7 +219,7 @@ mod test_datetime {

#[test]
fn datetime_from_iso_ywd() {
let iso = crate::DateTime {
let dt = crate::DateTime {
date: crate::Date::Week {
year: 2023,
week: 6,
Expand All @@ -236,7 +236,7 @@ mod test_datetime {
}),
},
};
let datetime = chrono::DateTime::try_from(iso).unwrap();
let datetime = chrono::DateTime::try_from(dt).unwrap();

assert_eq!(datetime.year(), 2023);
assert_eq!(datetime.month(), 2);
Expand Down
Loading
Loading