2
2
3
3
CLASSES:
4
4
LocaleTime -- Discovers and/or stores locale-specific time information
5
- TimeRE -- Creates regexes for pattern matching string of text containing
5
+ TimeRE -- Creates regexes for pattern matching string of text containing
6
6
time information as is returned by time.strftime()
7
7
8
8
FUNCTIONS:
9
9
firstjulian -- Calculates the Julian date up to the first of the specified
10
10
year
11
11
gregorian -- Calculates the Gregorian date based on the Julian day and
12
12
year
13
- julianday -- Calculates the Julian day since the first of the year based
13
+ julianday -- Calculates the Julian day since the first of the year based
14
14
on the Gregorian date
15
15
dayofweek -- Calculates the day of the week from the Gregorian date.
16
16
strptime -- Calculates the time struct represented by the passed-in string
@@ -40,23 +40,23 @@ class LocaleTime(object):
40
40
store the values have mangled names):
41
41
f_weekday -- full weekday names (7-item list)
42
42
a_weekday -- abbreviated weekday names (7-item list)
43
- f_month -- full weekday names (14-item list; dummy value in [0], which
43
+ f_month -- full weekday names (14-item list; dummy value in [0], which
44
44
is added by code)
45
- a_month -- abbreviated weekday names (13-item list, dummy value in
45
+ a_month -- abbreviated weekday names (13-item list, dummy value in
46
46
[0], which is added by code)
47
47
am_pm -- AM/PM representation (2-item list)
48
48
LC_date_time -- format string for date/time representation (string)
49
49
LC_date -- format string for date representation (string)
50
50
LC_time -- format string for time representation (string)
51
- timezone -- daylight- and non-daylight-savings timezone representation
52
- (3-item list; code tacks on blank item at end for
51
+ timezone -- daylight- and non-daylight-savings timezone representation
52
+ (3-item list; code tacks on blank item at end for
53
53
possible lack of timezone such as UTC)
54
54
lang -- Language used by instance (string)
55
-
55
+
56
56
"""
57
57
58
- def __init__ (self , f_weekday = None , a_weekday = None , f_month = None ,
59
- a_month = None , am_pm = None , LC_date_time = None , LC_time = None , LC_date = None ,
58
+ def __init__ (self , f_weekday = None , a_weekday = None , f_month = None ,
59
+ a_month = None , am_pm = None , LC_date_time = None , LC_time = None , LC_date = None ,
60
60
timezone = None , lang = None ):
61
61
"""Optionally set attributes with passed-in values."""
62
62
if f_weekday is None : self .__f_weekday = None
@@ -117,9 +117,9 @@ def __get_a_weekday(self):
117
117
if not self .__a_weekday : self .__calc_weekday ()
118
118
return self .__a_weekday
119
119
120
- f_weekday = property (__get_f_weekday , __set_nothing ,
120
+ f_weekday = property (__get_f_weekday , __set_nothing ,
121
121
doc = "Full weekday names" )
122
- a_weekday = property (__get_a_weekday , __set_nothing ,
122
+ a_weekday = property (__get_a_weekday , __set_nothing ,
123
123
doc = "Abbreviated weekday names" )
124
124
125
125
def __get_f_month (self ):
@@ -187,7 +187,7 @@ def __calc_weekday(self):
187
187
f_weekday = [calendar .day_name [i ] for i in range (7 )]
188
188
if not self .__a_weekday : self .__a_weekday = a_weekday
189
189
if not self .__f_weekday : self .__f_weekday = f_weekday
190
-
190
+
191
191
def __calc_month (self ):
192
192
"""Set self.__f_month and self.__a_month using the calendar module."""
193
193
a_month = [calendar .month_abbr [i ] for i in range (13 )]
@@ -197,11 +197,11 @@ def __calc_month(self):
197
197
198
198
def __calc_am_pm (self ):
199
199
"""Set self.__am_pm by using time.strftime().
200
-
201
- The magic date (2002, 3, 17, hour, 44, 44, 2, 76, 0) is not really
202
- that magical; just happened to have used it everywhere else where a
200
+
201
+ The magic date (2002, 3, 17, hour, 44, 44, 2, 76, 0) is not really
202
+ that magical; just happened to have used it everywhere else where a
203
203
static date was needed.
204
-
204
+
205
205
"""
206
206
am_pm = []
207
207
for hour in (01 ,22 ):
@@ -211,10 +211,10 @@ def __calc_am_pm(self):
211
211
212
212
def __calc_date_time (self ):
213
213
"""Set self.__date_time, self.__date, & self.__time by using time.strftime().
214
-
215
- Use (1999,3,17,22,44,55,2,76,0) for magic date because the amount of
216
- overloaded numbers is minimized. The order in which searches for
217
- values within the format string is very important; it eliminates
214
+
215
+ Use (1999,3,17,22,44,55,2,76,0) for magic date because the amount of
216
+ overloaded numbers is minimized. The order in which searches for
217
+ values within the format string is very important; it eliminates
218
218
possible ambiguity for what something represents.
219
219
220
220
"""
@@ -255,17 +255,17 @@ def __calc_date_time(self):
255
255
256
256
def __calc_timezone (self ):
257
257
"""Set self.__timezone by using time.tzname.
258
-
259
- Empty string used for matching when timezone is not used/needed such
258
+
259
+ Empty string used for matching when timezone is not used/needed such
260
260
as with UTC.
261
261
262
262
"""
263
263
self .__timezone = self .__pad (time .tzname , 0 )
264
264
265
265
def __calc_lang (self ):
266
- """Set self.lang by using locale.getlocale() or
266
+ """Set self.lang by using locale.getlocale() or
267
267
locale.getdefaultlocale().
268
-
268
+
269
269
"""
270
270
current_lang = locale .getlocale (locale .LC_TIME )[0 ]
271
271
if current_lang : self .__lang = current_lang
@@ -277,7 +277,7 @@ class TimeRE(dict):
277
277
def __init__ (self , locale_time = LocaleTime ()):
278
278
"""Initialize instance with non-locale regexes and store LocaleTime object."""
279
279
super (TimeRE ,self ).__init__ ({
280
- 'd' : r"(?P<d>3[0-1]|[0-2]\d|\d| \d)" , #The " \d" option is
280
+ 'd' : r"(?P<d>3[0-1]|[0-2]\d|\d| \d)" , #The " \d" option is
281
281
#to make %c from ANSI
282
282
#C work
283
283
'H' : r"(?P<H>2[0-3]|[0-1]\d|\d)" ,
@@ -299,16 +299,16 @@ def __getitem__(self, fetch):
299
299
return super (TimeRE ,self ).__getitem__ (fetch )
300
300
except KeyError :
301
301
if fetch == 'A' :
302
- self [fetch ] = self .__seqToRE (self .locale_time .f_weekday ,
302
+ self [fetch ] = self .__seqToRE (self .locale_time .f_weekday ,
303
303
fetch )
304
304
elif fetch == 'a' :
305
- self [fetch ] = self .__seqToRE (self .locale_time .a_weekday ,
305
+ self [fetch ] = self .__seqToRE (self .locale_time .a_weekday ,
306
306
fetch )
307
307
elif fetch == 'B' :
308
- self [fetch ] = self .__seqToRE (self .locale_time .f_month [1 :],
308
+ self [fetch ] = self .__seqToRE (self .locale_time .f_month [1 :],
309
309
fetch )
310
310
elif fetch == 'b' :
311
- self [fetch ] = self .__seqToRE (self .locale_time .a_month [1 :],
311
+ self [fetch ] = self .__seqToRE (self .locale_time .a_month [1 :],
312
312
fetch )
313
313
elif fetch == 'c' :
314
314
self [fetch ] = self .pattern (self .locale_time .LC_date_time )
@@ -319,28 +319,28 @@ def __getitem__(self, fetch):
319
319
elif fetch == 'X' :
320
320
self [fetch ] = self .pattern (self .locale_time .LC_time )
321
321
elif fetch == 'Z' :
322
- self [fetch ] = self .__seqToRE (self .locale_time .timezone ,
322
+ self [fetch ] = self .__seqToRE (self .locale_time .timezone ,
323
323
fetch )
324
324
elif fetch == '%' :
325
325
return '%'
326
326
return super (TimeRE ,self ).__getitem__ (fetch )
327
-
327
+
328
328
def __seqToRE (self , to_convert , directive ):
329
329
"""Convert a list to a regex string for matching directive."""
330
330
def sorter (a , b ):
331
331
"""Sort based on length.
332
-
332
+
333
333
Done in case for some strange reason that names in the locale only
334
334
differ by a suffix and thus want the name with the suffix to match
335
335
first.
336
-
336
+
337
337
"""
338
338
try : a_length = len (a )
339
339
except TypeError : a_length = 0
340
340
try : b_length = len (b )
341
341
except TypeError : b_length = 0
342
342
return cmp (b_length , a_length )
343
-
343
+
344
344
to_convert = to_convert [:] #Don't want to change value in-place.
345
345
to_convert .sort (sorter )
346
346
regex = '(?P<%s>' % directive
@@ -357,7 +357,7 @@ def pattern(self, format):
357
357
format = format .replace (whitespace , r'\s*' )
358
358
while format .find ('%' ) != - 1 :
359
359
directive_index = format .index ('%' )+ 1
360
- processed_format = "%s%s%s" % (processed_format ,
360
+ processed_format = "%s%s%s" % (processed_format ,
361
361
format [:directive_index - 1 ],
362
362
self [format [directive_index ]])
363
363
format = format [directive_index + 1 :]
@@ -371,12 +371,12 @@ def compile(self, format):
371
371
372
372
def strptime (data_string , format = "%a %b %d %H:%M:%S %Y" ):
373
373
"""Convert data_string to a time struct based on the format string or re object; will return an re object for format if data_string is False.
374
-
375
- The object passed in for format may either be a re object compiled by
376
- strptime() or a format string. If False is passed in for data_string
377
- then an re object for format will be returned. The re object
374
+
375
+ The object passed in for format may either be a re object compiled by
376
+ strptime() or a format string. If False is passed in for data_string
377
+ then an re object for format will be returned. The re object
378
378
must be used with the same language as used to compile the re object.
379
-
379
+
380
380
"""
381
381
locale_time = LocaleTime ()
382
382
if isinstance (format , type (re_compile ('' ))):
0 commit comments