-
Notifications
You must be signed in to change notification settings - Fork 0
/
PersianArithmeticDate.cs
112 lines (95 loc) · 4.03 KB
/
PersianArithmeticDate.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
using System;
using KCalendar.Core.Contract;
using KCalendar.Core.Culture;
namespace KCalendar.Core
{
public class PersianArithmeticDate : Calendar
{
public PersianArithmeticDate() { }
public override double Epoch => 1948320.5;
protected override void Init()
{
CalendarCulture = new PersianCalendarCulture();
LeapAlgorithm = new PersianBirashkLeap();
}
public PersianArithmeticDate(int year, int month, int day)
: base(year, month, day)
{ }
protected PersianArithmeticDate(int year, IMonth month, int day, int hour, int minute, int second, int millisecond) : base(year, month, day, hour, minute, second, millisecond) { }
protected PersianArithmeticDate(int year, IMonth month, int day, int hour, int minute, int second) : base(year, month, day, hour, minute, second) { }
public PersianArithmeticDate(double julianNumber)
: base(julianNumber)
{ }
public PersianArithmeticDate(ICalendar iCalendar)
: base(iCalendar)
{ }
public PersianArithmeticDate(DateTime dateTime)
: base(new GregorianDate(dateTime).JulianDay)
{ }
public static ICalendar Today => new PersianArithmeticDate(DateTime.Now);
public override int DayOfYear => (int)(JulianDay - DateToJulian(new PersianArithmeticDate(Year, 1, 1)));
public override ICalendarLeap LeapAlgorithm { get; set; }
/// <summary>
/// Convert Julian number to persian calendarDate
/// </summary>
/// <param name="julianNumber"></param>
/// <returns></returns>
public override ICalendar JulianToDate(double julianNumber)
{
double year;
double month;
double day;
double depoch;
double cycle;
double cyear;
double ycycle;
double aux1;
double aux2;
double yday;
double jd;
jd = Math.Floor(julianNumber) + 0.5;
depoch = jd - DateToJulian(new PersianArithmeticDate(475, 1, 1));
cycle = Math.Floor(depoch / 1029983);
cyear = Mod(depoch, 1029983);
if (Math.Abs(cyear - 1029982) < 0.5)
{
ycycle = 2820;
}
else
{
aux1 = Math.Floor(cyear / 366);
aux2 = cyear % 366;
ycycle = Math.Floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1;
}
year = ycycle + (2820 * cycle) + 474;
year = year <= 0 ? year - 1 : year;
if (year <= 0)
{
year--;
}
yday = (jd - DateToJulian(new PersianArithmeticDate((int)year, 1, 1))) + 1;
month = (yday <= 186) ? Math.Ceiling(yday / 31) : Math.Ceiling((yday - 6) / 30);
day = (jd - DateToJulian(new PersianArithmeticDate((int)year, (int)month, 1))) + 1;
Year = (int)year;
Month = CalendarCulture.GetMonth((int)month);//(int)month;
Day = (int)day;
return new PersianArithmeticDate(Year, Month, Day); ;
}
public override CalendarCulture CalendarCulture { get; set; }
public override double DateToJulian(ICalendar calendarDate)
{
double epbase = calendarDate.Year - ((calendarDate.Year >= 0) ? 474 : 473);
var epyear = 474 + (epbase % 2820);
return (calendarDate.Day + ((calendarDate.Month <= 7) ? ((calendarDate.Month - 1) * 31) : (((calendarDate.Month - 1) * 30) + 6))
+ Math.Floor(((epyear * 682) - 110) / 2816) + (epyear - 1) * 365 + (Epoch - 1));
}
public static explicit operator PersianArithmeticDate(string date)
{
return (PersianArithmeticDate)Parse(date, new PersianArithmeticDate());
}
public static explicit operator PersianArithmeticDate(double jDay)
{
return new PersianArithmeticDate(jDay);
}
}
}