-
Notifications
You must be signed in to change notification settings - Fork 0
/
jalali.py
196 lines (171 loc) · 6.17 KB
/
jalali.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
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
#! /usr/bin/python
# -*- coding: utf-8 -*-
# Jalali date converter
# 2014 07 25
# Ported from PHP (http://jdf.scr.ir/) to Python (2&3) by Mohammad Javad Naderi <[email protected]>
#
# As mentioned in http://jdf.scr.ir/, the original code is free and open source,
# and you are not allowed to sell it. You can read more in http://jdf.scr.ir/.
#
# Original License Notes:
#
# /** Software Hijri_Shamsi , Solar(Jalali) Date and Time
# Copyright(C)2011, Reza Gholampanahi , http://jdf.scr.ir
# version 2.55 :: 1391/08/24 = 1433/12/18 = 2012/11/15 */
#
# /** Convertor from and to Gregorian and Jalali (Hijri_Shamsi,Solar) Functions
# Copyright(C)2011, Reza Gholampanahi [ http://jdf.scr.ir/jdf ] version 2.50 */
"""
Example Usage:
>>> import jalali
>>> jalali.Persian('1393-1-11').gregorian_string()
'2014-3-31'
>>> jalali.Persian(1393, 1, 11).gregorian_datetime()
datetime.date(2014, 3, 31)
>>> jalali.Persian('1393/1/11').gregorian_string("{}/{}/{}")
'2014/3/31'
>>> jalali.Persian((1393, 1, 11)).gregorian_tuple()
(2014, 3, 31)
>>> jalali.Gregorian('2014-3-31').persian_string()
'1393-1-11'
>>> jalali.Gregorian('2014,03,31').persian_tuple()
(1393, 1, 11)
>>> jalali.Gregorian(2014, 3, 31).persian_year
1393
"""
import re
import datetime
class Gregorian:
def __init__(self, *date):
# Parse date
if len(date) == 1:
date = date[0]
if type(date) is str:
m = re.match(r'^(\d{4})\D(\d{1,2})\D(\d{1,2})$', date)
if m:
[year, month, day] = [int(m.group(1)), int(m.group(2)), int(m.group(3))]
else:
raise Exception("Invalid Input String")
elif type(date) is datetime.date:
[year, month, day] = [date.year, date.month, date.day]
elif type(date) is tuple:
year, month, day = date
year = int(year)
month = int(month)
day = int(day)
else:
raise Exception("Invalid Input Type")
elif len(date) == 3:
year = int(date[0])
month = int(date[1])
day = int(date[2])
else:
raise Exception("Invalid Input")
# Check the validity of input date
try:
datetime.datetime(year, month, day)
except:
raise Exception("Invalid Date")
self.gregorian_year = year
self.gregorian_month = month
self.gregorian_day = day
# Convert date to Jalali
d_4 = year % 4
g_a = [0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
doy_g = g_a[month] + day
if d_4 == 0 and month > 2:
doy_g += 1
d_33 = int(((year - 16) % 132) * .0305)
a = 286 if (d_33 == 3 or d_33 < (d_4 - 1) or d_4 == 0) else 287
if (d_33 == 1 or d_33 == 2) and (d_33 == d_4 or d_4 == 1):
b = 78
else:
b = 80 if (d_33 == 3 and d_4 == 0) else 79
if int((year - 10) / 63) == 30:
a -= 1
b += 1
if doy_g > b:
jy = year - 621
doy_j = doy_g - b
else:
jy = year - 622
doy_j = doy_g + a
if doy_j < 187:
jm = int((doy_j - 1) / 31)
jd = doy_j - (31 * jm)
jm += 1
else:
jm = int((doy_j - 187) / 30)
jd = doy_j - 186 - (jm * 30)
jm += 7
self.persian_year = jy
self.persian_month = jm
self.persian_day = jd
def persian_tuple(self):
return self.persian_year, self.persian_month, self.persian_day
def persian_string(self, date_format="{}-{}-{}"):
return date_format.format(self.persian_year, self.persian_month, self.persian_day)
class Persian:
def __init__(self, *date):
# Parse date
if len(date) == 1:
date = date[0]
if type(date) is str:
m = re.match(r'^(\d{4})\D(\d{1,2})\D(\d{1,2})$', date)
if m:
[year, month, day] = [int(m.group(1)), int(m.group(2)), int(m.group(3))]
else:
raise Exception("Invalid Input String")
elif type(date) is tuple:
year, month, day = date
year = int(year)
month = int(month)
day = int(day)
else:
raise Exception("Invalid Input Type")
elif len(date) == 3:
year = int(date[0])
month = int(date[1])
day = int(date[2])
else:
raise Exception("Invalid Input")
# Check validity of date. TODO better check (leap years)
if year < 1 or month < 1 or month > 12 or day < 1 or day > 31 or (month > 6 and day == 31):
raise Exception("Incorrect Date")
self.persian_year = year
self.persian_month = month
self.persian_day = day
# Convert date
d_4 = (year + 1) % 4
if month < 7:
doy_j = ((month - 1) * 31) + day
else:
doy_j = ((month - 7) * 30) + day + 186
d_33 = int(((year - 55) % 132) * .0305)
a = 287 if (d_33 != 3 and d_4 <= d_33) else 286
if (d_33 == 1 or d_33 == 2) and (d_33 == d_4 or d_4 == 1):
b = 78
else:
b = 80 if (d_33 == 3 and d_4 == 0) else 79
if int((year - 19) / 63) == 20:
a -= 1
b += 1
if doy_j <= a:
gy = year + 621
gd = doy_j + b
else:
gy = year + 622
gd = doy_j - a
for gm, v in enumerate([0, 31, 29 if (gy % 4 == 0) else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]):
if gd <= v:
break
gd -= v
self.gregorian_year = gy
self.gregorian_month = gm
self.gregorian_day = gd
def gregorian_tuple(self):
return self.gregorian_year, self.gregorian_month, self.gregorian_day
def gregorian_string(self, date_format="{}-{}-{}"):
return date_format.format(self.gregorian_year, self.gregorian_month, self.gregorian_day)
def gregorian_datetime(self):
return datetime.date(self.gregorian_year, self.gregorian_month, self.gregorian_day)