-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.py
65 lines (60 loc) · 1.85 KB
/
converter.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
# Gregorian & Jalali ( Hijri_Shamsi , Solar ) Date Converter Functions
# Author: JDF.SCR.IR =>> Download Full Version : http://jdf.scr.ir/jdf
# License: GNU/LGPL _ Open Source & Free :: Version: 2.80 : [2020=1399]
# ---------------------------------------------------------------------
# 355746=361590-5844 & 361590=(30*33*365)+(30*8) & 5844=(16*365)+(16/4)
# 355666=355746-79-1 & 355668=355746-79+1 & 1595=605+990 & 605=621-16
# 990=30*33 & 12053=(365*33)+(32/4) & 36524=(365*100)+(100/4)-(100/100)
# 1461=(365*4)+(4/4) & 146097=(365*400)+(400/4)-(400/100)+(400/400)
def gregorian_to_jalali(gy, gm, gd):
g_d_m = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
if (gm > 2):
gy2 = gy + 1
else:
gy2 = 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
if (days < 186):
jm = 1 + (days // 31)
jd = 1 + (days % 31)
else:
jm = 7 + ((days - 186) // 30)
jd = 1 + ((days - 186) % 30)
return [jy, jm, jd]
def jalali_to_gregorian(jy, jm, jd):
jy += 1595
days = -355668 + (365 * jy) + ((jy // 33) * 8) + (((jy % 33) + 3) // 4) + jd
if (jm < 7):
days += (jm - 1) * 31
else:
days += ((jm - 7) * 30) + 186
gy = 400 * (days // 146097)
days %= 146097
if (days > 36524):
days -= 1
gy += 100 * (days // 36524)
days %= 36524
if (days >= 365):
days += 1
gy += 4 * (days // 1461)
days %= 1461
if (days > 365):
gy += ((days - 1) // 365)
days = (days - 1) % 365
gd = days + 1
if ((gy % 4 == 0 and gy % 100 != 0) or (gy % 400 == 0)):
kab = 29
else:
kab = 28
sal_a = [0, 31, kab, 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 [gy, gm, gd]