#default #default-value #date-time #chrono

datetime-default

Provide NewType of Datetime that overrides the default value

6 releases (stable)

1.1.1 Feb 26, 2023
1.1.0 Oct 25, 2022
1.0.1 Oct 11, 2022
0.1.1 Oct 10, 2022
0.1.0 Oct 10, 2022

#222 in Date and time

21 downloads per month

MIT license

34KB
909 lines

datetime-default

DateTime NewType set for default time

Latest Version GitHub license

When you want to use #[derive(Default)] to a struct, you may want the current time to be the default for DateTime. For this purpose, we have provided NewType that overrides the default value.

Features

DateTimeDefaultNow

DateTime with current time as default.

use chrono::{Local, TimeZone};
use datetime_default::DateTimeDefaultNow;

assert_eq!(
    DateTimeDefaultNow::<Local>::default(),
    Local.datetime_from_str("2022/10/10 23:40:11.695164300", "%Y/%m/%d %H:%M:%S%.9f").unwrap()
);

DateTimeDefaultUnix

DateTime with UNIX epoch as default.

use chrono::{Utc, TimeZone};
use datetime_default::DateTimeDefaultUnix;

assert_eq!(
    DateTimeDefaultUnix::<Utc>::default(),
    Utc.datetime_from_str("1970/1/1 00:00:00", "%Y/%m/%d %H:%M:%S").unwrap()
);

Default Datetime with TimeZone

use chrono::{DateTime, FixedOffset, TimeZone};
use datetime_default::DateTimeDefaultUnix;

assert_eq!(
    DateTimeDefaultUnix::<FixedOffset, 9>::default(),
    DateTime::parse_from_rfc3339("1970-01-01T09:00:00+09:00").unwrap()
);

License: MIT

Dependencies

~1–1.5MB
~25K SLoC