chrono_utilities/
lib.rs

1//! Provides utility functions to manipulate [chrono](https://github.com/chronotope/chrono/) dates.
2//! Only [NaiveDate](https://docs.rs/chrono/0.4.11/chrono/naive/struct.NaiveDate.html) is
3//! supported as of now. Support for naive and timezone aware DateTime coming soon.
4//!
5//! The crate provides the following:
6//!
7//! **Transition APIs**
8//! Transition a chrono struct into a future or previous date using standardised methods
9//! like `start_of_pred_iso8601_week()` which provides the date on which the previous week
10//! starts. Such functions are provided for week, month and year.
11
12extern crate chrono;
13extern crate time as oldtime;
14
15pub mod naive;
16
17
18#[cfg(test)]
19mod tests {
20    use chrono::NaiveDate;
21    use crate::naive::DateTransitions;
22
23    #[test]
24    fn test_api_interface() {
25        let d1 = NaiveDate::from_ymd(1996, 2, 23);
26        // Month
27        assert_eq!(d1.end_of_month().unwrap(), NaiveDate::from_ymd(1996, 2, 29));
28        assert_eq!(d1.start_of_month().unwrap(), NaiveDate::from_ymd(1996, 2, 1));
29        assert_eq!(d1.end_of_pred_month().unwrap(), NaiveDate::from_ymd(1996, 1, 31));
30        assert_eq!(d1.start_of_pred_month().unwrap(), NaiveDate::from_ymd(1996, 1, 1));
31        assert_eq!(d1.end_of_succ_month().unwrap(), NaiveDate::from_ymd(1996, 3, 31));
32        assert_eq!(d1.start_of_succ_month().unwrap(), NaiveDate::from_ymd(1996, 3, 1));
33
34        // Year
35        assert_eq!(d1.end_of_year().unwrap(), NaiveDate::from_ymd(1996, 12, 31));
36        assert_eq!(d1.start_of_year().unwrap(), NaiveDate::from_ymd(1996, 1, 1));
37        assert_eq!(d1.end_of_pred_year().unwrap(), NaiveDate::from_ymd(1995, 12, 31));
38        assert_eq!(d1.start_of_pred_year().unwrap(), NaiveDate::from_ymd(1995, 1, 1));
39        assert_eq!(d1.end_of_succ_year().unwrap(), NaiveDate::from_ymd(1997, 12, 31));
40        assert_eq!(d1.start_of_succ_year().unwrap(), NaiveDate::from_ymd(1997, 1, 1));
41
42        // ISO 8601 Week
43        assert_eq!(d1.end_of_iso8601_week().unwrap(), NaiveDate::from_ymd(1996, 2, 25));
44        assert_eq!(d1.start_of_iso8601_week().unwrap(), NaiveDate::from_ymd(1996, 2, 19));
45        assert_eq!(d1.end_of_pred_iso8601_week().unwrap(), NaiveDate::from_ymd(1996, 2, 18));
46        assert_eq!(d1.start_of_pred_iso8601_week().unwrap(), NaiveDate::from_ymd(1996, 2, 12));
47        assert_eq!(d1.end_of_succ_iso8601_week().unwrap(), NaiveDate::from_ymd(1996, 3, 3));
48        assert_eq!(d1.start_of_succ_iso8601_week().unwrap(), NaiveDate::from_ymd(1996, 2, 26));
49
50        // Leap year
51        assert_eq!(d1.is_leap_year(), true);
52        let d2 = NaiveDate::from_ymd(1900, 7, 4);
53        assert_eq!(d2.is_leap_year(), false);
54    }
55}