31
31
"""
32
32
33
33
import calendar
34
- import operator
35
34
import re
36
35
import time as _time
37
36
from datetime import tzinfo , timedelta , datetime , date
38
37
38
+ try :
39
+ any ([0 , 1 ])
40
+ except :
41
+ from vsc .utils .missing import any
42
+
43
+
39
44
class FancyMonth :
40
45
"""Convenience class for month math"""
41
46
def __init__ (self , tmpdate = None , year = None , month = None , day = None ):
@@ -53,7 +58,6 @@ def __init__(self, tmpdate=None, year=None, month=None, day=None):
53
58
54
59
self .date = date (year , month , day )
55
60
56
-
57
61
self .first = None
58
62
self .last = None
59
63
self .nrdays = None
@@ -124,7 +128,7 @@ def get_start_end(self, otherdate):
124
128
def number (self , otherdate ):
125
129
"""Calculate the number of months between this month (date actually) and otherdate
126
130
"""
127
- if self .include == False :
131
+ if self .include is False :
128
132
msg = "number: include=False not implemented"
129
133
raise (Exception (msg ))
130
134
else :
@@ -137,22 +141,22 @@ def number(self, otherdate):
137
141
138
142
return nr
139
143
140
- def get_other (self , shift = - 1 ):
144
+ def get_other (self , shift = - 1 ):
141
145
"""Return month that is shifted shift months: negative integer is in past, positive is in future"""
142
146
new = self .date .year * 12 + self .date .month - 1 + shift
143
147
return self .__class__ (date (new // 12 , new % 12 + 1 , 01 ))
144
148
145
149
def interval (self , otherdate ):
146
150
"""Return time ordered list of months between date and otherdate"""
147
- if self .include == False :
151
+ if self .include is False :
148
152
msg = "interval: include=False not implemented"
149
153
raise (Exception (msg ))
150
154
else :
151
155
nr = self .number (otherdate )
152
156
startdate , enddate = self .get_start_end (otherdate )
153
157
154
158
start = self .__class__ (startdate )
155
- all_dates = [ start .get_other (m ) for m in range (nr )]
159
+ all_dates = [start .get_other (m ) for m in range (nr )]
156
160
157
161
return all_dates
158
162
@@ -192,6 +196,7 @@ def parser(self, txt):
192
196
193
197
return res
194
198
199
+
195
200
def date_parser (txt ):
196
201
"""Parse txt
197
202
@@ -208,8 +213,10 @@ def date_parser(txt):
208
213
if txt .endswith ('MONTH' ):
209
214
m = FancyMonth ()
210
215
res = m .parser (txt )
211
- elif reduce (operator .or_ , testsupportedmonths ): # TODO replace with any()
212
- m = FancyMonth (month = testsupportedmonths .index (True ) + 1 )
216
+ elif any (testsupportedmonths ):
217
+ # set day=1 or this will fail on day's with an index more then the count of days then the month you want to parse
218
+ # e.g. will fail on 31'st when trying to parse april
219
+ m = FancyMonth (month = testsupportedmonths .index (True ) + 1 , day = 1 )
213
220
res = m .parser (txt )
214
221
elif txt in reserveddate :
215
222
if txt in ('TODAY' ,):
@@ -229,6 +236,7 @@ def date_parser(txt):
229
236
230
237
return res
231
238
239
+
232
240
def datetime_parser (txt ):
233
241
"""Parse txt: tmpdate YYYY-MM-DD HH:MM:SS.mmmmmm in datetime.datetime
234
242
- date part is parsed with date_parser
@@ -255,6 +263,7 @@ def datetime_parser(txt):
255
263
256
264
return res
257
265
266
+
258
267
def timestamp_parser (timestamp ):
259
268
"""Parse timestamp to datetime"""
260
269
return datetime .fromtimestamp (float (timestamp ))
@@ -267,8 +276,8 @@ def timestamp_parser(timestamp):
267
276
ZERO = timedelta (0 )
268
277
HOUR = timedelta (hours = 1 )
269
278
270
- # A UTC class.
271
279
280
+ # A UTC class.
272
281
class UTC (tzinfo ):
273
282
"""UTC"""
274
283
@@ -283,12 +292,14 @@ def dst(self, dt):
283
292
284
293
utc = UTC ()
285
294
286
- # A class building tzinfo objects for fixed-offset time zones.
287
- # Note that FixedOffset(0, "UTC") is a different way to build a
288
- # UTC tzinfo object.
289
295
290
296
class FixedOffset (tzinfo ):
291
- """Fixed offset in minutes east from UTC."""
297
+ """Fixed offset in minutes east from UTC.
298
+
299
+ This is a class for building tzinfo objects for fixed-offset time zones.
300
+ Note that FixedOffset(0, "UTC") is a different way to build a
301
+ UTC tzinfo object.
302
+ """
292
303
def __init__ (self , offset , name ):
293
304
self .__offset = timedelta (minutes = offset )
294
305
self .__name = name
@@ -302,17 +313,20 @@ def tzname(self, dt):
302
313
def dst (self , dt ):
303
314
return ZERO
304
315
305
- # A class capturing the platform's idea of local time.
306
316
307
- STDOFFSET = timedelta (seconds = - _time .timezone )
317
+ STDOFFSET = timedelta (seconds = - _time .timezone )
308
318
if _time .daylight :
309
- DSTOFFSET = timedelta (seconds = - _time .altzone )
319
+ DSTOFFSET = timedelta (seconds = - _time .altzone )
310
320
else :
311
321
DSTOFFSET = STDOFFSET
312
322
313
323
DSTDIFF = DSTOFFSET - STDOFFSET
314
324
325
+
315
326
class LocalTimezone (tzinfo ):
327
+ """
328
+ A class capturing the platform's idea of local time.
329
+ """
316
330
317
331
def utcoffset (self , dt ):
318
332
if self ._isdst (dt ):
0 commit comments