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

ISO 8601 parsing/formatting for RelativeDuration #14

Merged
merged 14 commits into from
Apr 24, 2024
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ version = "0.2.6"
[dev-dependencies]
criterion = "0.3"
chrono-tz = "0.8.3"
proptest = "1.4.0"

[[bench]]
name = "delta"
harness = false

[[bench]]
name = "relative_duration"
harness = false
53 changes: 53 additions & 0 deletions benches/relative_duration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use chronoutil::RelativeDuration;

fn relative_duration_format_benchmark(c: &mut Criterion) {
let durations = [
"P1M",
"P1Y1M1W1DT1H1M1S",
"P99999999Y11M30DT23H59M59.999999999S",
]
.iter()
.map(|s| RelativeDuration::from_iso_8601(s).unwrap())
.collect::<Vec<RelativeDuration>>();

let mut g = c.benchmark_group("relative_duration_format");

g.bench_function("one_specifier", |b| {
b.iter(|| black_box(durations[0]).to_iso_8601())
});
g.bench_function("all_specifiers", |b| {
b.iter(|| black_box(durations[1]).to_iso_8601())
});
g.bench_function("long_specifiers", |b| {
b.iter(|| black_box(durations[2]).to_iso_8601())
});
}

fn relative_duration_parse_benchmark(c: &mut Criterion) {
let durations = [
"P1M",
"P1Y1M1W1DT1H1M1S",
"P99999999Y11M30DT23H59M59.999999999S",
];

let mut g = c.benchmark_group("relative_duration_parse");

g.bench_function("one_specifier", |b| {
b.iter(|| RelativeDuration::from_iso_8601(black_box(durations[0])))
});
g.bench_function("all_specifiers", |b| {
b.iter(|| RelativeDuration::from_iso_8601(black_box(durations[1])))
});
g.bench_function("long_specifiers", |b| {
b.iter(|| RelativeDuration::from_iso_8601(black_box(durations[2])))
});
}

criterion_group!(
benches,
relative_duration_format_benchmark,
relative_duration_parse_benchmark
);
criterion_main!(benches);
2 changes: 2 additions & 0 deletions src/relative_duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
use core::ops::{Add, Div, Mul, Neg, Sub};
use std::time::Duration as StdDuration;

use chrono::{Date, DateTime, Duration, NaiveDate, NaiveDateTime, TimeZone};

Check warning on line 5 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Check

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead

Check warning on line 5 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Test

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead

use super::delta::shift_months;

mod parse;

/// Relative time duration extending Chrono's Duration.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct RelativeDuration {
Expand Down Expand Up @@ -275,14 +277,14 @@
}
}

impl<Tz> Add<RelativeDuration> for Date<Tz>

Check warning on line 280 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Check

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead

Check warning on line 280 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Test

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead
where
Tz: TimeZone,
{
type Output = Date<Tz>;

Check warning on line 284 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Check

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead

Check warning on line 284 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Test

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead

#[inline]
fn add(self, rhs: RelativeDuration) -> Date<Tz> {

Check warning on line 287 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Check

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead

Check warning on line 287 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Test

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead
shift_months(self, rhs.months) + rhs.duration
}
}
Expand Down Expand Up @@ -317,14 +319,14 @@
}
}

impl<Tz> Sub<RelativeDuration> for Date<Tz>

Check warning on line 322 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Check

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead

Check warning on line 322 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Test

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead
where
Tz: TimeZone,
{
type Output = Date<Tz>;

Check warning on line 326 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Check

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead

Check warning on line 326 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Test

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead

#[inline]
fn sub(self, rhs: RelativeDuration) -> Date<Tz> {

Check warning on line 329 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Check

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead

Check warning on line 329 in src/relative_duration.rs

View workflow job for this annotation

GitHub Actions / Test

use of deprecated struct `chrono::Date`: Use `NaiveDate` or `DateTime<Tz>` instead
self + (-rhs)
}
}
Expand Down
Loading
Loading