Skip to content

Commit 71f72af

Browse files
authored
Merge pull request #9 from soulstompp/jiff-convert
jiff conversions for dates, times and durations
2 parents cf32cdf + 302a696 commit 71f72af

File tree

7 files changed

+268
-39
lines changed

7 files changed

+268
-39
lines changed

crates/winnow-datetime/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.2.2 - 2015-05-14
2+
* Added convert::jiff for jiff support of date and time
3+
14
## 0.2.1 - 2015-05-11
25
* Made the convert modules public so that they can be used outside of the crate.
36
* Added TryInto for time::OffsetDateTime

crates/winnow-datetime/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "winnow_datetime"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
description = "Parsing dates using winnow"
55
keywords = [ "iso8601", "date-time", "parser", "winnow" ]
66
categories = [ "parser-implementations", "date-and-time" ]
@@ -15,6 +15,7 @@ edition = "2021"
1515
[dependencies]
1616
winnow = "0.7"
1717
chrono = { version = "0.4", default-features = false, optional = true }
18+
jiff = { version = "0.2.13", optional = true }
1819
time = { version = "0.3.37", default-features = false, optional = true }
1920
num-traits = { version = "0.2", optional = true }
2021
paste = "1.0.15"
@@ -24,5 +25,6 @@ serde = { version = "1.0", features = ["derive"], optional = true }
2425
default = ["std"]
2526
std = ["winnow/std"]
2627
chrono = ["dep:chrono", "dep:num-traits"]
28+
jiff = ["dep:jiff", "dep:num-traits"]
2729
time = ["dep:time", "dep:num-traits"]
2830
serde = ["dep:serde"]

crates/winnow-datetime/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ is the most common format used on the internet.
2929
dates, times, durations, and intervals. [ISO8601](https://en.wikipedia.org/wiki/ISO_8601) is a very ambitious format
3030
that can represent a wide range of date and time concepts.
3131

32+
### Conversion
33+
[winnow-datetime] provides a set of TryInto implementations to convert to common rust date/time libraries. Currently
34+
chrono, jiff, and time are supported. Each have a feature flag of the same name as the lib to enable support for the
35+
conversions. The TryInto implementations are available with the features and so try_into() could be called to convert to
36+
any of the compatible types.
37+
3238
## Parsing Something Strange
3339
Despite there being countless specifications some people will still come up with their own way to poetically express a
3440
datetime. So if you are looking to parse those you can build the provided structs with any combination of the pieces

crates/winnow-datetime/src/convert/chrono.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use num_traits::FromPrimitive;
77
impl TryFrom<crate::Date> for chrono::NaiveDate {
88
type Error = ();
99

10-
fn try_from(iso: crate::Date) -> Result<Self, Self::Error> {
11-
let maybe = match iso {
10+
fn try_from(d: crate::Date) -> Result<Self, Self::Error> {
11+
let maybe = match d {
1212
crate::Date::YMD { year, month, day } => {
1313
chrono::NaiveDate::from_ymd_opt(year, month, day)
1414
}
@@ -37,37 +37,37 @@ mod test_date {
3737

3838
#[test]
3939
fn naivedate_from_ymd() {
40-
let iso = crate::Date::YMD {
40+
let d = crate::Date::YMD {
4141
year: 2023,
4242
month: 2,
4343
day: 8,
4444
};
45-
let naive = chrono::NaiveDate::try_from(iso).unwrap();
45+
let naive = chrono::NaiveDate::try_from(d).unwrap();
4646
assert_eq!(naive.year(), 2023);
4747
assert_eq!(naive.month(), 2);
4848
assert_eq!(naive.day(), 8);
4949
}
5050

5151
#[test]
5252
fn naivedate_from_ywd() {
53-
let iso = Date::Week {
53+
let d = Date::Week {
5454
year: 2023,
5555
week: 6,
5656
day: 2,
5757
};
58-
let naive = chrono::NaiveDate::try_from(iso).unwrap();
58+
let naive = chrono::NaiveDate::try_from(d).unwrap();
5959
assert_eq!(naive.year(), 2023);
6060
assert_eq!(naive.month(), 2);
6161
assert_eq!(naive.day(), 8);
6262
}
6363

6464
#[test]
6565
fn naivedate_from_ordinal() {
66-
let iso = crate::Date::Ordinal {
66+
let d = crate::Date::Ordinal {
6767
year: 2023,
6868
day: 39,
6969
};
70-
let naive = chrono::NaiveDate::try_from(iso).unwrap();
70+
let naive = chrono::NaiveDate::try_from(d).unwrap();
7171
assert_eq!(naive.year(), 2023);
7272
assert_eq!(naive.month(), 2);
7373
assert_eq!(naive.day(), 8);
@@ -76,8 +76,8 @@ mod test_date {
7676

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

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

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

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

103-
let naive_time = chrono::NaiveTime::try_from(iso.time)?;
104-
let naive_date_time = chrono::NaiveDate::try_from(iso.date)?.and_time(naive_time);
103+
let naive_time = chrono::NaiveTime::try_from(dt.time)?;
104+
let naive_date_time = chrono::NaiveDate::try_from(dt.date)?.and_time(naive_time);
105105

106106
offset
107107
.from_local_datetime(&naive_date_time)
@@ -129,7 +129,7 @@ mod test_datetime {
129129

130130
#[test]
131131
fn datetime_from_iso_ymd_offset() {
132-
let iso = crate::DateTime {
132+
let dt = crate::DateTime {
133133
date: crate::Date::YMD {
134134
year: 2023,
135135
month: 2,
@@ -146,7 +146,7 @@ mod test_datetime {
146146
}),
147147
},
148148
};
149-
let datetime = chrono::DateTime::try_from(iso).unwrap();
149+
let datetime = chrono::DateTime::try_from(dt).unwrap();
150150

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

160160
#[test]
161161
fn datetime_from_iso_ymd_utc() {
162-
let iso = crate::DateTime {
162+
let dt = crate::DateTime {
163163
date: crate::Date::YMD {
164164
year: 2023,
165165
month: 2,
@@ -176,7 +176,7 @@ mod test_datetime {
176176
}),
177177
},
178178
};
179-
let datetime = chrono::DateTime::try_from(iso).unwrap();
179+
let datetime = chrono::DateTime::try_from(dt).unwrap();
180180

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

190190
#[test]
191191
fn datetime_from_iso_ymd_no_offset() {
192-
let iso = crate::DateTime {
192+
let dt = crate::DateTime {
193193
date: crate::Date::YMD {
194194
year: 2023,
195195
month: 2,
@@ -206,7 +206,7 @@ mod test_datetime {
206206
}),
207207
},
208208
};
209-
let datetime = chrono::DateTime::try_from(iso).unwrap();
209+
let datetime = chrono::DateTime::try_from(dt).unwrap();
210210

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

220220
#[test]
221221
fn datetime_from_iso_ywd() {
222-
let iso = crate::DateTime {
222+
let dt = crate::DateTime {
223223
date: crate::Date::Week {
224224
year: 2023,
225225
week: 6,
@@ -236,7 +236,7 @@ mod test_datetime {
236236
}),
237237
},
238238
};
239-
let datetime = chrono::DateTime::try_from(iso).unwrap();
239+
let datetime = chrono::DateTime::try_from(dt).unwrap();
240240

241241
assert_eq!(datetime.year(), 2023);
242242
assert_eq!(datetime.month(), 2);

0 commit comments

Comments
 (0)