Crate chinese_holiday

Crate chinese_holiday 

Source
Expand description

§The Chinese Holiday Library

§English Version

chinese_holiday is a Rust library for determining Chinese holidays. It can determine whether a given date is a holiday, and the type of holiday.

§Features

  • chrono-compatible: Enable this feature to make the library compatible with the chrono crate. This feature is enabled by default.

§Example

fn main() {
    use chinese_holiday::*;

    assert_eq!(
        chinese_holiday(Ymd::new(2004, 1, 1)),
        DayKind::NewYearsDayHoliday
    );
    assert!(chinese_holiday(Ymd::new(2004, 1, 1)).is_holiday());

    assert_eq!(
        chinese_holiday(Ymd::new(2004, 5, 8)),
        DayKind::InternationalWorkersDayWorkday
    );
    assert!(chinese_holiday(Ymd::new(2004, 5, 8)).is_workday());
}

The library is compatible with the chrono, for example:

#[allow(deprecated)]
fn main() {
   use chinese_holiday::*;
   use chrono::*;
   use chrono_tz::Asia::Shanghai;

   let date = NaiveDate::from_ymd_opt(2004, 1, 1).unwrap();
   assert_eq!(chinese_holiday(&date), DayKind::NewYearsDayHoliday);
   let datetime = date.and_hms_opt(0, 0, 0).unwrap();
   assert_eq!(chinese_holiday(&datetime), DayKind::NewYearsDayHoliday);

   let date = Shanghai.ymd(2004, 5, 8);
   assert_eq!(
       chinese_holiday(&date),
       DayKind::InternationalWorkersDayWorkday
   );
   let datetime = date.and_hms_opt(0, 0, 0).unwrap();
   assert_eq!(
       chinese_holiday(&datetime),
       DayKind::InternationalWorkersDayWorkday
   );
}

§中文版说明

chinese_holiday 是一个用于判断中国节假日的 Rust 库,可以判断给定日期是否是节假日, 以及节假日的类型。

§Features

  • chrono-compatible: 启用此特性可以使此库与 chrono crate 兼容。此特性默认启用。

§示例

fn main() {
    use chinese_holiday::*;

    assert_eq!(
        chinese_holiday(Ymd::new(2004, 1, 1)),
        DayKind::NewYearsDayHoliday
    );
    assert!(chinese_holiday(Ymd::new(2004, 1, 1)).is_holiday());

    assert_eq!(
        chinese_holiday(Ymd::new(2004, 5, 8)),
        DayKind::InternationalWorkersDayWorkday
    );
    assert!(chinese_holiday(Ymd::new(2004, 5, 8)).is_workday());
}

本库与 chrono crate 兼容,例如:

#[allow(deprecated)]
fn main() {
   use chinese_holiday::*;
   use chrono::*;
   use chrono_tz::Asia::Shanghai;

   let date = NaiveDate::from_ymd_opt(2004, 1, 1).unwrap();
   assert_eq!(chinese_holiday(&date), DayKind::NewYearsDayHoliday);
   let datetime = date.and_hms_opt(0, 0, 0).unwrap();
   assert_eq!(chinese_holiday(&datetime), DayKind::NewYearsDayHoliday);

   let date = Shanghai.ymd(2004, 5, 8);
   assert_eq!(
       chinese_holiday(&date),
       DayKind::InternationalWorkersDayWorkday
   );
   let datetime = date.and_hms_opt(0, 0, 0).unwrap();
   assert_eq!(
       chinese_holiday(&datetime),
       DayKind::InternationalWorkersDayWorkday
   );
}

Structs§

Ymd
Ymd is a packed representation of a year, month and day. It uses a u16 to store the year, month and day, which can represent dates between 2000-01-01 and 2127-12-31.

Enums§

DayKind
English Version

Functions§

chinese_holiday
Return the DayKind of the given date. According to DayKind, you can determine whether this day is a workday or a holiday.