-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjdate.py
164 lines (140 loc) · 3.5 KB
/
jdate.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
158
159
160
161
162
163
164
#!/usr/bin/python3
import datetime
jMonth = [
"Farvardin",
"Ordibehesht",
"Khordad",
"Tir",
"Mordad",
"Shahrivar",
"Mehr",
"Aban",
"Azar",
"Day",
"Bahman",
"Esfand",
]
gMonth = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]
jWeek = [
"Doshanbe",
"Seshanbe",
"Chaharshanbe",
"Panjshanbe",
"Jom-e",
"Shanbe",
"Yekshanbe",
]
gWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
def gregorian_to_jalali(gd, gm, gy):
# converts gregorian date to jalali
if not gy or not gm or not gd:
raise ValueError
g_d_m = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
gy2 = (gy + 1) if (gm > 2) else gy
days = (
355666
+ (365 * gy)
+ (((gy2 + 3) // 4))
- (((gy2 + 99) // 100))
+ (((gy2 + 399) // 400))
+ gd
+ g_d_m[gm - 1]
)
jy = -1595 + (33 * ((days // 12053)))
days %= 12053
jy += 4 * ((days // 1461))
days %= 1461
if days > 365:
jy += (days - 1) // 365
days = (days - 1) % 365
jm = (1 + (days // 31)) if (days < 186) else (7 + ((days - 186) // 30))
jd = 1 + ((days % 31) if (days < 186) else ((days - 186) % 30))
return [jd, jm, jy]
def jalali_to_gregorian(jd, jm, jy):
# converts jalali date to gregorian
if not jy or not jm or not jd:
raise ValueError
jy += 1595
days = (
-355668
+ (365 * jy)
+ (((jy // 33)) * 8)
+ (((jy % 33) + 3) // 4)
+ jd
+ ((jm - 1) * 31 if (jm < 7) else ((jm - 7) * 30) + 186)
)
gy = 400 * ((days // 146097))
days = days % 146097
if days > 36524:
gy += 100 * ((--days // 36524))
days = days % 36524
if days >= 365:
days += 1
gy += 4 * ((days // 1461))
days = days % 1461
if days > 365:
gy += (days - 1) // 365
days = (days - 1) % 365
gd = days + 1
sal_a = [
0,
31,
29 if (((gy % 4) == 0 and (gy % 100) != 0) or ((gy % 400) == 0)) else 28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
]
gm = 0
while gm < 13 and gd > sal_a[gm]:
gd -= sal_a[gm]
gm += 1
return [gd - 1, gm, gy]
def gToday():
gDate = datetime.datetime.today()
return [gDate.day, gDate.month, gDate.year]
def jToday():
gDate = gToday()
return gregorian_to_jalali(gDate[0], gDate[1], gDate[2])
def weekDay():
return datetime.datetime.today().weekday()
def formatDate(date):
if not (type(date) == list and len(date) > 2):
raise ValueError
else:
return "{0:0=4d}/{1:0=2d}/{2:0=2d}".format(date[2], date[1], date[0])
def jFormatToday():
date = jToday()
return "{0:0=4d} {1} {2} {3:0=2d}".format(
date[2], jWeek[weekDay()][:3], jMonth[date[1] - 1][:3], date[0]
)
def gFormatToday():
date = gToday()
return "{0:0=4d} {1} {2} {3:0=2d}".format(
date[2], gWeek[weekDay()][:3], gMonth[date[1] - 1][:3], date[0]
)
# print(gregorian_to_jalali(6,9,2023))
# print(jalali_to_gregorian(1,4,1402))
# print(gToday())
# print(jToday())
# print(formatDate(gToday()))
print(jFormatToday())