blob: 635613edcaba6004eb8246b01f0ca3af23cd6c49 [file] [log] [blame]
Guido van Rossum00efe7e2002-07-19 17:04:46 +00001"""Strptime-related classes and functions.
2
3CLASSES:
4 LocaleTime -- Discovers and/or stores locale-specific time information
Barry Warsaw4d895fa2002-09-23 22:46:49 +00005 TimeRE -- Creates regexes for pattern matching a string of text containing
Guido van Rossum00efe7e2002-07-19 17:04:46 +00006 time information as is returned by time.strftime()
7
8FUNCTIONS:
Raymond Hettinger1fdb6332003-03-09 07:44:42 +00009 _getlang -- Figure out what language is being used for the locale
Guido van Rossum00efe7e2002-07-19 17:04:46 +000010 strptime -- Calculates the time struct represented by the passed-in string
11
Raymond Hettinger1fdb6332003-03-09 07:44:42 +000012Requires Python 2.2.1 or higher (mainly because of the use of property()).
Guido van Rossum00efe7e2002-07-19 17:04:46 +000013Can be used in Python 2.2 if the following line is added:
Raymond Hettinger1fdb6332003-03-09 07:44:42 +000014 True = 1; False = 0
Guido van Rossum00efe7e2002-07-19 17:04:46 +000015"""
16import time
17import locale
18import calendar
19from re import compile as re_compile
20from re import IGNORECASE
Raymond Hettinger1fdb6332003-03-09 07:44:42 +000021from datetime import date as datetime_date
Guido van Rossum00efe7e2002-07-19 17:04:46 +000022
Guido van Rossum00efe7e2002-07-19 17:04:46 +000023__author__ = "Brett Cannon"
Raymond Hettinger1fdb6332003-03-09 07:44:42 +000024__email__ = "brett@python.org"
Guido van Rossum00efe7e2002-07-19 17:04:46 +000025
26__all__ = ['strptime']
27
Tim Peters80cebc12003-01-19 04:40:44 +000028def _getlang():
29 # Figure out what the current language is set to.
30 current_lang = locale.getlocale(locale.LC_TIME)[0]
31 if current_lang:
32 return current_lang
33 else:
34 current_lang = locale.getdefaultlocale()[0]
35 if current_lang:
36 return current_lang
37 else:
38 return ''
Barry Warsaw35816e62002-08-29 16:24:50 +000039
Guido van Rossum00efe7e2002-07-19 17:04:46 +000040class LocaleTime(object):
41 """Stores and handles locale-specific information related to time.
42
43 ATTRIBUTES (all read-only after instance creation! Instance variables that
44 store the values have mangled names):
45 f_weekday -- full weekday names (7-item list)
46 a_weekday -- abbreviated weekday names (7-item list)
Tim Peters469cdad2002-08-08 20:19:19 +000047 f_month -- full weekday names (14-item list; dummy value in [0], which
Guido van Rossum00efe7e2002-07-19 17:04:46 +000048 is added by code)
Tim Peters469cdad2002-08-08 20:19:19 +000049 a_month -- abbreviated weekday names (13-item list, dummy value in
Guido van Rossum00efe7e2002-07-19 17:04:46 +000050 [0], which is added by code)
51 am_pm -- AM/PM representation (2-item list)
52 LC_date_time -- format string for date/time representation (string)
53 LC_date -- format string for date representation (string)
54 LC_time -- format string for time representation (string)
Tim Peters469cdad2002-08-08 20:19:19 +000055 timezone -- daylight- and non-daylight-savings timezone representation
56 (3-item list; code tacks on blank item at end for
Guido van Rossum00efe7e2002-07-19 17:04:46 +000057 possible lack of timezone such as UTC)
58 lang -- Language used by instance (string)
Guido van Rossum00efe7e2002-07-19 17:04:46 +000059 """
60
Tim Peters469cdad2002-08-08 20:19:19 +000061 def __init__(self, f_weekday=None, a_weekday=None, f_month=None,
Barry Warsaw35816e62002-08-29 16:24:50 +000062 a_month=None, am_pm=None, LC_date_time=None, LC_time=None,
63 LC_date=None, timezone=None, lang=None):
Guido van Rossum00efe7e2002-07-19 17:04:46 +000064 """Optionally set attributes with passed-in values."""
Barry Warsaw35816e62002-08-29 16:24:50 +000065 if f_weekday is None:
66 self.__f_weekday = None
67 elif len(f_weekday) == 7:
68 self.__f_weekday = list(f_weekday)
Guido van Rossum00efe7e2002-07-19 17:04:46 +000069 else:
70 raise TypeError("full weekday names must be a 7-item sequence")
Barry Warsaw35816e62002-08-29 16:24:50 +000071 if a_weekday is None:
72 self.__a_weekday = None
73 elif len(a_weekday) == 7:
74 self.__a_weekday = list(a_weekday)
Guido van Rossum00efe7e2002-07-19 17:04:46 +000075 else:
76 raise TypeError(
Barry Warsaw35816e62002-08-29 16:24:50 +000077 "abbreviated weekday names must be a 7-item sequence")
78 if f_month is None:
79 self.__f_month = None
Guido van Rossum00efe7e2002-07-19 17:04:46 +000080 elif len(f_month) == 12:
81 self.__f_month = self.__pad(f_month, True)
82 else:
83 raise TypeError("full month names must be a 12-item sequence")
Barry Warsaw35816e62002-08-29 16:24:50 +000084 if a_month is None:
85 self.__a_month = None
Guido van Rossum00efe7e2002-07-19 17:04:46 +000086 elif len(a_month) == 12:
87 self.__a_month = self.__pad(a_month, True)
88 else:
89 raise TypeError(
Barry Warsaw35816e62002-08-29 16:24:50 +000090 "abbreviated month names must be a 12-item sequence")
Guido van Rossum00efe7e2002-07-19 17:04:46 +000091 if am_pm is None:
92 self.__am_pm = None
93 elif len(am_pm) == 2:
94 self.__am_pm = am_pm
95 else:
96 raise TypeError("AM/PM representation must be a 2-item sequence")
97 self.__LC_date_time = LC_date_time
98 self.__LC_time = LC_time
99 self.__LC_date = LC_date
100 self.__timezone = timezone
101 if timezone:
102 if len(timezone) != 2:
103 raise TypeError("timezone names must contain 2 items")
104 else:
105 self.__timezone = self.__pad(timezone, False)
106 self.__lang = lang
107
108 def __pad(self, seq, front):
Barry Warsaw35816e62002-08-29 16:24:50 +0000109 # Add '' to seq to either front (is True), else the back.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000110 seq = list(seq)
Barry Warsaw35816e62002-08-29 16:24:50 +0000111 if front:
112 seq.insert(0, '')
113 else:
114 seq.append('')
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000115 return seq
116
117 def __set_nothing(self, stuff):
Barry Warsaw35816e62002-08-29 16:24:50 +0000118 # Raise TypeError when trying to set an attribute.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000119 raise TypeError("attribute does not support assignment")
120
121 def __get_f_weekday(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000122 # Fetch self.f_weekday.
123 if not self.__f_weekday:
124 self.__calc_weekday()
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000125 return self.__f_weekday
126
127 def __get_a_weekday(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000128 # Fetch self.a_weekday.
129 if not self.__a_weekday:
130 self.__calc_weekday()
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000131 return self.__a_weekday
132
Tim Peters469cdad2002-08-08 20:19:19 +0000133 f_weekday = property(__get_f_weekday, __set_nothing,
Barry Warsaw35816e62002-08-29 16:24:50 +0000134 doc="Full weekday names")
Tim Peters469cdad2002-08-08 20:19:19 +0000135 a_weekday = property(__get_a_weekday, __set_nothing,
Barry Warsaw35816e62002-08-29 16:24:50 +0000136 doc="Abbreviated weekday names")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000137
138 def __get_f_month(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000139 # Fetch self.f_month.
140 if not self.__f_month:
141 self.__calc_month()
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000142 return self.__f_month
143
144 def __get_a_month(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000145 # Fetch self.a_month.
146 if not self.__a_month:
147 self.__calc_month()
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000148 return self.__a_month
149
150 f_month = property(__get_f_month, __set_nothing,
Barry Warsaw35816e62002-08-29 16:24:50 +0000151 doc="Full month names (dummy value at index 0)")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000152 a_month = property(__get_a_month, __set_nothing,
Barry Warsaw35816e62002-08-29 16:24:50 +0000153 doc="Abbreviated month names (dummy value at index 0)")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000154
155 def __get_am_pm(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000156 # Fetch self.am_pm.
157 if not self.__am_pm:
158 self.__calc_am_pm()
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000159 return self.__am_pm
160
161 am_pm = property(__get_am_pm, __set_nothing, doc="AM/PM representation")
162
163 def __get_timezone(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000164 # Fetch self.timezone.
165 if not self.__timezone:
166 self.__calc_timezone()
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000167 return self.__timezone
168
169 timezone = property(__get_timezone, __set_nothing,
170 doc="Timezone representation (dummy value at index 2)")
171
172 def __get_LC_date_time(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000173 # Fetch self.LC_date_time.
174 if not self.__LC_date_time:
175 self.__calc_date_time()
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000176 return self.__LC_date_time
177
178 def __get_LC_date(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000179 # Fetch self.LC_date.
180 if not self.__LC_date:
181 self.__calc_date_time()
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000182 return self.__LC_date
183
184 def __get_LC_time(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000185 # Fetch self.LC_time.
186 if not self.__LC_time:
187 self.__calc_date_time()
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000188 return self.__LC_time
189
Barry Warsaw35816e62002-08-29 16:24:50 +0000190 LC_date_time = property(
191 __get_LC_date_time, __set_nothing,
192 doc=
193 "Format string for locale's date/time representation ('%c' format)")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000194 LC_date = property(__get_LC_date, __set_nothing,
195 doc="Format string for locale's date representation ('%x' format)")
196 LC_time = property(__get_LC_time, __set_nothing,
197 doc="Format string for locale's time representation ('%X' format)")
198
199 def __get_lang(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000200 # Fetch self.lang.
201 if not self.__lang:
202 self.__calc_lang()
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000203 return self.__lang
204
Barry Warsaw35816e62002-08-29 16:24:50 +0000205 lang = property(__get_lang, __set_nothing,
206 doc="Language used for instance")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000207
208 def __calc_weekday(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000209 # Set self.__a_weekday and self.__f_weekday using the calendar
210 # module.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000211 a_weekday = [calendar.day_abbr[i] for i in range(7)]
212 f_weekday = [calendar.day_name[i] for i in range(7)]
Barry Warsaw35816e62002-08-29 16:24:50 +0000213 if not self.__a_weekday:
214 self.__a_weekday = a_weekday
215 if not self.__f_weekday:
216 self.__f_weekday = f_weekday
Tim Peters469cdad2002-08-08 20:19:19 +0000217
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000218 def __calc_month(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000219 # Set self.__f_month and self.__a_month using the calendar module.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000220 a_month = [calendar.month_abbr[i] for i in range(13)]
221 f_month = [calendar.month_name[i] for i in range(13)]
Barry Warsaw35816e62002-08-29 16:24:50 +0000222 if not self.__a_month:
223 self.__a_month = a_month
224 if not self.__f_month:
225 self.__f_month = f_month
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000226
227 def __calc_am_pm(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000228 # Set self.__am_pm by using time.strftime().
Tim Peters469cdad2002-08-08 20:19:19 +0000229
Barry Warsaw35816e62002-08-29 16:24:50 +0000230 # The magic date (1999,3,17,hour,44,55,2,76,0) is not really that
231 # magical; just happened to have used it everywhere else where a
232 # static date was needed.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000233 am_pm = []
234 for hour in (01,22):
235 time_tuple = time.struct_time((1999,3,17,hour,44,55,2,76,0))
236 am_pm.append(time.strftime("%p", time_tuple))
237 self.__am_pm = am_pm
238
239 def __calc_date_time(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000240 # Set self.__date_time, self.__date, & self.__time by using
241 # time.strftime().
Tim Peters469cdad2002-08-08 20:19:19 +0000242
Barry Warsaw35816e62002-08-29 16:24:50 +0000243 # Use (1999,3,17,22,44,55,2,76,0) for magic date because the amount of
244 # overloaded numbers is minimized. The order in which searches for
245 # values within the format string is very important; it eliminates
246 # possible ambiguity for what something represents.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000247 time_tuple = time.struct_time((1999,3,17,22,44,55,2,76,0))
248 date_time = [None, None, None]
249 date_time[0] = time.strftime("%c", time_tuple)
250 date_time[1] = time.strftime("%x", time_tuple)
251 date_time[2] = time.strftime("%X", time_tuple)
252 for offset,directive in ((0,'%c'), (1,'%x'), (2,'%X')):
253 current_format = date_time[offset]
Barry Warsaw4d895fa2002-09-23 22:46:49 +0000254 for old, new in (
255 ('%', '%%'), (self.f_weekday[2], '%A'),
256 (self.f_month[3], '%B'), (self.a_weekday[2], '%a'),
257 (self.a_month[3], '%b'), (self.am_pm[1], '%p'),
258 (self.timezone[0], '%Z'), (self.timezone[1], '%Z'),
259 ('1999', '%Y'), ('99', '%y'), ('22', '%H'),
260 ('44', '%M'), ('55', '%S'), ('76', '%j'),
261 ('17', '%d'), ('03', '%m'), ('3', '%m'),
262 # '3' needed for when no leading zero.
263 ('2', '%w'), ('10', '%I')):
Jack Jansen62fe7552003-01-15 22:59:39 +0000264 # Must deal with possible lack of locale info
265 # manifesting itself as the empty string (e.g., Swedish's
266 # lack of AM/PM info) or a platform returning a tuple of empty
267 # strings (e.g., MacOS 9 having timezone as ('','')).
268 if old:
Barry Warsaw4d895fa2002-09-23 22:46:49 +0000269 current_format = current_format.replace(old, new)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000270 time_tuple = time.struct_time((1999,1,3,1,1,1,6,3,0))
271 if time.strftime(directive, time_tuple).find('00'):
272 U_W = '%U'
273 else:
274 U_W = '%W'
275 date_time[offset] = current_format.replace('11', U_W)
Barry Warsaw35816e62002-08-29 16:24:50 +0000276 if not self.__LC_date_time:
277 self.__LC_date_time = date_time[0]
278 if not self.__LC_date:
279 self.__LC_date = date_time[1]
280 if not self.__LC_time:
281 self.__LC_time = date_time[2]
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000282
283 def __calc_timezone(self):
Barry Warsaw35816e62002-08-29 16:24:50 +0000284 # Set self.__timezone by using time.tzname.
285 #
Brett Cannon172d9ef2003-05-11 06:23:36 +0000286 # Empty string used for matching when timezone is not used/needed.
Brett Cannonabe8eb02003-05-13 20:28:15 +0000287 try:
288 time.tzset()
289 except AttributeError:
290 pass
Brett Cannon172d9ef2003-05-11 06:23:36 +0000291 time_zones = ["UTC", "GMT"]
292 if time.daylight:
293 time_zones.extend(time.tzname)
294 else:
295 time_zones.append(time.tzname[0])
296 self.__timezone = self.__pad(time_zones, 0)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000297
298 def __calc_lang(self):
Tim Peters80cebc12003-01-19 04:40:44 +0000299 # Set self.__lang by using __getlang().
300 self.__lang = _getlang()
301
Barry Warsaw35816e62002-08-29 16:24:50 +0000302
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000303
304class TimeRE(dict):
305 """Handle conversion from format directives to regexes."""
306
307 def __init__(self, locale_time=LocaleTime()):
Barry Warsaw35816e62002-08-29 16:24:50 +0000308 """Init inst with non-locale regexes and store LocaleTime object."""
Neal Norwitz5efc50d2002-12-30 22:23:12 +0000309 #XXX: Does 'Y' need to worry about having less or more than 4 digits?
310 base = super(TimeRE, self)
311 base.__init__({
Barry Warsaw35816e62002-08-29 16:24:50 +0000312 # The " \d" option is to make %c from ANSI C work
Neal Norwitz5efc50d2002-12-30 22:23:12 +0000313 'd': r"(?P<d>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])",
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000314 'H': r"(?P<H>2[0-3]|[0-1]\d|\d)",
Neal Norwitz5efc50d2002-12-30 22:23:12 +0000315 'I': r"(?P<I>1[0-2]|0[1-9]|[1-9])",
316 'j': r"(?P<j>36[0-6]|3[0-5]\d|[1-2]\d\d|0[1-9]\d|00[1-9]|[1-9]\d|0[1-9]|[1-9])",
317 'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])",
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000318 'M': r"(?P<M>[0-5]\d|\d)",
319 'S': r"(?P<S>6[0-1]|[0-5]\d|\d)",
320 'U': r"(?P<U>5[0-3]|[0-4]\d|\d)",
321 'w': r"(?P<w>[0-6])",
Neal Norwitz5efc50d2002-12-30 22:23:12 +0000322 # W is set below by using 'U'
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000323 'y': r"(?P<y>\d\d)",
324 'Y': r"(?P<Y>\d\d\d\d)"})
Neal Norwitz5efc50d2002-12-30 22:23:12 +0000325 base.__setitem__('W', base.__getitem__('U'))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000326 self.locale_time = locale_time
327
328 def __getitem__(self, fetch):
329 """Try to fetch regex; if it does not exist, construct it."""
330 try:
Barry Warsaw4d895fa2002-09-23 22:46:49 +0000331 return super(TimeRE, self).__getitem__(fetch)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000332 except KeyError:
Barry Warsaw4d895fa2002-09-23 22:46:49 +0000333 constructors = {
334 'A': lambda: self.__seqToRE(self.locale_time.f_weekday, fetch),
335 'a': lambda: self.__seqToRE(self.locale_time.a_weekday, fetch),
336 'B': lambda: self.__seqToRE(self.locale_time.f_month[1:],
337 fetch),
338 'b': lambda: self.__seqToRE(self.locale_time.a_month[1:],
339 fetch),
340 'c': lambda: self.pattern(self.locale_time.LC_date_time),
341 'p': lambda: self.__seqToRE(self.locale_time.am_pm, fetch),
342 'x': lambda: self.pattern(self.locale_time.LC_date),
343 'X': lambda: self.pattern(self.locale_time.LC_time),
344 'Z': lambda: self.__seqToRE(self.locale_time.timezone, fetch),
345 '%': lambda: '%',
346 }
347 if fetch in constructors:
348 self[fetch] = constructors[fetch]()
349 return self[fetch]
350 else:
351 raise
Tim Peters469cdad2002-08-08 20:19:19 +0000352
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000353 def __seqToRE(self, to_convert, directive):
Jack Jansen62fe7552003-01-15 22:59:39 +0000354 """Convert a list to a regex string for matching a directive."""
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000355 def sorter(a, b):
356 """Sort based on length.
Tim Peters469cdad2002-08-08 20:19:19 +0000357
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000358 Done in case for some strange reason that names in the locale only
359 differ by a suffix and thus want the name with the suffix to match
360 first.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000361 """
Barry Warsaw35816e62002-08-29 16:24:50 +0000362 try:
363 a_length = len(a)
364 except TypeError:
365 a_length = 0
366 try:
367 b_length = len(b)
368 except TypeError:
369 b_length = 0
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000370 return cmp(b_length, a_length)
Tim Peters469cdad2002-08-08 20:19:19 +0000371
Barry Warsaw35816e62002-08-29 16:24:50 +0000372 to_convert = to_convert[:] # Don't want to change value in-place.
Jack Jansen62fe7552003-01-15 22:59:39 +0000373 for value in to_convert:
374 if value != '':
375 break
376 else:
377 return ''
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000378 to_convert.sort(sorter)
Barry Warsaw4d895fa2002-09-23 22:46:49 +0000379 regex = '|'.join(to_convert)
380 regex = '(?P<%s>%s' % (directive, regex)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000381 return '%s)' % regex
382
383 def pattern(self, format):
Brett Cannon1e91d8e2003-04-19 04:00:56 +0000384 """Return re pattern for the format string.
Tim Peters0eadaac2003-04-24 16:02:54 +0000385
Brett Cannon1e91d8e2003-04-19 04:00:56 +0000386 Need to make sure that any characters that might be interpreted as
387 regex syntax is escaped.
Tim Peters0eadaac2003-04-24 16:02:54 +0000388
Brett Cannon1e91d8e2003-04-19 04:00:56 +0000389 """
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000390 processed_format = ''
Brett Cannon1e91d8e2003-04-19 04:00:56 +0000391 # The sub() call escapes all characters that might be misconstrued
392 # as regex syntax.
393 regex_chars = re_compile(r"([\\.^$*+?{}\[\]|])")
394 format = regex_chars.sub(r"\\\1", format)
Tim Peters80cebc12003-01-19 04:40:44 +0000395 whitespace_replacement = re_compile('\s+')
396 format = whitespace_replacement.sub('\s*', format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000397 while format.find('%') != -1:
398 directive_index = format.index('%')+1
Tim Peters469cdad2002-08-08 20:19:19 +0000399 processed_format = "%s%s%s" % (processed_format,
Barry Warsaw35816e62002-08-29 16:24:50 +0000400 format[:directive_index-1],
401 self[format[directive_index]])
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000402 format = format[directive_index+1:]
403 return "%s%s" % (processed_format, format)
404
405 def compile(self, format):
406 """Return a compiled re object for the format string."""
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000407 return re_compile(self.pattern(format), IGNORECASE)
408
Tim Peters80cebc12003-01-19 04:40:44 +0000409# Cached TimeRE; probably only need one instance ever so cache it for performance
410_locale_cache = TimeRE()
411# Cached regex objects; same reason as for TimeRE cache
412_regex_cache = dict()
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000413
414def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
Tim Peters08e54272003-01-18 03:53:49 +0000415 """Return a time struct based on the input data and the format string."""
Tim Peters80cebc12003-01-19 04:40:44 +0000416 global _locale_cache
417 global _regex_cache
418 locale_time = _locale_cache.locale_time
419 # If the language changes, caches are invalidated, so clear them
420 if locale_time.lang != _getlang():
421 _locale_cache = TimeRE()
422 _regex_cache.clear()
423 format_regex = _regex_cache.get(format)
424 if not format_regex:
425 # Limit regex cache size to prevent major bloating of the module;
426 # The value 5 is arbitrary
427 if len(_regex_cache) > 5:
428 _regex_cache.clear()
429 format_regex = _locale_cache.compile(format)
430 _regex_cache[format] = format_regex
431 found = format_regex.match(data_string)
Tim Peters08e54272003-01-18 03:53:49 +0000432 if not found:
Raymond Hettinger4a6302b2003-07-13 01:31:38 +0000433 raise ValueError("time data did not match format: data=%s fmt=%s" %
434 (data_string, format))
Brett Cannon2b6dfec2003-04-28 21:30:13 +0000435 if len(data_string) != found.end():
436 raise ValueError("unconverted data remains: %s" %
437 data_string[found.end():])
Tim Peters08e54272003-01-18 03:53:49 +0000438 year = 1900
439 month = day = 1
440 hour = minute = second = 0
441 tz = -1
Raymond Hettinger1fdb6332003-03-09 07:44:42 +0000442 # weekday and julian defaulted to -1 so as to signal need to calculate values
Tim Peters08e54272003-01-18 03:53:49 +0000443 weekday = julian = -1
444 found_dict = found.groupdict()
445 for group_key in found_dict.iterkeys():
446 if group_key == 'y':
447 year = int(found_dict['y'])
448 # Open Group specification for strptime() states that a %y
449 #value in the range of [00, 68] is in the century 2000, while
450 #[69,99] is in the century 1900
451 if year <= 68:
452 year += 2000
453 else:
454 year += 1900
455 elif group_key == 'Y':
456 year = int(found_dict['Y'])
457 elif group_key == 'm':
458 month = int(found_dict['m'])
459 elif group_key == 'B':
460 month = _insensitiveindex(locale_time.f_month, found_dict['B'])
461 elif group_key == 'b':
462 month = _insensitiveindex(locale_time.a_month, found_dict['b'])
463 elif group_key == 'd':
464 day = int(found_dict['d'])
Neal Norwitz77290f22003-06-29 04:16:49 +0000465 elif group_key == 'H':
Tim Peters08e54272003-01-18 03:53:49 +0000466 hour = int(found_dict['H'])
467 elif group_key == 'I':
468 hour = int(found_dict['I'])
469 ampm = found_dict.get('p', '').lower()
470 # If there was no AM/PM indicator, we'll treat this like AM
471 if ampm in ('', locale_time.am_pm[0].lower()):
472 # We're in AM so the hour is correct unless we're
473 # looking at 12 midnight.
474 # 12 midnight == 12 AM == hour 0
475 if hour == 12:
476 hour = 0
477 elif ampm == locale_time.am_pm[1].lower():
478 # We're in PM so we need to add 12 to the hour unless
479 # we're looking at 12 noon.
480 # 12 noon == 12 PM == hour 12
481 if hour != 12:
482 hour += 12
483 elif group_key == 'M':
484 minute = int(found_dict['M'])
485 elif group_key == 'S':
486 second = int(found_dict['S'])
487 elif group_key == 'A':
488 weekday = _insensitiveindex(locale_time.f_weekday,
489 found_dict['A'])
490 elif group_key == 'a':
491 weekday = _insensitiveindex(locale_time.a_weekday,
492 found_dict['a'])
493 elif group_key == 'w':
494 weekday = int(found_dict['w'])
495 if weekday == 0:
496 weekday = 6
497 else:
498 weekday -= 1
499 elif group_key == 'j':
500 julian = int(found_dict['j'])
501 elif group_key == 'Z':
Brett Cannon172d9ef2003-05-11 06:23:36 +0000502 # Since -1 is default value only need to worry about setting tz if
503 # it can be something other than -1.
Tim Peters08e54272003-01-18 03:53:49 +0000504 found_zone = found_dict['Z'].lower()
Brett Cannoncde22002003-07-03 19:59:57 +0000505 if locale_time.timezone[0] == locale_time.timezone[1] and \
506 time.daylight:
Tim Peters08e54272003-01-18 03:53:49 +0000507 pass #Deals with bad locale setup where timezone info is
508 # the same; first found on FreeBSD 4.4.
Brett Cannon172d9ef2003-05-11 06:23:36 +0000509 elif found_zone in ("utc", "gmt"):
Tim Peters08e54272003-01-18 03:53:49 +0000510 tz = 0
Tim Peters08e54272003-01-18 03:53:49 +0000511 elif locale_time.timezone[2].lower() == found_zone:
Brett Cannon172d9ef2003-05-11 06:23:36 +0000512 tz = 0
Brett Cannoncde22002003-07-03 19:59:57 +0000513 elif time.daylight and \
514 locale_time.timezone[3].lower() == found_zone:
515 tz = 1
Brett Cannon172d9ef2003-05-11 06:23:36 +0000516
Raymond Hettinger1fdb6332003-03-09 07:44:42 +0000517 # Cannot pre-calculate datetime_date() since can change in Julian
518 #calculation and thus could have different value for the day of the week
519 #calculation
Tim Peters08e54272003-01-18 03:53:49 +0000520 if julian == -1:
Raymond Hettinger1fdb6332003-03-09 07:44:42 +0000521 # Need to add 1 to result since first day of the year is 1, not 0.
522 julian = datetime_date(year, month, day).toordinal() - \
523 datetime_date(year, 1, 1).toordinal() + 1
524 else: # Assume that if they bothered to include Julian day it will
Tim Peters08e54272003-01-18 03:53:49 +0000525 #be accurate
Raymond Hettinger1fdb6332003-03-09 07:44:42 +0000526 datetime_result = datetime_date.fromordinal((julian - 1) + datetime_date(year, 1, 1).toordinal())
527 year = datetime_result.year
528 month = datetime_result.month
529 day = datetime_result.day
Tim Peters08e54272003-01-18 03:53:49 +0000530 if weekday == -1:
Raymond Hettinger1fdb6332003-03-09 07:44:42 +0000531 weekday = datetime_date(year, month, day).weekday()
Tim Peters08e54272003-01-18 03:53:49 +0000532 return time.struct_time((year, month, day,
533 hour, minute, second,
534 weekday, julian, tz))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000535
Barry Warsaw4d895fa2002-09-23 22:46:49 +0000536def _insensitiveindex(lst, findme):
537 # Perform a case-insensitive index search.
538
539 #XXX <bc>: If LocaleTime is not exposed, then consider removing this and
540 # just lowercase when LocaleTime sets its vars and lowercasing
541 # search values.
542 findme = findme.lower()
543 for key,item in enumerate(lst):
544 if item.lower() == findme:
545 return key
546 else:
547 raise ValueError("value not in list")