yangyin
2025-01-13 36616eb44dc36a4e6e3e7a7540310cb850218ea9
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// 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;
using System.Globalization;
 
namespace ChineseCalendar
{
    /// <summary>
    /// 中国农历日期
    /// </summary>
    public class ChineseDate
    {
        private ChineseDate()
        {
        }
        /// <summary>
        /// 转换一个公历日期为农历日期
        /// </summary>
        /// <param name="date">公历日期</param>
        /// <exception cref="ArgumentOutOfRangeException">日期超出范围</exception>
        /// <returns>农历日期</returns>
        public static ChineseDate From(DateTime date)
        {
            try
            {
                var cdate = new ChineseDate();
                cdate.Year = chineseCalendar.GetYear(date);
                cdate.MonthIndex = cdate.Month = chineseCalendar.GetMonth(date);
                cdate.Day = chineseCalendar.GetDayOfMonth(date);
                //获取闰月, 0 则表示没有闰月
                int leapMonth = chineseCalendar.GetLeapMonth(cdate.Year);
                cdate.IsLeapMonth = leapMonth == cdate.Month;
                if (cdate.Month >= leapMonth && leapMonth > 0)
                {
                    cdate.Month--;
                }
                cdate.LeapMonthOfYear = Math.Max(0, leapMonth - 1);
                return cdate;
            }
            catch (ArgumentOutOfRangeException ex)
            {
                throw new ArgumentOutOfRangeException($"日期超出范围 1901-02-19(公历) -- 2101-01-28(公历)", ex);
            }
        }
        /// <summary>
        /// 指定年月日索引,月/日可以为负数,负数表示倒数
        /// </summary>
        /// <param name="year">年份,范围为1901-2100</param>
        /// <param name="month">月份,允许值:1-12(当年不含闰月),1-13(当年含闰月)</param>
        /// <param name="day">日期,允许值:1-30(大月),1-29(小月)</param>
        /// <exception cref="ArgumentOutOfRangeException">日期超出范围</exception>
        /// <returns>农历日期</returns>
        public static ChineseDate FromIndex(int year, int month, int day)
        {
            if (year < 1901 || year > 2100)
            {
                throw new ArgumentOutOfRangeException($"年份超出范围 1901 -- 2100");
            }
            var months = chineseCalendar.GetMonthsInYear(year);
            if (month == 0 || month > months)
            {
                throw new ArgumentOutOfRangeException($"{year}年,月份允许范围为 1 -- {months}");
            }
            if (month < 0 - months)
            {
                throw new ArgumentOutOfRangeException($"{year}年,月份允许范围为 -1 -- {0 - months}");
            }
            if (month < 0)
            {
                month = months + month + 1;
            }
 
            var days = chineseCalendar.GetDaysInMonth(year, month);
            if (day == 0 || day > days)
            {
                throw new ArgumentOutOfRangeException($"日期允许范围为 1 -- {days}");
            }
            if (day < 0 - days)
            {
                throw new ArgumentOutOfRangeException($"日期允许范围为 -1 -- {0 - days}");
            }
            if (day < 0)
            {
                day = days + day + 1;
            }
 
            var cdate = new ChineseDate();
            cdate.Year = year;
            cdate.MonthIndex = cdate.Month = month;
            cdate.Day = day;
            int leapMonth = chineseCalendar.GetLeapMonth(year);
            cdate.IsLeapMonth = leapMonth == cdate.MonthIndex;
            if (cdate.Month >= leapMonth && leapMonth > 0)
            {
                cdate.Month--;
            }
            cdate.LeapMonthOfYear = Math.Max(0, leapMonth - 1);
            return cdate;
        }
        /// <summary>
        /// 指定年月日,月/日可以为负数,负数表示倒数
        /// </summary>
        /// <param name="year">年份,范围为1901-2100</param>
        /// <param name="month">月份,允许值:1-12,正数忽略闰月,负数忽略被闰月</param>
        /// <param name="day">日期,允许值:1-30(大月),1-29(小月)</param>
        /// <exception cref="ArgumentOutOfRangeException">日期超出范围</exception>
        /// <returns>农历日期</returns>
        public static ChineseDate From(int year, int month, int day)
        {
            if (year < 1901 || year > 2100)
            {
                throw new ArgumentOutOfRangeException($"年份超出范围 1901 -- 2100");
            }
            if (month == 0 || month > 12)
            {
                throw new ArgumentOutOfRangeException($"{year}年,月份允许范围为 1 -- 12");
            }
            if (month < 0 - 12)
            {
                throw new ArgumentOutOfRangeException($"{year}年,月份允许范围为 -1 -- -12");
            }
            int leapMonth = chineseCalendar.GetLeapMonth(year);
            if (month < 0)
            {
                month = 12 + month + 1;
            }
            var monthIndex = month;
            if (month > 0)
            {
                if (month >= leapMonth && leapMonth > 0)
                {
                    monthIndex++;
                }
            }
            else if (month < 0)
            {
                if (month >= leapMonth - 1 && leapMonth > 0)
                {
                    monthIndex++;
                }
            }
 
            var days = chineseCalendar.GetDaysInMonth(year, monthIndex);
            if (day == 0 || day > days)
            {
                throw new ArgumentOutOfRangeException($"日期允许范围为 1 -- {days}");
            }
            if (day < 0 - days)
            {
                throw new ArgumentOutOfRangeException($"日期允许范围为 -1 -- {0 - days}");
            }
            if (day < 0)
            {
                day = days + day + 1;
            }
 
            var cdate = new ChineseDate();
            cdate.Year = year;
            cdate.MonthIndex = monthIndex;
            cdate.Month = month;
            cdate.Day = day;
            cdate.IsLeapMonth = monthIndex == leapMonth;
            cdate.LeapMonthOfYear = Math.Max(0, leapMonth - 1);
            return cdate;
        }
        /// <summary> 年份 </summary>
        public int Year { get; internal set; }
        /// <summary> 月份 </summary>
        public int Month { get; internal set; }
        /// <summary> 月份顺序,含闰月 </summary>
        public int MonthIndex { get; internal set; }
        /// <summary> 日期 </summary>
        public int Day { get; internal set; }
        /// <summary> 当前月是闰月 </summary>
        public bool IsLeapMonth { get; internal set; }
        /// <summary> 当年的闰月,0表示无闰月,正常范围 1-12 </summary>
        public int LeapMonthOfYear { get; internal set; }
        /// <summary> 今天 </summary>
        public static ChineseDate Today
        {
            get { return ChineseDate.From(DateTime.Today); }
        }
        /// <summary> 最小值 </summary> 
        public static ChineseDate MinValue
        {
            get { return ChineseDate.From(chineseCalendar.MinSupportedDateTime); }
        }
        /// <summary> 最大值 </summary>
        public static ChineseDate MaxValue
        {
            get { return ChineseDate.From(chineseCalendar.MaxSupportedDateTime); }
        }
        /// <summary>
        /// 返回当前农历日期对应的公历日期
        /// </summary>
        /// <returns>公历日期</returns>
        public DateTime ToDate()
        {
            return chineseCalendar.ToDateTime(this.Year, this.MonthIndex, this.Day, 0, 0, 0, 0);
        }
        /// <inheritdoc/>
        public override bool Equals(object obj)
        {
            if (obj is ChineseDate cd)
            {
                return cd.Year == this.Year && cd.Month == this.Month && cd.Day == this.Day;
            }
            return false;
        }
 
        /// <inheritdoc/>
        public override int GetHashCode()
        {
            return new { Year, MonthIndex, Day }.GetHashCode();
        }
 
        /// <inheritdoc/>
        public override string ToString()
        {
            return $"{ChineseEra}[{AnimalSign}]年{MonthString}月{DayString}";
        }
 
        private static readonly ChineseLunisolarCalendar chineseCalendar = new ChineseLunisolarCalendar();
 
        ///<summary>  农历月 </summary>
        private static readonly string MONTHSTRING = "正二三四五六七八九十冬腊";
        ///<summary> 农历日 </summary>
        private static readonly string DAYSTRING = "初一初二初三初四初五初六初七初八初九初十十一十二十三十四十五十六十七十八十九二十廿一廿二廿三廿四廿五廿六廿七廿八廿九三十";
        ///<summary> 十天干 </summary>
        private static readonly string CELESTIAL_STEMS = "甲乙丙丁戊己庚辛壬癸";
        ///<summary> 十二地支 </summary>
        private static readonly string TERRESTRIAL_BRANCHS = "子丑寅卯辰巳午未申酉戌亥";
        ///<summary> 十二生肖 </summary>
        private static readonly string ANIMAL_SIGNS = "鼠牛虎免龙蛇马羊猴鸡狗猪";
        ///<summary> 数字 </summary>
        private static readonly string DIGITALS = "〇一二三四五六七八九";
 
        /// <summary> 天干 </summary>
        public string CelestialStem
        {
            get { return CELESTIAL_STEMS[(Year - 4) % 10].ToString(); }
        }
        /// <summary> 地支 </summary>
        public string TerrestrialBranch
        {
            get { return TERRESTRIAL_BRANCHS[(Year - 4) % 12].ToString(); }
        }
        /// <summary> 干支 </summary>
        public string ChineseEra
        {
            get { return CelestialStem + TerrestrialBranch; }
        }
        /// <summary> 生肖属相 </summary>
        public string AnimalSign
        {
            get { return ANIMAL_SIGNS[(Year - 4) % 12].ToString(); }
        }
        /// <summary> 星期几 </summary>
        public DayOfWeek DayOfWeek
        {
            get { return ToDate().DayOfWeek; }
        }
        /// <summary> 一年中的第几天,闰年1-384,平年1-354 </summary>
        public int DayOfYear
        {
            get { return chineseCalendar.GetDayOfYear(this.ToDate()); }
        }
        /// <summary> 当月总天数 </summary>
        public int DayInMonth
        {
            get { return chineseCalendar.GetDaysInMonth(this.Year, this.MonthIndex); }
        }
        /// <summary> 当年总天数 </summary>
        public int DayInYear
        {
            get { return chineseCalendar.GetDaysInYear(this.Year); }
        }
        /// <summary> 当年总月份数 </summary>
        public int MonthsInYear
        {
            get { return chineseCalendar.GetMonthsInYear(this.Year); }
        }
        /// <summary> 日历名称 </summary>
        public string CalendarName { get { return "农历"; } }
        /// <summary> 年的字符串 </summary>
        public string YearString
        {
            get
            {
                var str = string.Empty;
                var u = 1000;
                var y = Year;
                while (u > 0)
                {
                    var i = y / u;
                    str += DIGITALS[i];
                    y %= u;
                    u /= 10;
                }
                return str;
            }
        }
        /// <summary> 农历月 </summary>
        public string MonthString
        {
            get
            {
                var str = MONTHSTRING[Month - 1].ToString();
                if (IsLeapMonth)
                {
                    str = "闰" + str;
                }
                return str;
            }
        }
        /// <summary> 农历日 </summary>
        public string DayString
        {
            get
            {
                return DAYSTRING.Substring((Day - 1) * 2, 2);
            }
        }
        /// <summary>
        /// 增加年份数值,
        /// 结果总会是非闰月的日期
        /// </summary>
        /// <param name="value">年份数值,可为负数</param>
        /// <returns>增加指定年份后的日期</returns>
        /// <exception cref="ArgumentOutOfRangeException">日期超出范围</exception>
        public ChineseDate AddYears(int value)
        {
            if (value == 0)
            {
                return this;
            }
            var nyear = this.Year + value;
            if (nyear < 1901 || nyear > 2100)
            {
                throw new ArgumentOutOfRangeException($"年份超出范围 1901 -- 2100");
            }
 
            int leapMonth = chineseCalendar.GetLeapMonth(nyear);
            var nmonthIndex = this.Month;
            if (nmonthIndex >= leapMonth && leapMonth > 0)
            {
                nmonthIndex++;
            }
            var days = chineseCalendar.GetDaysInMonth(nyear, nmonthIndex);
 
            var nday = Math.Min(this.Day, days);
            return FromIndex(nyear, nmonthIndex, nday);
        }
        /// <summary>
        /// 增加月数,
        /// </summary>
        /// <param name="value">月数,可为负数</param>
        /// <returns>增加指定月数后的日期</returns>
        /// <exception cref="ArgumentOutOfRangeException">日期超出范围</exception>
        public ChineseDate AddMonths(int value)
        {
            if (value == 0)
            {
                return this;
            }
            var nyear = this.Year;
            var nmonthIndex = this.MonthIndex + value;
            var months = chineseCalendar.GetMonthsInYear(nyear);
            if (nmonthIndex > months)
            {
                while (nmonthIndex > months)
                {
                    nmonthIndex -= months;
                    nyear += 1;
 
                    if (nyear < 1901 || nyear > 2100)
                    {
                        throw new ArgumentOutOfRangeException($"年份超出范围 1901 -- 2100");
                    }
                    else
                    {
                        months = chineseCalendar.GetMonthsInYear(nyear);
                    }
                }
            }
            else if (nmonthIndex < 1)
            {
                while (nmonthIndex < 1)
                {
                    nyear -= 1;
 
                    if (nyear < 1901 || nyear > 2100)
                    {
                        throw new ArgumentOutOfRangeException($"年份超出范围 1901 -- 2100");
                    }
                    else
                    {
                        months = chineseCalendar.GetMonthsInYear(nyear);
                        nmonthIndex += months;
                    }
                }
            }
            var days = chineseCalendar.GetDaysInMonth(nyear, nmonthIndex);
 
            var nday = Math.Min(this.Day, days);
            return FromIndex(nyear, nmonthIndex, nday);
        }
        /// <summary>
        /// 增加天数,
        /// </summary>
        /// <param name="value">天数,可为负数</param>
        /// <returns>增加指定天数后的日期</returns>
        public ChineseDate AddDays(int value)
        {
            if (value == 0)
            {
                return this;
            }
            return From(ToDate().AddDays(value));
        }
    }
}