4 releases

0.1.3 Sep 27, 2022
0.1.2 Jul 21, 2021
0.1.1 Jul 21, 2021
0.1.0 Sep 25, 2019

#77 in Date and time

Download history 39857/week @ 2024-07-23 42035/week @ 2024-07-30 31781/week @ 2024-08-06 41536/week @ 2024-08-13 48859/week @ 2024-08-20 56342/week @ 2024-08-27 54555/week @ 2024-09-03 43459/week @ 2024-09-10 34484/week @ 2024-09-17 41079/week @ 2024-09-24 39016/week @ 2024-10-01 39776/week @ 2024-10-08 44769/week @ 2024-10-15 46108/week @ 2024-10-22 43376/week @ 2024-10-29 38284/week @ 2024-11-05

178,819 downloads per month
Used in 136 crates (4 directly)

MIT license

21KB
373 lines

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

Dependencies

~1–6.5MB
~26K SLoC