This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
nepdate.py
277 lines (235 loc) · 10.7 KB
/
nepdate.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
### BEGIN LICENSE
# Copyright (C) 2013 Shritesh Bhattarai [email protected]
# This library is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License, as published
# by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
### END LICENSE
import datetime
'''
A module to convert Bikram Samwat (B.S.) to A.D. and vice versa.
Usage:
print nepdate.ad2bs((1995,9,12))
print nepdate.bs2ad((2052,05,27))
Range:
1944 A.D. to 2033 A.D.
2000 B.S. to 2089 B.S.
bs : a dictionary that contains the number of days in each month of the B.S. year
bs_equiv, ad_equiv : The B.S. and A.D. equivalent dates for counting and calculation
'''
(bs_equiv, ad_equiv) = ((2000,9,17),(1944,1,1))
bs = {}
bs[2000]=(30,32,31,32,31,30,30,30,29,30,29,31)
bs[2001]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2002]=(31,31,32,32,31,30,30,29,30,29,30,30)
bs[2003]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2004]=(30,32,31,32,31,30,30,30,29,30,29,31)
bs[2005]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2006]=(31,31,32,32,31,30,30,29,30,29,30,30)
bs[2007]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2008]=(31,31,31,32,31,31,29,30,30,29,29,31)
bs[2009]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2010]=(31,31,32,32,31,30,30,29,30,29,30,30)
bs[2011]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2012]=(31,31,31,32,31,31,29,30,30,29,30,30)
bs[2013]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2014]=(31,31,32,32,31,30,30,29,30,29,30,30)
bs[2015]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2016]=(31,31,31,32,31,31,29,30,30,29,30,30)
bs[2017]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2018]=(31,32,31,32,31,30,30,29,30,29,30,30)
bs[2019]=(31,32,31,32,31,30,30,30,29,30,29,31)
bs[2020]=(31,31,31,32,31,31,30,29,30,29,30,30)
bs[2021]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2022]=(31,32,31,32,31,30,30,30,29,29,30,30)
bs[2023]=(31,32,31,32,31,30,30,30,29,30,29,31)
bs[2024]=(31,31,31,32,31,31,30,29,30,29,30,30)
bs[2025]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2026]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2027]=(30,32,31,32,31,30,30,30,29,30,29,31)
bs[2028]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2029]=(31,31,32,31,32,30,30,29,30,29,30,30)
bs[2030]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2031]=(30,32,31,32,31,30,30,30,29,30,29,31)
bs[2032]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2033]=(31,31,32,32,31,30,30,29,30,29,30,30)
bs[2034]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2035]=(30,32,31,32,31,31,29,30,30,29,29,31)
bs[2036]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2037]=(31,31,32,32,31,30,30,29,30,29,30,30)
bs[2038]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2039]=(31,31,31,32,31,31,29,30,30,29,30,30)
bs[2040]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2041]=(31,31,32,32,31,30,30,29,30,29,30,30)
bs[2042]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2043]=(31,31,31,32,31,31,29,30,30,29,30,30)
bs[2044]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2045]=(31,32,31,32,31,30,30,29,30,29,30,30)
bs[2046]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2047]=(31,31,31,32,31,31,30,29,30,29,30,30)
bs[2048]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2049]=(31,32,31,32,31,30,30,30,29,29,30,30)
bs[2050]=(31,32,31,32,31,30,30,30,29,30,29,31)
bs[2051]=(31,31,31,32,31,31,30,29,30,29,30,30)
bs[2052]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2053]=(31,32,31,32,31,30,30,30,29,29,30,30)
bs[2054]=(31,32,31,32,31,30,30,30,29,30,29,31)
bs[2055]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2056]=(31,31,32,31,32,30,30,29,30,29,30,30)
bs[2057]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2058]=(30,32,31,32,31,30,30,30,29,30,29,31)
bs[2059]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2060]=(31,31,32,32,31,30,30,29,30,29,30,30)
bs[2061]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2062]=(30,32,31,32,31,31,29,30,29,30,29,31)
bs[2063]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2064]=(31,31,32,32,31,30,30,29,30,29,30,30)
bs[2065]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2066]=(31,31,31,32,31,31,29,30,30,29,29,31)
bs[2067]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2068]=(31,31,32,32,31,30,30,29,30,29,30,30)
bs[2069]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2070]=(31,31,31,32,31,31,29,30,30,29,30,30)
bs[2071]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2072]=(31,32,31,32,31,30,30,29,30,29,30,30)
bs[2073]=(31,32,31,32,31,30,30,30,29,29,30,31)
bs[2074]=(31,31,31,32,31,31,30,29,30,29,30,30)
bs[2075]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2076]=(31,32,31,32,31,30,30,30,29,29,30,30)
bs[2077]=(31,32,31,32,31,30,30,30,29,30,29,31)
bs[2078]=(31,31,31,32,31,31,30,29,30,29,30,30)
bs[2079]=(31,31,32,31,31,31,30,29,30,29,30,30)
bs[2080]=(31,32,31,32,31,30,30,30,29,29,30,30)
bs[2081]=(31,31,32,32,31,30,30,30,29,30,30,30)
bs[2082]=(30,32,31,32,31,30,30,30,29,30,30,30)
bs[2083]=(31,31,32,31,31,30,30,30,29,30,30,30)
bs[2084]=(31,31,32,31,31,30,30,30,29,30,30,30)
bs[2085]=(31,32,31,32,30,31,30,30,29,30,30,30)
bs[2086]=(30,32,31,32,31,30,30,30,29,30,30,30)
bs[2087]=(31,31,32,31,31,31,30,30,29,30,30,30)
bs[2088]=(30,31,32,32,30,31,30,30,29,30,30,30)
bs[2089]=(30,32,31,32,31,30,30,30,29,30,30,30)
bs[2090]=(30,32,31,32,31,30,30,30,29,30,30,30)
def date_from_tuple(tuple_to_convert):
'''
Returns the given tuple as datetime.date object
tuple_to_convert : A tuple in the format (year,month,day)
'''
(year, month, day) = tuple_to_convert
return datetime.date(year,month,day)
def tuple_from_date(date_to_convert):
'''
Returns the given date object as tuple in the format (year,month,day)
date_to_convert : A date object
'''
(year, month, day) = (date_to_convert.year, date_to_convert.month, date_to_convert.day)
return (year, month, day)
def count_ad_days(begin_ad_date,end_ad_date):
'''
Returns the number of days between the two given A.D. dates.
begin_ad_date : A tuple in the format (year,month,day) that specify the date to start counting from.
end_ad_date : A tuple in the format (year,month,day) that specify the date to end counting.
'''
date_begin = date_from_tuple(begin_ad_date)
date_end = date_from_tuple(end_ad_date)
delta = date_end - date_begin
return delta.days
def count_bs_days(begin_bs_date,end_bs_date):
'''
Returns the number of days between the two given B.S. dates.
begin_ad_date : A tuple in the format (year,month,day) that specify the date to start counting from.
end_ad_date : A tuple in the format (year,month,day) that specify the date to end counting.
Algorithm:
Its not the piece of algorithm, but it works for this program..
1) First add total days in all the years
2) Subtract the days from first (n-1) months of the beginning year
3) Add the number of days from the last month of the beginning year
4) Subtract the days from the last months from the end year
5) Add the beginning days excluding the day itself
6) Add the last remaining days excluding the day itself
NOTE:
Tuple in the dictionary starts from 0
The range(a,b) function starts from a and ends at b-1
'''
begin_year, begin_month, begin_day = begin_bs_date
end_year, end_month, end_day = end_bs_date
days = 0
#1) First add total days in all the years
for year in range(begin_year, end_year + 1):
for days_in_month in bs[year]:
days = days + days_in_month
#2) Subtract the days from first (n-1) months of the beginning year
for month in range(0,begin_month):
days = days - bs[begin_year][month]
#3) Add the number of days from the last month of the beginning year
days = days + bs[begin_year][12-1]
#4) Subtract the days from the last months from the end year
for month in range(end_month - 1,12):
days = days - bs[end_year][month]
#5) Add the beginning days excluding the day itself
days = days - begin_day - 1
#5) Add the last remaining days excluding the day itself
days = days + end_day - 1
return days
def add_ad_days(ad_date,num_days):
'''
Adds the given number of days to the given A.D. date and returns it as a tuple in the format (year,month,day)
ad_date : A tuple in the format (year,month,day)
num_days : Number of days to add to the given date
'''
date = date_from_tuple(ad_date)
day = datetime.timedelta(days=num_days)
return tuple_from_date(date + day)
def add_bs_days(bs_date,num_days):
'''
Adds the given number of days to the given B.S. date and returns it as a tuple in the format (year,month,day)
bs_date : a tuple in the format (year,month,day)
num_days : Number of days to add to the given date
Algorithm:
1) Add the total number of days to the original days
2) Until the number of days becomes applicable to the current month, subtract the days by the number of days in the current month and increase the month
3) If month reaches 12, increase the year by 1 and set the month to 1
Note:
Tuple in the dictionary starts from 0
'''
(year, month, day) = bs_date
#1) Add the total number of days to the original days
day = day + num_days
#2) Until the number of days becomes applicable to the current month, subtract the days by the number of days in the current month and increase the month
while day > bs[year][month - 1]:
day = day - bs[year][month- 1]
month = month + 1
#3) If month reaches 12, increase the year by 1 and set the month to 1
if month > 12:
month = 1
year = year + 1
return (year, month, day)
def bs2ad(bs_date):
'''
Returns the A.D. equivalent date as a tuple in the format (year,month,day) if the date is within range, else returns None
bs_date : A tuple in the format (year,month,day)
'''
(year, month, day) = bs_date
if year < 2000 or year > 2089 or month < 1 or month > 12 or day < 1 or day > 32:
return None
else:
date_delta = count_bs_days(bs_equiv, bs_date)
return add_ad_days(ad_equiv, date_delta)
def ad2bs(ad_date):
'''
Returns the B.S. equivalent date as a tuple in the format (year,month,day) if the date is within range, else returns None
bs_date : An tuple in the format (year,month,day)
'''
(year, month, day) = ad_date
if year < 1944 or year > 2033 or month < 1 or month > 12 or day < 1 or day > 31:
return None
else:
date_delta = count_ad_days(ad_equiv, ad_date)
return add_bs_days(bs_equiv, date_delta)