-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdate_conversion.py
157 lines (139 loc) · 4.31 KB
/
date_conversion.py
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
# -*- coding: utf-8 -*-
"""
Some functions to convert between Dekatrian and Gregorian calendar.
@author: Pena
11\10\2017
dekatrian.com
"""
def Dek2week(dekDay, dekMonth, dekYear):
"""
Returns the Gregorian week day from a Dekatrian date.
1 = Sunday; 2 = Monday; 3 = Tuesday ... 7 = Saturday.
"""
weekDay = ((WeekdayOnFirstAuroran(dekYear)
+ DekatrianWeek(dekDay, dekMonth) - 2) % 7) + 1
if dekMonth == 0:
weekDay = ((weekDay - 3 + dekDay) % 7) + 1
return weekDay
def DekatrianWeek(dekDay, dekMonth):
"""
Returns the Dekatrian week day from a Dekatrian date.
Here we can see the elegance of Dekatrian, since it's not necessary to
inform the year. Actually, barely it's necessary to inform the month,
as it's only needed to check if that is an Achronian day.
0 = Achronian; 1 = first week day; 2 = second week day ... 7 = seventh.
"""
if dekMonth == 0:
return 0
else:
dekWeekDay = ((dekDay-1) % 7) + 1
return dekWeekDay
def WeekdayOnFirstAuroran(dekYear):
"""
Returns the Gregorian week day for the 1 Auroran of a given year
"""
weekDay = ((1 + 5*((dekYear) % 4) + 4*((dekYear) % 100)
+ 6*((dekYear) % 400)) % 7) + 1
return weekDay
def CheckLeapYear(dekYear):
if (dekYear % 4 == 0) and (dekYear % 100 != 0 or dekYear % 400 == 0):
return 1
else:
return 0
def YearDayOnDekaDate(dekDay, dekMonth, dekYear):
"""
Returns the day of the year from a Dekatrian date.
Achronian is the day 1.
Sinchronian is day 2 when it exists.
"""
if dekMonth == 0:
return dekDay
else:
return (CheckLeapYear(dekYear)) + 1 + (dekMonth-1)*28 + dekDay
def YearDayOnGregDate(day, month, year):
"""
Returns the day of the year from a Gregorian date.
Jan 1 is the day 1.
Dez 31 is the day 365 or 366, whether it's a leap year or not
"""
Jan = 31
Fev = 28 + CheckLeapYear(year)
Mar = 31
Apr = 30
Mai = 31
Jun = 30
Jul = 31
Ago = 31
Set = 30
Out = 31
Nov = 30
Dez = 31
Meses = (Jan, Fev, Mar, Apr, Mai, Jun, Jul, Ago, Set, Out, Nov, Dez)
i = 0
days = 0
while i < (month-1):
days += Meses[i]
i += 1
return days + day
def Dek2Greg(dekDay, dekMonth, dekYear):
"""
Returns a Gregorian date from a Dekatrian date.
"""
YearDay = YearDayOnDekaDate(dekDay, dekMonth, dekYear)
Jan = 31
Fev = 28 + CheckLeapYear(dekYear)
Mar = 31
Apr = 30
Mai = 31
Jun = 30
Jul = 31
Ago = 31
Set = 30
Out = 31
Nov = 30
Dez = 31
Meses = (Jan, Fev, Mar, Apr, Mai, Jun, Jul, Ago, Set, Out, Nov, Dez)
for mes, dias in enumerate(Meses, start=1):
if YearDay > dias:
YearDay -= dias
else:
break
return (YearDay, mes, dekYear)
def Greg2Dek(day, month, year):
"""
Returns a Dekatrian date from a Gregorian date
"""
YearDay = YearDayOnGregDate(day, month, year)
LeapYear = CheckLeapYear(year)
#print(YearDay)
if YearDay > (1 + LeapYear):
YearDay -= 1 + LeapYear
#print(YearDay)
dekMonth = int((YearDay-1) / 28) + 1
dekDay = (YearDay-1) % 28 + 1
else:
dekMonth = 0
dekDay = day
return (dekDay, dekMonth, year)
if __name__ == "__main__":
# Examples #
print("Dekatrian 28\\13\\2015 falls on Greg week day: "
+ str(Dek2week(28, 13, 2015)))
print("Dekatrian 1\\0\\2016 falls on Greg week day: "
+ str(Dek2week(1, 0, 2016)))
print("Dekatrian 2\\0\\2016 falls on Greg week day: "
+ str(Dek2week(2, 0, 2016)))
print("Dekatrian 1\\1\\2016 falls on Greg week day: "
+ str(Dek2week(1, 1, 2016)))
print("Achronian corresponds to Dekatrian week day: "
+ str(DekatrianWeek(1, 0)))
print("Dekatrian 1\\1\\2016 happens on Gregorian week day: "
+ str(WeekdayOnFirstAuroran(2016)))
print("Dekatrian 3\\1\\2017 is the year day: "
+ str(YearDayOnDekaDate(3, 1, 2017)))
print("Dekatrian 10\\10\\2017 corresponds to Gregorian "
+ str(Dek2Greg(10, 10, 2017)))
print("Gregorian 29/12/2016 is the year day: "
+ str(YearDayOnGregDate(29, 12, 2016)))
print("Gregorian 3/1/2016 corresponds to Dekatrian: "
+ str(Greg2Dek(3, 1, 2016)))