yangyin
2025-01-13 809cab44fa1cbea94d84c42cce94a35cc49ce5ad
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
// THIS FILE IS PART OF ChineseCalendar PROJECT
// THE ChineseCalendar PROJECT IS AN OPENSOURCE LIBRARY LICENSED UNDER THE MIT License.
// COPYRIGHT (C) lpz. ALL RIGHTS RESERVED.
// GITEE: https://gitee.com/lipz89/ChineseCalendar
 
using System;
 
namespace ChineseCalendar
{
    /// <summary>
    /// 农历节假日
    /// </summary>
    public class ChineseFestival : LoopFestival
    {
        /// <summary> 节日的第一个日期 </summary>
        public ChineseDate First { get; protected set; }
        private ChineseFestival() { }
        /// <summary>
        /// 定义一个农历节假日
        /// </summary>
        /// <param name="name">节日名称</param>
        /// <param name="month">月份</param>
        /// <param name="day">日期</param>
        /// <param name="firstYear">第一个节日的年份,0表示无永恒</param>
        /// <param name="description">节日描述</param>
        /// <exception cref="ArgumentNullException">没有名称</exception>
        /// <exception cref="ArgumentOutOfRangeException">日期超出范围</exception>
        public ChineseFestival(string name, int month, int day, int? firstYear = null, string description = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (month == 0 || month > 12 || month < -12)
            {
                throw new ArgumentOutOfRangeException(nameof(month), "[-12,-1],[1,12]", "月份超出范围");
            }
            if (day == 0 || day > 30 || day < -30)
            {
                throw new ArgumentOutOfRangeException(nameof(day), "[-30,-1],[1,30]", "日期超出范围");
            }
            this.Name = name;
            this.Month = month;
            this.Day = day;
            this.FirstYear = firstYear;
            this.Description = description;
            if (this.FirstYear.HasValue)
            {
                this.First = ChineseDate.From(firstYear.Value, month, day);
            }
        }
        /// <summary> 春节 </summary>
        public static readonly ChineseFestival SpringFestival = new ChineseFestival
        {
            Name = "春节",
            Description = "正月初一",
            Month = 1,
            Day = 1,
        };
        /// <summary> 元宵节 </summary>
        public static readonly ChineseFestival LanternFestival = new ChineseFestival
        {
            Name = "元宵节",
            Description = "正月十五",
            Month = 1,
            Day = 15,
        };
        /// <summary> 龙抬头 </summary>
        public static readonly ChineseFestival DragonHeadraisingDay = new ChineseFestival
        {
            Name = "龙抬头",
            Description = "二月初二",
            Month = 2,
            Day = 2,
        };
        /// <summary> 端午 </summary>
        public static readonly ChineseFestival DragonBoatFestival = new ChineseFestival
        {
            Name = "端午",
            Description = "五月初五",
            Month = 5,
            Day = 5,
        };
        /// <summary> 七夕 </summary>
        public static readonly ChineseFestival QixiFestival = new ChineseFestival
        {
            Name = "七夕",
            Description = "七月初七",
            Month = 7,
            Day = 7,
        };
        /// <summary> 中元节 </summary>
        public static readonly ChineseFestival GhostFestival = new ChineseFestival
        {
            Name = "中元节",
            Description = "七月十五",
            Month = 7,
            Day = 15,
        };
        /// <summary> 中秋 </summary>
        public static readonly ChineseFestival MidAutumnFestival = new ChineseFestival
        {
            Name = "中秋",
            Description = "八月十五",
            Month = 8,
            Day = 15,
        };
        /// <summary> 重阳节 </summary>
        public static readonly ChineseFestival DoubleNinthFestival = new ChineseFestival
        {
            Name = "重阳节",
            Description = "九月初九",
            Month = 9,
            Day = 9,
        };
        /// <summary> 除夕 </summary>
        public static readonly ChineseFestival NewYearsEve = new ChineseFestival
        {
            Name = "除夕",
            Description = "大年三十",
            Month = -1,
            Day = -1,
        };
 
        /// <inheritdoc/>
        protected override bool TryGetDate(int year, int month, int day, out DateTime date)
        {
            try
            {
                date = ChineseDate.From(year, month, day).ToDate();
                return true;
            }
            catch (Exception)
            {
                date = DateTime.Now;
                return false;
            }
        }
        /// <inheritdoc/>
        public override bool IsThisFestival(DateTime date)
        {
            var cdate = ChineseDate.From(date);
            var festival = ChineseDate.From(cdate.Year, Month, Day);
            return cdate == festival;
        }
    }
}