-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calendar.cs
345 lines (290 loc) · 11.3 KB
/
Calendar.cs
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
using System;
using System.Globalization;
using KCalendar.Core.Contract;
using KCalendar.Core.Culture;
namespace KCalendar.Core
{
public abstract class Calendar : PositionalAstronomy, ICalendar
{
protected Calendar()
{
Init();
}
protected Calendar(int year, int month, int day)
: this()
{
Init();
Year = year;
Month = CalendarCulture.GetMonth(month);
Day = day;
}
protected Calendar(double julianNumber)
: this()
{
JulianToDate(julianNumber);
}
protected Calendar(ICalendar iCalendar)
: this(iCalendar.DateToJulian())
{
}
protected Calendar(DateTime dateTime) : this(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second,
dateTime.Millisecond)
{
}
protected Calendar(int year, int month, int day, int hour, int minute, int second) : this()
{
Year = year;
Month = CalendarCulture.GetMonth(month);
Day = day;
Hour = hour;
Minute = minute;
Second = second;
}
protected Calendar(int year, int month, int day, int hour, int minute, int second, int millisecond) : this()
{
Year = year;
Month = CalendarCulture.GetMonth(month);
Day = day;
Hour = hour;
Minute = minute;
Second = second;
Millisecond = millisecond;
}
#region Property
public virtual int Year { get; set; }
public virtual IMonth Month { get; set; }
public virtual int Day { get; set; }
public virtual int Hour { get; set; }
public virtual int Minute { get; set; }
public virtual int Second { get; set; }
public virtual int Millisecond { get; set; }
#endregion
public virtual int MonthCount => CalendarCulture.MonthCount;
public abstract double Epoch { get; }
public virtual double JulianDay => DateToJulian(this);
public bool IsLeap
{
get
{
var isLeap = LeapAlgorithm.IsLeap(this);
CalendarCulture.SetLeap(isLeapYear: isLeap);
return isLeap;
}
}
public abstract int DayOfYear { get; }
public IWeek DayOfWeek => CalendarCulture.GetWeekDay((int)(Math.Ceiling(Math.Floor((JulianDay + 1.5)) % 7)) + 1);
public abstract ICalendarLeap LeapAlgorithm { get; set; }
public abstract ICalendar JulianToDate(double julianNumber);
public ICalendar AddDay(int day)
{
var j = JulianDay + day;
return JulianToDate(j);
}
private IMonth ChangeMonth(int amountOfChangeMonth)
{
if (amountOfChangeMonth >= MonthCount || amountOfChangeMonth < 1)
throw new CalendarExceptions();
Month = CalendarCulture.GetMonth(Month.Index + amountOfChangeMonth);
return CalendarCulture.GetMonth(Month);
}
public IMonth ChangeMonth(IMonth month, int amountOfChangeMonth)
{
if (amountOfChangeMonth >= MonthCount || amountOfChangeMonth < 1)
throw new CalendarExceptions();
month = CalendarCulture.GetMonth(month + amountOfChangeMonth);
return month;
}
#region addMonth
/// <summary>
/// Add or minus some month to this instant
/// </summary>
/// <param name="month">can be variable int number ; negative for minus month and positiv for add month</param>
/// <returns>MemberwiseClone of this instant after calculate</returns>
public virtual ICalendar AddMonth(int month)
{
if (month < 1)
{
return this;
}
if ((Month.Index + month) <= MonthCount)
{
Month = ChangeMonth(month);
return (ICalendar)MemberwiseClone();
}
// Calculate new month and year
var newMonth = (Month.Index + month) % MonthCount;
var newYear = Year + (Month.Index + month - 1) / MonthCount;
// Adjust for the case where newMonth is 0
if (newMonth == 0)
{
newMonth = MonthCount;
newYear--;
}
Year = newYear;
Month = ChangeMonth(newMonth);
return (ICalendar)MemberwiseClone();
}
#endregion addMonth
public virtual ICalendar AddYear(int year)
{
Year += year;
return (ICalendar)MemberwiseClone();
}
public abstract CalendarCulture CalendarCulture { get; set; }
public virtual double DateToJulian()
{
return DateToJulian(this);
}
public abstract double DateToJulian(ICalendar calendarDate);
public virtual ICalendar CastTo(ICalendar iCalendar)
{
if (iCalendar == null) throw new Exception("");
return iCalendar.JulianToDate(JulianDay);
}
public IMonth GetMonthInfo(int month)
{
if (month < 1 && month > MonthCount)
throw new Exception("");
return CalendarCulture.GetMonth(month); //-1
}
/// <summary>
/// Converts the string representation of a date format to its Calendar date equivalent.
/// </summary>
/// <param name="date">A string containing a date format to convert.</param>
/// <returns>Calendar equivalent to the date contained in date.</returns>
public ICalendar Parse(string date)
{
if (date == null) throw new ArgumentNullException(nameof(date));
date = date.ToLower().Trim();
if (date.Length < 6)
{
throw new Exception("Format of string is not a date format!");
}
var tmp = date.Replace('\\', '/').Split('/');
if (tmp == null) throw new Exception("Date Not in correct format!");
if (tmp[2].Length > tmp[0].Length)
{
Year = Convert.ToInt32(tmp[2]);
Month = GetMonthInfo(Convert.ToInt32(tmp[1]));
Day = Convert.ToInt32(tmp[0]);
}
else
{
Year = Convert.ToInt32(tmp[0]);
Month = GetMonthInfo(Convert.ToInt32(tmp[1]));
Day = Convert.ToInt32(tmp[2]);
}
return (ICalendar)MemberwiseClone();
}
public DateTime ToDateTime()
{
var gd = (GregorianDate)CastTo(new GregorianDate());
var dt = new DateTime(gd.Year, gd.Month, gd.Day);
return dt;
}
public static DateTime ToDateTime(ICalendar calendarDate)
{
if (calendarDate == null) throw new ArgumentNullException(nameof(calendarDate));
var gd = (GregorianDate)calendarDate.CastTo(new GregorianDate());
var dt = new DateTime(gd.Year, gd.Month, gd.Day);
return dt;
}
/// <summary>
/// Converts the string representation of a date format to its Calendar date equivalent.
/// </summary>
/// <param name="date">A string containing a date format to convert.</param>
/// <param name="calendarType">a instance of calendar that date string based on it</param>
/// <returns>Calendar equivalent to the date contained in date.</returns>
public static ICalendar Parse(string date, ICalendar calendarType)
{
if (date == null) throw new ArgumentNullException(nameof(date));
if (calendarType == null) throw new ArgumentNullException(nameof(calendarType));
date = date.ToLower().Trim();
if (date.Length < 6)
{
throw new Exception("Format of string is not a date format!");
}
if (calendarType is GregorianDate)
{
DateTime dt;
CultureInfo cultureInfo = new CultureInfo("en-US");
DateTime.TryParse(date, cultureInfo, DateTimeStyles.None, out dt);
return new GregorianDate(dt);
}
var tmp = date.Replace('\\', '/').Replace("-", "/").Replace(".", "/").Replace("_", "/").Split('/');
if (tmp[2].Length > tmp[0].Length)
{
calendarType.Year = Convert.ToInt32(tmp[2]);
calendarType.Month = calendarType.GetMonthInfo(Convert.ToInt32(tmp[1]));
calendarType.Day = Convert.ToInt32(tmp[0]);
}
else
{
calendarType.Year = Convert.ToInt32(tmp[0]);
calendarType.Month = calendarType.GetMonthInfo(Convert.ToInt32(tmp[1]));
calendarType.Day = Convert.ToInt32(tmp[2]);
}
return calendarType;
}
public string ToString(string format)
{
return CalendarCulture.ToString(this, format);
}
public string ToString(DateFormat format)
{
return CalendarCulture.ToString(this, format);
}
public static explicit operator DateTime(Calendar calendar)
{
return new DateTime(calendar.Year, calendar.Month, calendar.Day);
}
protected abstract void Init();
public ICalendar FirstNextMonth(ICalendar destinationCalendar, int month)
{
var destinationDate = CastTo(destinationCalendar);
destinationDate = destinationDate.GotoDate(destinationDate.Year, month, 1);
var thisJulianDay = JulianDay;
return thisJulianDay <= destinationDate.JulianDay ? destinationDate : destinationDate.GotoDate(destinationDate.Year + 1, month, 1);
}
public ICalendar GotoDate(int year, int month, int day)
{
Year = year;
Month = GetMonthInfo(month);
Day = day;
return JulianToDate(JulianDay);
}
public ICalendar GotoDate(int month, int day)
{
Month = GetMonthInfo(month);
Day = day;
return JulianToDate(JulianDay);
}
public ICalendar FirstNextDay(ICalendar destinationCalendarType, int month, int day)
{
var nextMonth = FirstNextMonth(destinationCalendarType, month);
return nextMonth.AddDay(day - 1);
}
public ICalendar FirstWeekDayDate(ICalendar destinationCalendar)
{
var destinationDate = CastTo(destinationCalendar);
var dayofWeekDayIndex = destinationDate.DayOfWeek.DayIndex;
if (dayofWeekDayIndex == 7)
{
return destinationDate;
}
destinationDate = destinationDate.AddDay(-1 * dayofWeekDayIndex);
return destinationDate;
}
public ICalendar LastWeekDayDate(ICalendar destinationCalendar)
{
var destinationDate = CastTo(destinationCalendar);
var dayofWeekDayIndex = destinationDate.DayOfWeek.DayIndex;
if (dayofWeekDayIndex == 7)
{
return destinationDate.AddDay(6);
}
destinationDate = destinationDate.AddDay(6 - dayofWeekDayIndex);
return destinationDate;
}
}
}