Crates.io | chronounit |
lib.rs | chronounit |
version | 0.3.0 |
source | src |
created_at | 2024-03-03 09:27:25.912273 |
updated_at | 2024-03-12 15:38:01.396417 |
description | A date-time formatter and time-unit library for Rust time |
homepage | https://github.com/photowey/chrono-unit |
repository | https://github.com/photowey/chrono-unit |
max_upload_size | |
id | 1160492 |
size | 139,016 |
chrono-unit
A date-time
formatter
and time-unit
library for Rust
time
Usage
Add this to your Cargo.toml
:
[dependencies]
chronounit = "0.3"
# If necessary
chrono = "0.4"
APIs
TimeUnit
functions
to_nanos
TimeUnit::Nanoseconds.to_nanos(1024)
to_micros
to_millis
to_seconds
to_minutes
to_hours
to_days
value
assert_eq!(TimeUnit::Seconds.value(), "Seconds");
value_of
assert_eq!(TimeUnit::value_of("Seconds"), Some(TimeUnit::Seconds))
insensitive_case_value_of
assert_eq!(TimeUnit::insensitive_case_value_of("Seconds"), Some(TimeUnit::Seconds));
assert_eq!(TimeUnit::insensitive_case_value_of("SECONDS"), Some(TimeUnit::Seconds));
assert_eq!(TimeUnit::insensitive_case_value_of("seconds"), Some(TimeUnit::Seconds));
Formatter
Builtin
let dtf = DefaultDateTimeFormatter::builtin();
// default pattern: DateTimePattern::YyyyMmDdHhMmSs
new
let dtf = DefaultDateTimeFormatter::new(DateTimePattern::YyyyMmDd);
of_pattern
let dtf = DefaultDateTimeFormatter::new(DateTimePattern::YyyyMmDd);
let dtf = dtf.of_pattern(DateTimePattern::YyyyMmDdHhMmSs);
Format
format_date_time_utc_default
DateTime<Utc>
- Default pattern
let now = "2024-03-01 02:03:04";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
let datetime_utc: DateTime<Utc> = Utc.from_utc_datetime( & ndt);
let dtf = DefaultDateTimeFormatter::new(DateTimePattern::YyyyMmDd);
assert_eq!(
dtf.format_date_time_utc_default(&datetime_utc),
"2024-03-01"
);
format_date_time_utc
let now = "2024-03-01 02:03:04";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
let datetime_utc: DateTime<Utc> = Utc.from_utc_datetime( & ndt);
let dtf = DefaultDateTimeFormatter::new(DateTimePattern::YyyyMmDd);
assert_eq!(
dtf.format_date_time_utc(&datetime_utc, DateTimePattern::YyyyMmDdHhMmSs),
"2024-03-01 02:03:04"
);
format_naive_date_time_utc_default
with timezone
let now = "2024-03-01 02:03:04";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
let dtf = DefaultDateTimeFormatter::new(DateTimePattern::YyyyMmDd);
assert_eq!(
dtf.format_naive_date_time_utc_default(&ndt),
"2024-03-01"
);
format_naive_date_time_utc
with timezone
let now = "2024-03-01 02:03:04";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
let dtf = DefaultDateTimeFormatter::new(DateTimePattern::YyyyMmDd);
assert_eq!(
dtf.format_naive_date_time_utc(&ndt, DateTimePattern::YyyyMmDd),
"2024-03-01"
);
format_naive_date_time_default
let now = "2024-03-01 02:03:04";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
let dtf = DefaultDateTimeFormatter::new(DateTimePattern::YyyyMmDdHhMmSs);
assert_eq!(
dtf.format_naive_date_time_default(&ndt),
"2024-03-01 02:03:04"
);
format_naive_date_time
let now = "2024-03-01 02:03:04";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
let dtf = DefaultDateTimeFormatter::new(DateTimePattern::YyyyMmDdHhMmSs);
assert_eq!(
dtf.format_naive_date_time(&ndt, DateTimePattern::YyyyMmDdHhMmSs),
"2024-03-01 02:03:04"
);
Function
format_date_time_utc_default
let now = "2024-03-12 22:55:00";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
let datetime_utc: DateTime<Utc> = Utc.from_utc_datetime( & ndt);
assert_eq!(
formatter::format_date_time_utc_default(&datetime_utc),
"2024-03-12 22:55:00"
);
format_naive_date_time_utc_default
let now = "2024-03-12 22:55:00";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
assert_eq!(
formatter::format_naive_date_time_utc_default(&ndt),
"2024-03-12 22:55:00"
);
format_naive_date_time_default
let now = "2024-03-12 22:55:00";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
assert_eq!(
formatter::format_naive_date_time_utc_default(&ndt),
"2024-03-12 22:55:00"
);
format_date_time_utc
let now = "2024-03-12 22:55:00";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
let datetime_utc: DateTime<Utc> = Utc.from_utc_datetime( & ndt);
assert_eq!(
formatter::format_date_time_utc(&datetime_utc, DateTimePattern::YyyyMmDd),
"2024-03-12"
);
assert_eq!(
formatter::format_date_time_utc(&datetime_utc, DateTimePattern::YyyyMmDdHhMmSs),
"2024-03-12 22:55:00"
);
assert_eq!(
formatter::format_date_time_utc(&datetime_utc, DateTimePattern::HhMmSs),
"22:55:00"
);
format_naive_date_time_utc
let now = "2024-03-12 22:55:00";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
assert_eq!(
formatter::format_naive_date_time_utc(&ndt, DateTimePattern::YyyyMmDd),
"2024-03-12"
);
assert_eq!(
formatter::format_naive_date_time_utc(&ndt, DateTimePattern::YyyyMmDdHhMmSs),
"2024-03-12 22:55:00"
);
assert_eq!(
formatter::format_naive_date_time_utc(&ndt, DateTimePattern::HhMmSs),
"22:55:00"
);
format_naive_date_time
let now = "2024-03-12 22:55:00";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
assert_eq!(
formatter::format_naive_date_time(&ndt, DateTimePattern::YyyyMmDd),
"2024-03-12"
);
assert_eq!(
formatter::format_naive_date_time(&ndt, DateTimePattern::YyyyMmDdHhMmSs),
"2024-03-12 22:55:00"
);
assert_eq!(
formatter::format_naive_date_time(&ndt, DateTimePattern::HhMmSs),
"22:55:00"
);
Macro
format_date_time_utc_default
let now = "2024-03-12 22:55:00";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
let datetime_utc: DateTime<Utc> = Utc.from_utc_datetime( & ndt);
assert_eq!(
format_date_time_utc_default!(&datetime_utc),
"2024-03-12 22:55:00"
);
format_naive_date_time_utc_default
let now = "2024-03-12 22:55:00";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
assert_eq!(
format_naive_date_time_utc_default!(&ndt),
"2024-03-12 22:55:00"
);
format_naive_date_time_default
let now = "2024-03-12 22:55:00";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
assert_eq!(
format_naive_date_time_utc_default!(&ndt),
"2024-03-12 22:55:00"
);
format_date_time_utc
let now = "2024-03-12 22:55:00";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
let datetime_utc: DateTime<Utc> = Utc.from_utc_datetime( & ndt);
assert_eq!(
format_date_time_utc!(&datetime_utc, DateTimePattern::YyyyMmDd),
"2024-03-12"
);
assert_eq!(
format_date_time_utc!(&datetime_utc, DateTimePattern::YyyyMmDdHhMmSs),
"2024-03-12 22:55:00"
);
assert_eq!(
format_date_time_utc!(&datetime_utc, DateTimePattern::HhMmSs),
"22:55:00"
);
format_naive_date_time_utc
let now = "2024-03-12 22:55:00";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
assert_eq!(
format_naive_date_time_utc!(&ndt, DateTimePattern::YyyyMmDd),
"2024-03-12"
);
assert_eq!(
format_naive_date_time_utc!(&ndt, DateTimePattern::YyyyMmDdHhMmSs),
"2024-03-12 22:55:00"
);
assert_eq!(
format_naive_date_time_utc!(&ndt, DateTimePattern::HhMmSs),
"22:55:00"
);
format_naive_date_time
let now = "2024-03-12 22:55:00";
let ndt = NaiveDateTime::parse_from_str(now, "%Y-%m-%d %H:%M:%S").expect("Parse error");
assert_eq!(
format_naive_date_time!(&ndt, DateTimePattern::YyyyMmDd),
"2024-03-12"
);
assert_eq!(
format_naive_date_time!(&ndt, DateTimePattern::YyyyMmDdHhMmSs),
"2024-03-12 22:55:00"
);
assert_eq!(
format_naive_date_time!(&ndt, DateTimePattern::HhMmSs),
"22:55:00"
);
Sleep
to_duration
assert_eq!(TimeUnit::Nanoseconds.to_duration(100), Duration::from_nanos(100));
to_chrono_duration
assert_eq!(TimeUnit::Nanoseconds.to_chrono_duration(1024), chrono::Duration::nanoseconds(1024));
sleep
let start = std::time::Instant::now();
TimeUnit::Milliseconds.sleep(1024);
let duration = start.elapsed();
assert!(duration >= Duration::from_millis(1024));
closure_sleep
let start = std::time::Instant::now();
TimeUnit::Milliseconds.closure_sleep(1024, | x| {
assert_eq ! (x, Duration::from_millis(1024));
thread::sleep(x);
});
let duration = start.elapsed();
assert!(duration >= Duration::from_millis(1024));
closure_chrono_sleep
let start = std::time::Instant::now();
TimeUnit::Milliseconds.closure_chrono_sleep(1024, | x| {
assert_eq ! (x, chrono::Duration::milliseconds(1024));
thread::sleep(Duration::from_millis(x.num_milliseconds() as u64));
});
let duration = start.elapsed();
assert!(duration >= Duration::from_millis(1024));