now

Crates.ionow
lib.rsnow
version0.1.3
sourcesrc
created_at2019-09-25 09:04:06.380515
updated_at2022-09-27 08:23:12.500668
descriptiona time toolkit for chrono
homepage
repositoryhttps://github.com/Kilerd/now
max_upload_size
id167520
size24,708
Kilerd Chan (Kilerd)

documentation

README

now

a time toolkit for chrono inspired by jinzhu/now

GitHub Workflow Status Crates.io docs.rs Coverage Status Crates.io (recent) Crates.io

Installation

add dependency to Cargo.toml:

# Cargo.toml
[dependencies]
now = "0.1"

or, you can use cargo-edit to add dependency via:

cargo add now

Usage

Trait TimeZoneNow provide convenient and human-readable method for chrono::Timezone:

use chrono::FixedOffset;
use now::TimeZoneNow;

let offset = FixedOffset::east(60 * 60 * 8);

offset.now();                   // 2021-07-21T12:48:58.626947+08:00

offset.beginning_of_minute();   // 2021-07-21T12:48:00+08:00
offset.beginning_of_hour();     // 2021-07-21T12:00:00+08:00
offset.beginning_of_day();      // 2021-07-21T00:00:00+08:00
offset.beginning_of_week();     // 2021-07-19T00:00:00+08:00
offset.beginning_of_month();    // 2021-07-01T00:00:00+08:00
offset.beginning_of_quarter();  // 2021-07-01T00:00:00+08:00
offset.beginning_of_year();     // 2021-01-01T00:00:00+08:00

offset.end_of_minute();         // 2021-07-21T12:48:59.999999999+08:00
offset.end_of_hour();           // 2021-07-21T12:59:59.999999999+08:00
offset.end_of_day();            // 2021-07-21T23:59:59.999999999+08:00
offset.end_of_week();           // 2021-07-25T23:59:59.999999999+08:00
offset.end_of_month();          // 2021-07-31T23:59:59.999999999+08:00
offset.end_of_quarter();        // 2021-09-30T23:59:59.999999999+08:00
offset.end_of_year();           // 2021-12-31T23:59:59.999999999+08:00

And Trait DateTimeNow support those methods for chrono:DateTime<T:Timezone>:

use now::DateTimeNow;

let time = Utc::now();          // 2021-07-21T05:18:25.011480Z

time.beginning_of_minute();     // 2021-07-21T05:18:00Z
time.beginning_of_hour();       // 2021-07-21T05:00:00Z
time.beginning_of_day();        // 2021-07-21T00:00:00Z
time.beginning_of_week();       // 2021-07-19T00:00:00Z
time.beginning_of_month();      // 2021-07-01T00:00:00Z
time.beginning_of_quarter();    // 2021-07-01T00:00:00Z
time.beginning_of_year();       // 2021-01-01T00:00:00Z

time.end_of_minute();           // 2021-07-21T05:18:59.999999999Z
time.end_of_hour();             // 2021-07-21T05:59:59.999999999Z
time.end_of_day();              // 2021-07-21T23:59:59.999999999Z
time.end_of_week();             // 2021-07-25T23:59:59.999999999Z
time.end_of_month();            // 2021-07-31T23:59:59.999999999Z
time.end_of_quarter();          // 2021-09-30T23:59:59.999999999Z
time.end_of_year();             // 2021-12-31T23:59:59.999999999Z

Because now is based on chrono, so features like Leap Year and daynight time(provided by chrono-tz) are also supoprted by now.

let naive_date_time = NaiveDate::from_ymd(2024, 2, 10).and_hms(0, 0, 1);
let date_time: DateTime<Utc> = Utc.from_local_datetime(&naive_date_time).unwrap();
let time = date_time.end_of_month();

assert_eq!(29, time.day()); // 2024 is leap year
Commit count: 25

cargo fmt