1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use super::{SolarYear, HeavenlyStems, EarthlyBranch, Zodiac, LunarYear, LunarMonth, LEAP_MONTHS, BIG_MONTHS, MIN_YEAR_IN_SOLAR_CALENDAR, MAX_YEAR_IN_SOLAR_CALENDAR};

use std::fmt::{self, Display, Formatter};

/// 農曆西曆年,農曆新年所在的西曆年份。
#[derive(Debug, PartialOrd, Ord, PartialEq, Clone, Eq, Hash, Copy)]
pub struct LunisolarYear {
    solar_year: SolarYear,
}

impl LunisolarYear {
    /// 透過西曆年份來取得 `LunisolarYear` 實體。
    pub unsafe fn from_solar_year_unsafe<Y: Into<SolarYear>>(solar_year: Y) -> LunisolarYear {
        let solar_year = solar_year.into();

        LunisolarYear {
            solar_year
        }
    }

    /// 透過西曆年份來取得 `LunisolarYear` 實體。
    pub fn from_solar_year<Y: Into<SolarYear>>(solar_year: Y) -> Option<LunisolarYear> {
        let solar_year = solar_year.into();

        let year = solar_year.to_u16();

        if year >= MIN_YEAR_IN_SOLAR_CALENDAR && year <= MAX_YEAR_IN_SOLAR_CALENDAR {
            Some(LunisolarYear {
                solar_year
            })
        } else {
            None
        }
    }

    /// 取得此西曆年中,農曆新年的中國天干。
    pub fn get_heavenly_stems(&self) -> HeavenlyStems {
        let index = (7 + (self.solar_year.to_u16() - MIN_YEAR_IN_SOLAR_CALENDAR)) % 10;

        unsafe {
            HeavenlyStems::from_ordinal_unsafe(index as i8)
        }
    }

    /// 取得此西曆年中,農曆新年的中國地支。
    pub fn get_earthly_branch(&self) -> EarthlyBranch {
        let index = (self.solar_year.to_u16() - MIN_YEAR_IN_SOLAR_CALENDAR + 1) % 12;

        unsafe {
            EarthlyBranch::from_ordinal_unsafe(index as i8)
        }
    }

    /// 取得此西曆年中,農曆新年所屬的生肖。
    pub fn get_zodiac(&self) -> Zodiac {
        let index = (self.solar_year.to_u16() - MIN_YEAR_IN_SOLAR_CALENDAR + 1) % 12;

        unsafe {
            Zodiac::from_ordinal_unsafe(index as i8)
        }
    }

    /// 取得此年的農曆閏月月份。
    pub fn get_leap_lunar_month(&self) -> Option<LunarMonth> {
        let year = self.to_u16();

        let month = LEAP_MONTHS[((year - MIN_YEAR_IN_SOLAR_CALENDAR) / 2) as usize];

        let index = if year % 2 == 1 {
            ((month & 0xf0) >> 4)
        } else {
            (month & 0x0f)
        };

        if index == 0 {
            None
        } else {
            Some(unsafe {
                LunarMonth::from_u8_unsafe(index, true)
            })
        }
    }

    /// 計算此西曆年下的農曆閏月共有幾天。如果沒有閏月,則回傳0。
    pub fn get_total_days_in_leap_month(&self) -> u16 {
        let leap_lunar_month = self.get_leap_lunar_month();

        match leap_lunar_month {
            Some(leap_lunar_month) => {
                self.get_total_days_in_leap_month_inner(leap_lunar_month)
            }
            None => 0
        }
    }

    /// 計算指定的農曆閏月共有幾天。
    pub(crate) fn get_total_days_in_leap_month_inner(&self, leap_lunar_month: LunarMonth) -> u16 {
        let year = self.to_u16();

        let leap_month = leap_lunar_month.to_u8();

        if (BIG_MONTHS[(year - MIN_YEAR_IN_SOLAR_CALENDAR) as usize] & (0x8000 >> leap_month)) == 0 {
            29
        } else {
            30
        }
    }

    /// 計算此西曆年下的農曆年共有幾天。。
    pub fn get_total_days(&self) -> u16 {
        let leap_lunar_month = self.get_leap_lunar_month();

        let (leap_month, mut n) = match leap_lunar_month {
            Some(leap_lunar_month) => {
                (leap_lunar_month.to_u8(), self.get_total_days_in_leap_month_inner(leap_lunar_month))
            }
            None => (0, 0)
        };

        let year = self.to_u16();

        if leap_month > 0 {
            for month in 1..=leap_month {
                if (BIG_MONTHS[(year - MIN_YEAR_IN_SOLAR_CALENDAR) as usize] & (0x8000 >> (month - 1))) == 0 {
                    n += 29;
                } else {
                    n += 30;
                }
            }

            for month in (leap_month + 1)..=12 {
                if (BIG_MONTHS[(year - MIN_YEAR_IN_SOLAR_CALENDAR) as usize] & (0x8000 >> month)) == 0 {
                    n += 29;
                } else {
                    n += 30;
                }
            }
        } else {
            for month in 1..=12 {
                if (BIG_MONTHS[(year - MIN_YEAR_IN_SOLAR_CALENDAR) as usize] & (0x8000 >> (month - 1))) == 0 {
                    n += 29;
                } else {
                    n += 30;
                }
            }
        }

        n
    }

    /// 計算此西曆年下的農曆年的某個月共有幾天。。
    pub fn get_total_days_in_a_month(&self, lunar_month: LunarMonth) -> Option<u8> {
        lunar_month.get_total_days(*self)
    }

    /// 取得 `LunarYear` 實體。
    pub fn to_lunar_year(&self) -> LunarYear {
        let heavenly_stems = self.get_heavenly_stems();
        let earthly_branch = self.get_earthly_branch();

        LunarYear::from_era(heavenly_stems, earthly_branch)
    }

    /// 取得 `SolarYear` 實體。
    pub fn to_solar_year(&self) -> SolarYear {
        self.solar_year
    }

    /// 取得 `LunisolarYear` 實體所代表的西曆年份數值。
    pub fn to_u16(&self) -> u16 {
        self.solar_year.to_u16()
    }
}

impl Display for LunisolarYear {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        Display::fmt(&self.solar_year, f)
    }
}