tx
2025-04-14 c33f54888d8fb4e1961bca69fe3d01e87fc54be6
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
// 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 FixDateFestival : Festival
    {
        /// <summary> 节日的第一个日期 </summary>
        public DateTime? First { get; protected set; }
        /// <summary> 年份 </summary>
        public int Year { get; private set; }
        /// <summary> 日期 </summary>
        public DateTime Date { get; private set; }
        /// <summary>
        /// 定义一个固定公历日期节假日
        /// </summary>
        /// <param name="name">节日名称</param>
        /// <param name="year">年份</param>
        /// <param name="month">月份</param>
        /// <param name="day">日期</param>
        /// <param name="description">节日描述</param>
        /// <exception cref="ArgumentNullException">没有名称</exception>
        /// <exception cref="ArgumentOutOfRangeException">年月日超出范围</exception>
        public FixDateFestival(string name, int year, int month, int day, string description = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            try
            {
                this.Name = name;
                this.Year = year;
                this.Month = month;
                this.Day = day;
                this.Description = description;
                this.First = this.Date = new DateTime(year, month, day);
            }
            catch (Exception ex)
            {
                throw new ArgumentOutOfRangeException("日期参数不正确", ex);
            }
        }
        /// <summary>
        /// 定义一个固定公历日期节假日
        /// </summary>
        /// <param name="name">节日名称</param>
        /// <param name="date">节日的公历日期</param>
        /// <param name="description">节日描述</param>
        /// <exception cref="ArgumentNullException">没有名称</exception>
        public FixDateFestival(string name, DateTime date, string description = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            this.Name = name;
            this.Year = date.Year;
            this.Month = date.Month;
            this.Day = date.Day;
            this.Description = description;
            this.Date = date;
        }
 
        /// <inheritdoc/>
        public override DateTime? GetLastDate(DateTime? date, bool containsThisDate = false)
        {
            DateTime date2 = date.HasValue ? date.Value.Date : DateTime.Today;
            if (containsThisDate && IsThisFestival(date2))
            {
                return date2;
            }
            if (this.Date < date2)
            {
                return this.Date;
            }
            return null;
        }
 
        /// <inheritdoc/>
        public override DateTime? GetNextDate(DateTime? date, bool containsThisDate = false)
        {
            DateTime date2 = date.HasValue ? date.Value.Date : DateTime.Today;
            if (containsThisDate && IsThisFestival(date2))
            {
                return date2;
            }
            if (this.Date > date2)
            {
                return this.Date;
            }
            return null;
        }
        /// <inheritdoc/>
        public override bool IsThisFestival(DateTime date)
        {
            return date.Date == Date.Date;
        }
    }
}