Skip to content

Commit

Permalink
feat: Add next_date and prev_date functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nakedible committed Oct 31, 2023
1 parent e351223 commit 7df2164
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 52 deletions.
6 changes: 6 additions & 0 deletions benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ fn bench_basic(c: &mut Criterion) {
c.bench_function("date_to_weekday", |b| {
b.iter_custom(bencher(rand_date, |d| datealgo::date_to_weekday(black_box(d))))
});
c.bench_function("next_date", |b| {
b.iter_custom(bencher(rand_date, |d| datealgo::next_date(black_box(d))))
});
c.bench_function("prev_date", |b| {
b.iter_custom(bencher(rand_date, |d| datealgo::prev_date(black_box(d))))
});
c.bench_function("secs_to_dhms", |b| {
b.iter_custom(bencher(rand_secs, |s| datealgo::secs_to_dhms(black_box(s))))
});
Expand Down
80 changes: 79 additions & 1 deletion benches/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ mod datealgo_alt {
let mm = secs / 60 % 60;
let hh = secs / 3600;
let days = (days as i32).wrapping_sub(DAY_OFFSET);
(days, hh as u8, mm as u8, ss as u8)
(days, hh as u8, mm as u8, ss as u8)
}

#[inline]
Expand Down Expand Up @@ -143,6 +143,18 @@ mod datealgo_alt {
((day as u32 + (13 * mm - 1) / 5 + yy + yy / 4 - yy / 100 + yy / 400 + 6) % 7 + 1) as u8
}

#[inline]
pub const fn next_date((y, m, d): (i32, u8, u8)) -> (i32, u8, u8) {
let rd = datealgo::date_to_rd((y, m, d));
datealgo::rd_to_date(rd + 1)
}

#[inline]
pub const fn prev_date((y, m, d): (i32, u8, u8)) -> (i32, u8, u8) {
let rd = datealgo::date_to_rd((y, m, d));
datealgo::rd_to_date(rd - 1)
}

#[inline]
pub const fn is_leap_year(y: i32) -> bool {
y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)
Expand Down Expand Up @@ -630,6 +642,21 @@ mod chrono {
use chrono::{Datelike, Timelike};
use std::time::SystemTime;

pub fn rand_date() -> chrono::NaiveDate {
let (y, m, d) = super::rand_date();
chrono::NaiveDate::from_ymd_opt(y, m as u32, d as u32).unwrap()
}

#[inline]
pub fn next_date(d: chrono::NaiveDate) -> chrono::NaiveDate {
d.succ_opt().unwrap()
}

#[inline]
pub fn prev_date(d: chrono::NaiveDate) -> chrono::NaiveDate {
d.pred_opt().unwrap()
}

#[inline]
pub fn rd_to_date(n: i32) -> (i32, u8, u8) {
let date = chrono::NaiveDate::from_num_days_from_ce_opt(n + 719162).unwrap();
Expand Down Expand Up @@ -688,6 +715,21 @@ mod time {

const UNIX_EPOCH_JULIAN_DAY: i32 = 2440588;

pub fn rand_date() -> time::Date {
let (y, m, d) = super::rand_date();
time::Date::from_calendar_date(y, m.try_into().unwrap(), d).unwrap()
}

#[inline]
pub fn next_date(d: time::Date) -> time::Date {
d.next_day().unwrap()
}

#[inline]
pub fn prev_date(d: time::Date) -> time::Date {
d.previous_day().unwrap()
}

#[inline]
pub fn rd_to_date(n: i32) -> (i32, u8, u8) {
let date = time::Date::from_julian_day(n + UNIX_EPOCH_JULIAN_DAY).unwrap();
Expand Down Expand Up @@ -862,6 +904,40 @@ fn bench_date_to_weekday(c: &mut Criterion) {
group.finish();
}

fn bench_next_date(c: &mut Criterion) {
let mut group = c.benchmark_group("compare_next_date");
group.bench_function("datealgo", |b| {
b.iter_custom(bencher(rand_date, |d| datealgo::next_date(black_box(d))))
});
group.bench_function("datealgo_alt", |b| {
b.iter_custom(bencher(rand_date, |d| datealgo_alt::next_date(black_box(d))))
});
group.bench_function("chrono", |b| {
b.iter_custom(bencher(chrono::rand_date, |d| chrono::next_date(black_box(d))))
});
group.bench_function("time", |b| {
b.iter_custom(bencher(time::rand_date, |d| time::next_date(black_box(d))))
});
group.finish();
}

fn bench_prev_date(c: &mut Criterion) {
let mut group = c.benchmark_group("compare_prev_date");
group.bench_function("datealgo", |b| {
b.iter_custom(bencher(rand_date, |d| datealgo::prev_date(black_box(d))))
});
group.bench_function("datealgo_alt", |b| {
b.iter_custom(bencher(rand_date, |d| datealgo_alt::prev_date(black_box(d))))
});
group.bench_function("chrono", |b| {
b.iter_custom(bencher(chrono::rand_date, |d| chrono::prev_date(black_box(d))))
});
group.bench_function("time", |b| {
b.iter_custom(bencher(time::rand_date, |d| time::prev_date(black_box(d))))
});
group.finish();
}

fn bench_secs_to_dhms(c: &mut Criterion) {
let mut group = c.benchmark_group("compare_secs_to_dhms");
group.bench_function("datealgo", |b| {
Expand Down Expand Up @@ -1029,6 +1105,8 @@ criterion_group!(
bench_date_to_rd,
bench_rd_to_weekday,
bench_date_to_weekday,
bench_next_date,
bench_prev_date,
bench_secs_to_dhms,
bench_dhms_to_secs,
bench_secs_to_datetime,
Expand Down
12 changes: 12 additions & 0 deletions benches/iai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ fn iai_date_to_weekday() -> u8 {
datealgo::date_to_weekday(black_box((2023, 5, 12)))
}

#[inline(never)]
fn iai_next_date() -> (i32, u8, u8) {
datealgo::next_date(black_box((2023, 5, 12)))
}

#[inline(never)]
fn iai_prev_date() -> (i32, u8, u8) {
datealgo::prev_date(black_box((2023, 5, 12)))
}

#[inline(never)]
fn iai_secs_to_dhms() -> (i32, u8, u8, u8) {
datealgo::secs_to_dhms(black_box(1684574678i64))
Expand Down Expand Up @@ -76,6 +86,8 @@ main!(
iai_date_to_rd,
iai_rd_to_weekday,
iai_date_to_weekday,
iai_next_date,
iai_prev_date,
iai_secs_to_dhms,
iai_dhms_to_secs,
iai_secs_to_datetime,
Expand Down
Loading

0 comments on commit 7df2164

Please sign in to comment.