5 releases

0.2.4 Nov 14, 2022
0.2.3 May 24, 2022
0.2.1 Jan 24, 2021
0.2.0 Oct 17, 2020
0.1.0 Jun 20, 2020

#112 in Date and time

Download history 1332/week @ 2024-07-21 2160/week @ 2024-07-28 1602/week @ 2024-08-04 2172/week @ 2024-08-11 1821/week @ 2024-08-18 1855/week @ 2024-08-25 2256/week @ 2024-09-01 1040/week @ 2024-09-08 1231/week @ 2024-09-15 1627/week @ 2024-09-22 1433/week @ 2024-09-29 1253/week @ 2024-10-06 1148/week @ 2024-10-13 1064/week @ 2024-10-20 1347/week @ 2024-10-27 955/week @ 2024-11-03

4,544 downloads per month
Used in 11 crates (3 directly)

BSD-2-Clause

71KB
1.5K SLoC

gregorian docs tests

An implementation of the proleptic Gregorian calendar, compatible with ISO 8601. Amongst others, that means that the calendar has a year zero preceeding the year 1.

This create does not deal with times or time zones.

The Date type represents a date (year, month and day), the Year type represents a calendar year, the Month type represents a calendar month, and the YearMonth type represents a month of a specific year.

Where possible, things are implemented as const fn. Currently, this excludes trait implementations and functions that rely on traits.

Example

use gregorian::{Date, Month::*, Year, YearMonth};

assert!(Year::new(2020).has_leap_day(), true);
assert!(YearMonth::new(1900, February).total_days() == 28);
assert!(YearMonth::new(2000, February).total_days() == 29);

assert!(Year::new(2020).with_month(March).first_day() == Date::new(2020, March, 1).unwrap());
assert!(Year::new(2020).with_month(March).last_day() == Date::new(2020, March, 31).unwrap());

assert!(Year::new(2020).first_day() == Date::new(2020, 1, 1).unwrap());
assert!(Year::new(2020).last_day() == Date::new(2020, 12, 31).unwrap());

assert!(Date::new(2020, 2, 1).unwrap().day_of_year() == 32);

Rounding invalid dates

When you use Date::add_years() or Date::add_months(), you can get invalid dates. These are reported with an InvalidDayOfMonth error which has the next_valid() and prev_valid() methods. Those can be used to get the next or previous valid date instead.

Additionally, there is an extension trait for Result<Date, InvalidDayOfMonth> with the or_next_valid() and or_prev_valid() methods. This allows you to directly round the date on the Result object.

use gregorian::{Date, DateResultExt};
let date = Date::new(2020, 1, 31).unwrap();
assert!(date.add_months(1).or_next_valid() == Date::new(2020, 3, 1).unwrap());
assert!(date.add_months(1).or_prev_valid() == Date::new(2020, 2, 29).unwrap());

Dependencies

~175KB