Skip to main content

JPHoliday

Struct JPHoliday 

Source
pub struct JPHoliday { /* private fields */ }
Expand description

日本の祝日を判定するクラス。

判定結果は日付単位でキャッシュされます。 キャッシュは問い合わせた日付ごとに増えるため、多数の日付を問い合わせる場合はインスタンスを drop することでまとめて解放できます(グローバル関数 API はキャッシュを持ちません)。

§Examples

use jpholiday::{Date, JPHoliday};

let jp = JPHoliday::new();
assert!(jp.is_holiday(Date::new(2017, 1, 1).unwrap()));
assert_eq!(
    jp.is_holiday_name(Date::new(2017, 1, 2).unwrap()).as_deref(),
    Some("元日 振替休日")
);

Implementations§

Source§

impl JPHoliday

Source

pub fn new() -> Self

既定のチェッカーを備えた新しいインスタンスを生成します。

Examples found in repository?
examples/basic.rs (line 36)
7fn main() {
8    // --- 関数 API ---
9    let new_year = Date::new(2017, 1, 1).unwrap();
10    println!("{new_year} は祝日? {}", jpholiday::is_holiday(new_year));
11    println!(
12        "{} の祝日名: {:?}",
13        Date::new(2017, 1, 2).unwrap(),
14        jpholiday::is_holiday_name(Date::new(2017, 1, 2).unwrap())
15    );
16
17    println!("\n2017 年の祝日一覧:");
18    for (date, name) in jpholiday::year_holidays(2017) {
19        println!("  {date}  {name}");
20    }
21
22    println!("\n2017 年 5 月の祝日:");
23    for (date, name) in jpholiday::month_holidays(2017, 5) {
24        println!("  {date}  {name}");
25    }
26
27    println!("\nゴールデンウィーク (2017-05-01 〜 2017-05-05):");
28    for (date, name) in jpholiday::between(
29        Date::new(2017, 5, 1).unwrap(),
30        Date::new(2017, 5, 5).unwrap(),
31    ) {
32        println!("  {date}  {name}");
33    }
34
35    // --- クラス API ---
36    let jp = JPHoliday::new();
37    let holidays = jp.holidays(Date::new(2020, 1, 1).unwrap());
38    println!("\nクラス API: {:?}", holidays);
39
40    // --- 独自祝日 ---
41    struct CompanyHoliday;
42    impl OriginalHolidayChecker for CompanyHoliday {
43        fn is_holiday(&self, date: Date) -> bool {
44            date == Date::new(2020, 2, 9).unwrap()
45        }
46        fn holiday_name(&self, _date: Date) -> String {
47            "特別休暇".to_string()
48        }
49    }
50
51    let mut jp = JPHoliday::new();
52    jp.register(CompanyHoliday);
53    let special = Date::new(2020, 2, 9).unwrap();
54    println!(
55        "\n独自祝日を登録: {} -> {:?}",
56        special,
57        jp.is_holiday_name(special)
58    );
59    jp.unregister::<CompanyHoliday>();
60    println!(
61        "登録解除後: {} -> {:?}",
62        special,
63        jp.is_holiday_name(special)
64    );
65}
Source

pub fn holidays(&self, date: Date) -> Vec<Holiday>

その日に該当するすべての祝日を返します。

Examples found in repository?
examples/basic.rs (line 37)
7fn main() {
8    // --- 関数 API ---
9    let new_year = Date::new(2017, 1, 1).unwrap();
10    println!("{new_year} は祝日? {}", jpholiday::is_holiday(new_year));
11    println!(
12        "{} の祝日名: {:?}",
13        Date::new(2017, 1, 2).unwrap(),
14        jpholiday::is_holiday_name(Date::new(2017, 1, 2).unwrap())
15    );
16
17    println!("\n2017 年の祝日一覧:");
18    for (date, name) in jpholiday::year_holidays(2017) {
19        println!("  {date}  {name}");
20    }
21
22    println!("\n2017 年 5 月の祝日:");
23    for (date, name) in jpholiday::month_holidays(2017, 5) {
24        println!("  {date}  {name}");
25    }
26
27    println!("\nゴールデンウィーク (2017-05-01 〜 2017-05-05):");
28    for (date, name) in jpholiday::between(
29        Date::new(2017, 5, 1).unwrap(),
30        Date::new(2017, 5, 5).unwrap(),
31    ) {
32        println!("  {date}  {name}");
33    }
34
35    // --- クラス API ---
36    let jp = JPHoliday::new();
37    let holidays = jp.holidays(Date::new(2020, 1, 1).unwrap());
38    println!("\nクラス API: {:?}", holidays);
39
40    // --- 独自祝日 ---
41    struct CompanyHoliday;
42    impl OriginalHolidayChecker for CompanyHoliday {
43        fn is_holiday(&self, date: Date) -> bool {
44            date == Date::new(2020, 2, 9).unwrap()
45        }
46        fn holiday_name(&self, _date: Date) -> String {
47            "特別休暇".to_string()
48        }
49    }
50
51    let mut jp = JPHoliday::new();
52    jp.register(CompanyHoliday);
53    let special = Date::new(2020, 2, 9).unwrap();
54    println!(
55        "\n独自祝日を登録: {} -> {:?}",
56        special,
57        jp.is_holiday_name(special)
58    );
59    jp.unregister::<CompanyHoliday>();
60    println!(
61        "登録解除後: {} -> {:?}",
62        special,
63        jp.is_holiday_name(special)
64    );
65}
Source

pub fn is_holiday(&self, date: Date) -> bool

その日が祝日かどうかを返します。

Source

pub fn is_holiday_name(&self, date: Date) -> Option<String>

その日の祝日名を返します(複数該当する場合は先頭、該当しなければ None)。

Examples found in repository?
examples/basic.rs (line 57)
7fn main() {
8    // --- 関数 API ---
9    let new_year = Date::new(2017, 1, 1).unwrap();
10    println!("{new_year} は祝日? {}", jpholiday::is_holiday(new_year));
11    println!(
12        "{} の祝日名: {:?}",
13        Date::new(2017, 1, 2).unwrap(),
14        jpholiday::is_holiday_name(Date::new(2017, 1, 2).unwrap())
15    );
16
17    println!("\n2017 年の祝日一覧:");
18    for (date, name) in jpholiday::year_holidays(2017) {
19        println!("  {date}  {name}");
20    }
21
22    println!("\n2017 年 5 月の祝日:");
23    for (date, name) in jpholiday::month_holidays(2017, 5) {
24        println!("  {date}  {name}");
25    }
26
27    println!("\nゴールデンウィーク (2017-05-01 〜 2017-05-05):");
28    for (date, name) in jpholiday::between(
29        Date::new(2017, 5, 1).unwrap(),
30        Date::new(2017, 5, 5).unwrap(),
31    ) {
32        println!("  {date}  {name}");
33    }
34
35    // --- クラス API ---
36    let jp = JPHoliday::new();
37    let holidays = jp.holidays(Date::new(2020, 1, 1).unwrap());
38    println!("\nクラス API: {:?}", holidays);
39
40    // --- 独自祝日 ---
41    struct CompanyHoliday;
42    impl OriginalHolidayChecker for CompanyHoliday {
43        fn is_holiday(&self, date: Date) -> bool {
44            date == Date::new(2020, 2, 9).unwrap()
45        }
46        fn holiday_name(&self, _date: Date) -> String {
47            "特別休暇".to_string()
48        }
49    }
50
51    let mut jp = JPHoliday::new();
52    jp.register(CompanyHoliday);
53    let special = Date::new(2020, 2, 9).unwrap();
54    println!(
55        "\n独自祝日を登録: {} -> {:?}",
56        special,
57        jp.is_holiday_name(special)
58    );
59    jp.unregister::<CompanyHoliday>();
60    println!(
61        "登録解除後: {} -> {:?}",
62        special,
63        jp.is_holiday_name(special)
64    );
65}
Source

pub fn year_holidays(&self, year: i32) -> Vec<Holiday>

その年のすべての祝日を返します。

Source

pub fn month_holidays(&self, year: i32, month: u32) -> Vec<Holiday>

その月のすべての祝日を返します。month が範囲外なら空を返します。

Source

pub fn between(&self, start: Date, end: Date) -> Vec<Holiday>

指定範囲(両端を含む)のすべての祝日を返します。

Source

pub fn register<C: OriginalHolidayChecker + 'static>(&mut self, checker: C)

独自の祝日チェッカーを登録します。

同一型のチェッカーが既に登録されている場合は何もしません。

Examples found in repository?
examples/basic.rs (line 52)
7fn main() {
8    // --- 関数 API ---
9    let new_year = Date::new(2017, 1, 1).unwrap();
10    println!("{new_year} は祝日? {}", jpholiday::is_holiday(new_year));
11    println!(
12        "{} の祝日名: {:?}",
13        Date::new(2017, 1, 2).unwrap(),
14        jpholiday::is_holiday_name(Date::new(2017, 1, 2).unwrap())
15    );
16
17    println!("\n2017 年の祝日一覧:");
18    for (date, name) in jpholiday::year_holidays(2017) {
19        println!("  {date}  {name}");
20    }
21
22    println!("\n2017 年 5 月の祝日:");
23    for (date, name) in jpholiday::month_holidays(2017, 5) {
24        println!("  {date}  {name}");
25    }
26
27    println!("\nゴールデンウィーク (2017-05-01 〜 2017-05-05):");
28    for (date, name) in jpholiday::between(
29        Date::new(2017, 5, 1).unwrap(),
30        Date::new(2017, 5, 5).unwrap(),
31    ) {
32        println!("  {date}  {name}");
33    }
34
35    // --- クラス API ---
36    let jp = JPHoliday::new();
37    let holidays = jp.holidays(Date::new(2020, 1, 1).unwrap());
38    println!("\nクラス API: {:?}", holidays);
39
40    // --- 独自祝日 ---
41    struct CompanyHoliday;
42    impl OriginalHolidayChecker for CompanyHoliday {
43        fn is_holiday(&self, date: Date) -> bool {
44            date == Date::new(2020, 2, 9).unwrap()
45        }
46        fn holiday_name(&self, _date: Date) -> String {
47            "特別休暇".to_string()
48        }
49    }
50
51    let mut jp = JPHoliday::new();
52    jp.register(CompanyHoliday);
53    let special = Date::new(2020, 2, 9).unwrap();
54    println!(
55        "\n独自祝日を登録: {} -> {:?}",
56        special,
57        jp.is_holiday_name(special)
58    );
59    jp.unregister::<CompanyHoliday>();
60    println!(
61        "登録解除後: {} -> {:?}",
62        special,
63        jp.is_holiday_name(special)
64    );
65}
Source

pub fn unregister<C: OriginalHolidayChecker + 'static>(&mut self)

指定型の独自祝日チェッカーを登録解除します。

登録解除は型パラメータで指定します(例: jp.unregister::<MyHoliday>())。

Examples found in repository?
examples/basic.rs (line 59)
7fn main() {
8    // --- 関数 API ---
9    let new_year = Date::new(2017, 1, 1).unwrap();
10    println!("{new_year} は祝日? {}", jpholiday::is_holiday(new_year));
11    println!(
12        "{} の祝日名: {:?}",
13        Date::new(2017, 1, 2).unwrap(),
14        jpholiday::is_holiday_name(Date::new(2017, 1, 2).unwrap())
15    );
16
17    println!("\n2017 年の祝日一覧:");
18    for (date, name) in jpholiday::year_holidays(2017) {
19        println!("  {date}  {name}");
20    }
21
22    println!("\n2017 年 5 月の祝日:");
23    for (date, name) in jpholiday::month_holidays(2017, 5) {
24        println!("  {date}  {name}");
25    }
26
27    println!("\nゴールデンウィーク (2017-05-01 〜 2017-05-05):");
28    for (date, name) in jpholiday::between(
29        Date::new(2017, 5, 1).unwrap(),
30        Date::new(2017, 5, 5).unwrap(),
31    ) {
32        println!("  {date}  {name}");
33    }
34
35    // --- クラス API ---
36    let jp = JPHoliday::new();
37    let holidays = jp.holidays(Date::new(2020, 1, 1).unwrap());
38    println!("\nクラス API: {:?}", holidays);
39
40    // --- 独自祝日 ---
41    struct CompanyHoliday;
42    impl OriginalHolidayChecker for CompanyHoliday {
43        fn is_holiday(&self, date: Date) -> bool {
44            date == Date::new(2020, 2, 9).unwrap()
45        }
46        fn holiday_name(&self, _date: Date) -> String {
47            "特別休暇".to_string()
48        }
49    }
50
51    let mut jp = JPHoliday::new();
52    jp.register(CompanyHoliday);
53    let special = Date::new(2020, 2, 9).unwrap();
54    println!(
55        "\n独自祝日を登録: {} -> {:?}",
56        special,
57        jp.is_holiday_name(special)
58    );
59    jp.unregister::<CompanyHoliday>();
60    println!(
61        "登録解除後: {} -> {:?}",
62        special,
63        jp.is_holiday_name(special)
64    );
65}

Trait Implementations§

Source§

impl Default for JPHoliday

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.