Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/python27/lib64/python2....
File: _strptime.py
"""Strptime-related classes and functions.
[0] Fix | Delete
[1] Fix | Delete
CLASSES:
[2] Fix | Delete
LocaleTime -- Discovers and stores locale-specific time information
[3] Fix | Delete
TimeRE -- Creates regexes for pattern matching a string of text containing
[4] Fix | Delete
time information
[5] Fix | Delete
[6] Fix | Delete
FUNCTIONS:
[7] Fix | Delete
_getlang -- Figure out what language is being used for the locale
[8] Fix | Delete
strptime -- Calculates the time struct represented by the passed-in string
[9] Fix | Delete
[10] Fix | Delete
"""
[11] Fix | Delete
import time
[12] Fix | Delete
import locale
[13] Fix | Delete
import calendar
[14] Fix | Delete
from re import compile as re_compile
[15] Fix | Delete
from re import IGNORECASE
[16] Fix | Delete
from re import escape as re_escape
[17] Fix | Delete
from datetime import date as datetime_date
[18] Fix | Delete
try:
[19] Fix | Delete
from thread import allocate_lock as _thread_allocate_lock
[20] Fix | Delete
except:
[21] Fix | Delete
from dummy_thread import allocate_lock as _thread_allocate_lock
[22] Fix | Delete
[23] Fix | Delete
__all__ = []
[24] Fix | Delete
[25] Fix | Delete
def _getlang():
[26] Fix | Delete
# Figure out what the current language is set to.
[27] Fix | Delete
return locale.getlocale(locale.LC_TIME)
[28] Fix | Delete
[29] Fix | Delete
class LocaleTime(object):
[30] Fix | Delete
"""Stores and handles locale-specific information related to time.
[31] Fix | Delete
[32] Fix | Delete
ATTRIBUTES:
[33] Fix | Delete
f_weekday -- full weekday names (7-item list)
[34] Fix | Delete
a_weekday -- abbreviated weekday names (7-item list)
[35] Fix | Delete
f_month -- full month names (13-item list; dummy value in [0], which
[36] Fix | Delete
is added by code)
[37] Fix | Delete
a_month -- abbreviated month names (13-item list, dummy value in
[38] Fix | Delete
[0], which is added by code)
[39] Fix | Delete
am_pm -- AM/PM representation (2-item list)
[40] Fix | Delete
LC_date_time -- format string for date/time representation (string)
[41] Fix | Delete
LC_date -- format string for date representation (string)
[42] Fix | Delete
LC_time -- format string for time representation (string)
[43] Fix | Delete
timezone -- daylight- and non-daylight-savings timezone representation
[44] Fix | Delete
(2-item list of sets)
[45] Fix | Delete
lang -- Language used by instance (2-item tuple)
[46] Fix | Delete
"""
[47] Fix | Delete
[48] Fix | Delete
def __init__(self):
[49] Fix | Delete
"""Set all attributes.
[50] Fix | Delete
[51] Fix | Delete
Order of methods called matters for dependency reasons.
[52] Fix | Delete
[53] Fix | Delete
The locale language is set at the offset and then checked again before
[54] Fix | Delete
exiting. This is to make sure that the attributes were not set with a
[55] Fix | Delete
mix of information from more than one locale. This would most likely
[56] Fix | Delete
happen when using threads where one thread calls a locale-dependent
[57] Fix | Delete
function while another thread changes the locale while the function in
[58] Fix | Delete
the other thread is still running. Proper coding would call for
[59] Fix | Delete
locks to prevent changing the locale while locale-dependent code is
[60] Fix | Delete
running. The check here is done in case someone does not think about
[61] Fix | Delete
doing this.
[62] Fix | Delete
[63] Fix | Delete
Only other possible issue is if someone changed the timezone and did
[64] Fix | Delete
not call tz.tzset . That is an issue for the programmer, though,
[65] Fix | Delete
since changing the timezone is worthless without that call.
[66] Fix | Delete
[67] Fix | Delete
"""
[68] Fix | Delete
self.lang = _getlang()
[69] Fix | Delete
self.__calc_weekday()
[70] Fix | Delete
self.__calc_month()
[71] Fix | Delete
self.__calc_am_pm()
[72] Fix | Delete
self.__calc_timezone()
[73] Fix | Delete
self.__calc_date_time()
[74] Fix | Delete
if _getlang() != self.lang:
[75] Fix | Delete
raise ValueError("locale changed during initialization")
[76] Fix | Delete
if time.tzname != self.tzname or time.daylight != self.daylight:
[77] Fix | Delete
raise ValueError("timezone changed during initialization")
[78] Fix | Delete
[79] Fix | Delete
def __pad(self, seq, front):
[80] Fix | Delete
# Add '' to seq to either the front (is True), else the back.
[81] Fix | Delete
seq = list(seq)
[82] Fix | Delete
if front:
[83] Fix | Delete
seq.insert(0, '')
[84] Fix | Delete
else:
[85] Fix | Delete
seq.append('')
[86] Fix | Delete
return seq
[87] Fix | Delete
[88] Fix | Delete
def __calc_weekday(self):
[89] Fix | Delete
# Set self.a_weekday and self.f_weekday using the calendar
[90] Fix | Delete
# module.
[91] Fix | Delete
a_weekday = [calendar.day_abbr[i].lower() for i in range(7)]
[92] Fix | Delete
f_weekday = [calendar.day_name[i].lower() for i in range(7)]
[93] Fix | Delete
self.a_weekday = a_weekday
[94] Fix | Delete
self.f_weekday = f_weekday
[95] Fix | Delete
[96] Fix | Delete
def __calc_month(self):
[97] Fix | Delete
# Set self.f_month and self.a_month using the calendar module.
[98] Fix | Delete
a_month = [calendar.month_abbr[i].lower() for i in range(13)]
[99] Fix | Delete
f_month = [calendar.month_name[i].lower() for i in range(13)]
[100] Fix | Delete
self.a_month = a_month
[101] Fix | Delete
self.f_month = f_month
[102] Fix | Delete
[103] Fix | Delete
def __calc_am_pm(self):
[104] Fix | Delete
# Set self.am_pm by using time.strftime().
[105] Fix | Delete
[106] Fix | Delete
# The magic date (1999,3,17,hour,44,55,2,76,0) is not really that
[107] Fix | Delete
# magical; just happened to have used it everywhere else where a
[108] Fix | Delete
# static date was needed.
[109] Fix | Delete
am_pm = []
[110] Fix | Delete
for hour in (01,22):
[111] Fix | Delete
time_tuple = time.struct_time((1999,3,17,hour,44,55,2,76,0))
[112] Fix | Delete
am_pm.append(time.strftime("%p", time_tuple).lower())
[113] Fix | Delete
self.am_pm = am_pm
[114] Fix | Delete
[115] Fix | Delete
def __calc_date_time(self):
[116] Fix | Delete
# Set self.date_time, self.date, & self.time by using
[117] Fix | Delete
# time.strftime().
[118] Fix | Delete
[119] Fix | Delete
# Use (1999,3,17,22,44,55,2,76,0) for magic date because the amount of
[120] Fix | Delete
# overloaded numbers is minimized. The order in which searches for
[121] Fix | Delete
# values within the format string is very important; it eliminates
[122] Fix | Delete
# possible ambiguity for what something represents.
[123] Fix | Delete
time_tuple = time.struct_time((1999,3,17,22,44,55,2,76,0))
[124] Fix | Delete
date_time = [None, None, None]
[125] Fix | Delete
date_time[0] = time.strftime("%c", time_tuple).lower()
[126] Fix | Delete
date_time[1] = time.strftime("%x", time_tuple).lower()
[127] Fix | Delete
date_time[2] = time.strftime("%X", time_tuple).lower()
[128] Fix | Delete
replacement_pairs = [('%', '%%'), (self.f_weekday[2], '%A'),
[129] Fix | Delete
(self.f_month[3], '%B'), (self.a_weekday[2], '%a'),
[130] Fix | Delete
(self.a_month[3], '%b'), (self.am_pm[1], '%p'),
[131] Fix | Delete
('1999', '%Y'), ('99', '%y'), ('22', '%H'),
[132] Fix | Delete
('44', '%M'), ('55', '%S'), ('76', '%j'),
[133] Fix | Delete
('17', '%d'), ('03', '%m'), ('3', '%m'),
[134] Fix | Delete
# '3' needed for when no leading zero.
[135] Fix | Delete
('2', '%w'), ('10', '%I')]
[136] Fix | Delete
replacement_pairs.extend([(tz, "%Z") for tz_values in self.timezone
[137] Fix | Delete
for tz in tz_values])
[138] Fix | Delete
for offset,directive in ((0,'%c'), (1,'%x'), (2,'%X')):
[139] Fix | Delete
current_format = date_time[offset]
[140] Fix | Delete
for old, new in replacement_pairs:
[141] Fix | Delete
# Must deal with possible lack of locale info
[142] Fix | Delete
# manifesting itself as the empty string (e.g., Swedish's
[143] Fix | Delete
# lack of AM/PM info) or a platform returning a tuple of empty
[144] Fix | Delete
# strings (e.g., MacOS 9 having timezone as ('','')).
[145] Fix | Delete
if old:
[146] Fix | Delete
current_format = current_format.replace(old, new)
[147] Fix | Delete
# If %W is used, then Sunday, 2005-01-03 will fall on week 0 since
[148] Fix | Delete
# 2005-01-03 occurs before the first Monday of the year. Otherwise
[149] Fix | Delete
# %U is used.
[150] Fix | Delete
time_tuple = time.struct_time((1999,1,3,1,1,1,6,3,0))
[151] Fix | Delete
if '00' in time.strftime(directive, time_tuple):
[152] Fix | Delete
U_W = '%W'
[153] Fix | Delete
else:
[154] Fix | Delete
U_W = '%U'
[155] Fix | Delete
date_time[offset] = current_format.replace('11', U_W)
[156] Fix | Delete
self.LC_date_time = date_time[0]
[157] Fix | Delete
self.LC_date = date_time[1]
[158] Fix | Delete
self.LC_time = date_time[2]
[159] Fix | Delete
[160] Fix | Delete
def __calc_timezone(self):
[161] Fix | Delete
# Set self.timezone by using time.tzname.
[162] Fix | Delete
# Do not worry about possibility of time.tzname[0] == time.tzname[1]
[163] Fix | Delete
# and time.daylight; handle that in strptime.
[164] Fix | Delete
try:
[165] Fix | Delete
time.tzset()
[166] Fix | Delete
except AttributeError:
[167] Fix | Delete
pass
[168] Fix | Delete
self.tzname = time.tzname
[169] Fix | Delete
self.daylight = time.daylight
[170] Fix | Delete
no_saving = frozenset(["utc", "gmt", self.tzname[0].lower()])
[171] Fix | Delete
if self.daylight:
[172] Fix | Delete
has_saving = frozenset([self.tzname[1].lower()])
[173] Fix | Delete
else:
[174] Fix | Delete
has_saving = frozenset()
[175] Fix | Delete
self.timezone = (no_saving, has_saving)
[176] Fix | Delete
[177] Fix | Delete
[178] Fix | Delete
class TimeRE(dict):
[179] Fix | Delete
"""Handle conversion from format directives to regexes."""
[180] Fix | Delete
[181] Fix | Delete
def __init__(self, locale_time=None):
[182] Fix | Delete
"""Create keys/values.
[183] Fix | Delete
[184] Fix | Delete
Order of execution is important for dependency reasons.
[185] Fix | Delete
[186] Fix | Delete
"""
[187] Fix | Delete
if locale_time:
[188] Fix | Delete
self.locale_time = locale_time
[189] Fix | Delete
else:
[190] Fix | Delete
self.locale_time = LocaleTime()
[191] Fix | Delete
base = super(TimeRE, self)
[192] Fix | Delete
base.__init__({
[193] Fix | Delete
# The " \d" part of the regex is to make %c from ANSI C work
[194] Fix | Delete
'd': r"(?P<d>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])",
[195] Fix | Delete
'f': r"(?P<f>[0-9]{1,6})",
[196] Fix | Delete
'H': r"(?P<H>2[0-3]|[0-1]\d|\d)",
[197] Fix | Delete
'I': r"(?P<I>1[0-2]|0[1-9]|[1-9])",
[198] Fix | Delete
'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])",
[199] Fix | Delete
'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])",
[200] Fix | Delete
'M': r"(?P<M>[0-5]\d|\d)",
[201] Fix | Delete
'S': r"(?P<S>6[0-1]|[0-5]\d|\d)",
[202] Fix | Delete
'U': r"(?P<U>5[0-3]|[0-4]\d|\d)",
[203] Fix | Delete
'w': r"(?P<w>[0-6])",
[204] Fix | Delete
# W is set below by using 'U'
[205] Fix | Delete
'y': r"(?P<y>\d\d)",
[206] Fix | Delete
#XXX: Does 'Y' need to worry about having less or more than
[207] Fix | Delete
# 4 digits?
[208] Fix | Delete
'Y': r"(?P<Y>\d\d\d\d)",
[209] Fix | Delete
'A': self.__seqToRE(self.locale_time.f_weekday, 'A'),
[210] Fix | Delete
'a': self.__seqToRE(self.locale_time.a_weekday, 'a'),
[211] Fix | Delete
'B': self.__seqToRE(self.locale_time.f_month[1:], 'B'),
[212] Fix | Delete
'b': self.__seqToRE(self.locale_time.a_month[1:], 'b'),
[213] Fix | Delete
'p': self.__seqToRE(self.locale_time.am_pm, 'p'),
[214] Fix | Delete
'Z': self.__seqToRE((tz for tz_names in self.locale_time.timezone
[215] Fix | Delete
for tz in tz_names),
[216] Fix | Delete
'Z'),
[217] Fix | Delete
'%': '%'})
[218] Fix | Delete
base.__setitem__('W', base.__getitem__('U').replace('U', 'W'))
[219] Fix | Delete
base.__setitem__('c', self.pattern(self.locale_time.LC_date_time))
[220] Fix | Delete
base.__setitem__('x', self.pattern(self.locale_time.LC_date))
[221] Fix | Delete
base.__setitem__('X', self.pattern(self.locale_time.LC_time))
[222] Fix | Delete
[223] Fix | Delete
def __seqToRE(self, to_convert, directive):
[224] Fix | Delete
"""Convert a list to a regex string for matching a directive.
[225] Fix | Delete
[226] Fix | Delete
Want possible matching values to be from longest to shortest. This
[227] Fix | Delete
prevents the possibility of a match occurring for a value that also
[228] Fix | Delete
a substring of a larger value that should have matched (e.g., 'abc'
[229] Fix | Delete
matching when 'abcdef' should have been the match).
[230] Fix | Delete
[231] Fix | Delete
"""
[232] Fix | Delete
to_convert = sorted(to_convert, key=len, reverse=True)
[233] Fix | Delete
for value in to_convert:
[234] Fix | Delete
if value != '':
[235] Fix | Delete
break
[236] Fix | Delete
else:
[237] Fix | Delete
return ''
[238] Fix | Delete
regex = '|'.join(re_escape(stuff) for stuff in to_convert)
[239] Fix | Delete
regex = '(?P<%s>%s' % (directive, regex)
[240] Fix | Delete
return '%s)' % regex
[241] Fix | Delete
[242] Fix | Delete
def pattern(self, format):
[243] Fix | Delete
"""Return regex pattern for the format string.
[244] Fix | Delete
[245] Fix | Delete
Need to make sure that any characters that might be interpreted as
[246] Fix | Delete
regex syntax are escaped.
[247] Fix | Delete
[248] Fix | Delete
"""
[249] Fix | Delete
processed_format = ''
[250] Fix | Delete
# The sub() call escapes all characters that might be misconstrued
[251] Fix | Delete
# as regex syntax. Cannot use re.escape since we have to deal with
[252] Fix | Delete
# format directives (%m, etc.).
[253] Fix | Delete
regex_chars = re_compile(r"([\\.^$*+?\(\){}\[\]|])")
[254] Fix | Delete
format = regex_chars.sub(r"\\\1", format)
[255] Fix | Delete
whitespace_replacement = re_compile(r'\s+')
[256] Fix | Delete
format = whitespace_replacement.sub(r'\\s+', format)
[257] Fix | Delete
while '%' in format:
[258] Fix | Delete
directive_index = format.index('%')+1
[259] Fix | Delete
processed_format = "%s%s%s" % (processed_format,
[260] Fix | Delete
format[:directive_index-1],
[261] Fix | Delete
self[format[directive_index]])
[262] Fix | Delete
format = format[directive_index+1:]
[263] Fix | Delete
return "%s%s" % (processed_format, format)
[264] Fix | Delete
[265] Fix | Delete
def compile(self, format):
[266] Fix | Delete
"""Return a compiled re object for the format string."""
[267] Fix | Delete
return re_compile(self.pattern(format), IGNORECASE)
[268] Fix | Delete
[269] Fix | Delete
_cache_lock = _thread_allocate_lock()
[270] Fix | Delete
# DO NOT modify _TimeRE_cache or _regex_cache without acquiring the cache lock
[271] Fix | Delete
# first!
[272] Fix | Delete
_TimeRE_cache = TimeRE()
[273] Fix | Delete
_CACHE_MAX_SIZE = 5 # Max number of regexes stored in _regex_cache
[274] Fix | Delete
_regex_cache = {}
[275] Fix | Delete
[276] Fix | Delete
def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon):
[277] Fix | Delete
"""Calculate the Julian day based on the year, week of the year, and day of
[278] Fix | Delete
the week, with week_start_day representing whether the week of the year
[279] Fix | Delete
assumes the week starts on Sunday or Monday (6 or 0)."""
[280] Fix | Delete
first_weekday = datetime_date(year, 1, 1).weekday()
[281] Fix | Delete
# If we are dealing with the %U directive (week starts on Sunday), it's
[282] Fix | Delete
# easier to just shift the view to Sunday being the first day of the
[283] Fix | Delete
# week.
[284] Fix | Delete
if not week_starts_Mon:
[285] Fix | Delete
first_weekday = (first_weekday + 1) % 7
[286] Fix | Delete
day_of_week = (day_of_week + 1) % 7
[287] Fix | Delete
# Need to watch out for a week 0 (when the first day of the year is not
[288] Fix | Delete
# the same as that specified by %U or %W).
[289] Fix | Delete
week_0_length = (7 - first_weekday) % 7
[290] Fix | Delete
if week_of_year == 0:
[291] Fix | Delete
return 1 + day_of_week - first_weekday
[292] Fix | Delete
else:
[293] Fix | Delete
days_to_week = week_0_length + (7 * (week_of_year - 1))
[294] Fix | Delete
return 1 + days_to_week + day_of_week
[295] Fix | Delete
[296] Fix | Delete
[297] Fix | Delete
def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
[298] Fix | Delete
"""Return a time struct based on the input string and the format string."""
[299] Fix | Delete
global _TimeRE_cache, _regex_cache
[300] Fix | Delete
with _cache_lock:
[301] Fix | Delete
locale_time = _TimeRE_cache.locale_time
[302] Fix | Delete
if (_getlang() != locale_time.lang or
[303] Fix | Delete
time.tzname != locale_time.tzname or
[304] Fix | Delete
time.daylight != locale_time.daylight):
[305] Fix | Delete
_TimeRE_cache = TimeRE()
[306] Fix | Delete
_regex_cache.clear()
[307] Fix | Delete
locale_time = _TimeRE_cache.locale_time
[308] Fix | Delete
if len(_regex_cache) > _CACHE_MAX_SIZE:
[309] Fix | Delete
_regex_cache.clear()
[310] Fix | Delete
format_regex = _regex_cache.get(format)
[311] Fix | Delete
if not format_regex:
[312] Fix | Delete
try:
[313] Fix | Delete
format_regex = _TimeRE_cache.compile(format)
[314] Fix | Delete
# KeyError raised when a bad format is found; can be specified as
[315] Fix | Delete
# \\, in which case it was a stray % but with a space after it
[316] Fix | Delete
except KeyError, err:
[317] Fix | Delete
bad_directive = err.args[0]
[318] Fix | Delete
if bad_directive == "\\":
[319] Fix | Delete
bad_directive = "%"
[320] Fix | Delete
del err
[321] Fix | Delete
raise ValueError("'%s' is a bad directive in format '%s'" %
[322] Fix | Delete
(bad_directive, format))
[323] Fix | Delete
# IndexError only occurs when the format string is "%"
[324] Fix | Delete
except IndexError:
[325] Fix | Delete
raise ValueError("stray %% in format '%s'" % format)
[326] Fix | Delete
_regex_cache[format] = format_regex
[327] Fix | Delete
found = format_regex.match(data_string)
[328] Fix | Delete
if not found:
[329] Fix | Delete
raise ValueError("time data %r does not match format %r" %
[330] Fix | Delete
(data_string, format))
[331] Fix | Delete
if len(data_string) != found.end():
[332] Fix | Delete
raise ValueError("unconverted data remains: %s" %
[333] Fix | Delete
data_string[found.end():])
[334] Fix | Delete
[335] Fix | Delete
year = None
[336] Fix | Delete
month = day = 1
[337] Fix | Delete
hour = minute = second = fraction = 0
[338] Fix | Delete
tz = -1
[339] Fix | Delete
# Default to -1 to signify that values not known; not critical to have,
[340] Fix | Delete
# though
[341] Fix | Delete
week_of_year = -1
[342] Fix | Delete
week_of_year_start = -1
[343] Fix | Delete
# weekday and julian defaulted to None so as to signal need to calculate
[344] Fix | Delete
# values
[345] Fix | Delete
weekday = julian = None
[346] Fix | Delete
found_dict = found.groupdict()
[347] Fix | Delete
for group_key in found_dict.iterkeys():
[348] Fix | Delete
# Directives not explicitly handled below:
[349] Fix | Delete
# c, x, X
[350] Fix | Delete
# handled by making out of other directives
[351] Fix | Delete
# U, W
[352] Fix | Delete
# worthless without day of the week
[353] Fix | Delete
if group_key == 'y':
[354] Fix | Delete
year = int(found_dict['y'])
[355] Fix | Delete
# Open Group specification for strptime() states that a %y
[356] Fix | Delete
#value in the range of [00, 68] is in the century 2000, while
[357] Fix | Delete
#[69,99] is in the century 1900
[358] Fix | Delete
if year <= 68:
[359] Fix | Delete
year += 2000
[360] Fix | Delete
else:
[361] Fix | Delete
year += 1900
[362] Fix | Delete
elif group_key == 'Y':
[363] Fix | Delete
year = int(found_dict['Y'])
[364] Fix | Delete
elif group_key == 'm':
[365] Fix | Delete
month = int(found_dict['m'])
[366] Fix | Delete
elif group_key == 'B':
[367] Fix | Delete
month = locale_time.f_month.index(found_dict['B'].lower())
[368] Fix | Delete
elif group_key == 'b':
[369] Fix | Delete
month = locale_time.a_month.index(found_dict['b'].lower())
[370] Fix | Delete
elif group_key == 'd':
[371] Fix | Delete
day = int(found_dict['d'])
[372] Fix | Delete
elif group_key == 'H':
[373] Fix | Delete
hour = int(found_dict['H'])
[374] Fix | Delete
elif group_key == 'I':
[375] Fix | Delete
hour = int(found_dict['I'])
[376] Fix | Delete
ampm = found_dict.get('p', '').lower()
[377] Fix | Delete
# If there was no AM/PM indicator, we'll treat this like AM
[378] Fix | Delete
if ampm in ('', locale_time.am_pm[0]):
[379] Fix | Delete
# We're in AM so the hour is correct unless we're
[380] Fix | Delete
# looking at 12 midnight.
[381] Fix | Delete
# 12 midnight == 12 AM == hour 0
[382] Fix | Delete
if hour == 12:
[383] Fix | Delete
hour = 0
[384] Fix | Delete
elif ampm == locale_time.am_pm[1]:
[385] Fix | Delete
# We're in PM so we need to add 12 to the hour unless
[386] Fix | Delete
# we're looking at 12 noon.
[387] Fix | Delete
# 12 noon == 12 PM == hour 12
[388] Fix | Delete
if hour != 12:
[389] Fix | Delete
hour += 12
[390] Fix | Delete
elif group_key == 'M':
[391] Fix | Delete
minute = int(found_dict['M'])
[392] Fix | Delete
elif group_key == 'S':
[393] Fix | Delete
second = int(found_dict['S'])
[394] Fix | Delete
elif group_key == 'f':
[395] Fix | Delete
s = found_dict['f']
[396] Fix | Delete
# Pad to always return microseconds.
[397] Fix | Delete
s += "0" * (6 - len(s))
[398] Fix | Delete
fraction = int(s)
[399] Fix | Delete
elif group_key == 'A':
[400] Fix | Delete
weekday = locale_time.f_weekday.index(found_dict['A'].lower())
[401] Fix | Delete
elif group_key == 'a':
[402] Fix | Delete
weekday = locale_time.a_weekday.index(found_dict['a'].lower())
[403] Fix | Delete
elif group_key == 'w':
[404] Fix | Delete
weekday = int(found_dict['w'])
[405] Fix | Delete
if weekday == 0:
[406] Fix | Delete
weekday = 6
[407] Fix | Delete
else:
[408] Fix | Delete
weekday -= 1
[409] Fix | Delete
elif group_key == 'j':
[410] Fix | Delete
julian = int(found_dict['j'])
[411] Fix | Delete
elif group_key in ('U', 'W'):
[412] Fix | Delete
week_of_year = int(found_dict[group_key])
[413] Fix | Delete
if group_key == 'U':
[414] Fix | Delete
# U starts week on Sunday.
[415] Fix | Delete
week_of_year_start = 6
[416] Fix | Delete
else:
[417] Fix | Delete
# W starts week on Monday.
[418] Fix | Delete
week_of_year_start = 0
[419] Fix | Delete
elif group_key == 'Z':
[420] Fix | Delete
# Since -1 is default value only need to worry about setting tz if
[421] Fix | Delete
# it can be something other than -1.
[422] Fix | Delete
found_zone = found_dict['Z'].lower()
[423] Fix | Delete
for value, tz_values in enumerate(locale_time.timezone):
[424] Fix | Delete
if found_zone in tz_values:
[425] Fix | Delete
# Deal with bad locale setup where timezone names are the
[426] Fix | Delete
# same and yet time.daylight is true; too ambiguous to
[427] Fix | Delete
# be able to tell what timezone has daylight savings
[428] Fix | Delete
if (time.tzname[0] == time.tzname[1] and
[429] Fix | Delete
time.daylight and found_zone not in ("utc", "gmt")):
[430] Fix | Delete
break
[431] Fix | Delete
else:
[432] Fix | Delete
tz = value
[433] Fix | Delete
break
[434] Fix | Delete
leap_year_fix = False
[435] Fix | Delete
if year is None and month == 2 and day == 29:
[436] Fix | Delete
year = 1904 # 1904 is first leap year of 20th century
[437] Fix | Delete
leap_year_fix = True
[438] Fix | Delete
elif year is None:
[439] Fix | Delete
year = 1900
[440] Fix | Delete
# If we know the week of the year and what day of that week, we can figure
[441] Fix | Delete
# out the Julian day of the year.
[442] Fix | Delete
if julian is None and week_of_year != -1 and weekday is not None:
[443] Fix | Delete
week_starts_Mon = True if week_of_year_start == 0 else False
[444] Fix | Delete
julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
[445] Fix | Delete
week_starts_Mon)
[446] Fix | Delete
if julian <= 0:
[447] Fix | Delete
year -= 1
[448] Fix | Delete
yday = 366 if calendar.isleap(year) else 365
[449] Fix | Delete
julian += yday
[450] Fix | Delete
# Cannot pre-calculate datetime_date() since can change in Julian
[451] Fix | Delete
# calculation and thus could have different value for the day of the week
[452] Fix | Delete
# calculation.
[453] Fix | Delete
if julian is None:
[454] Fix | Delete
# Need to add 1 to result since first day of the year is 1, not 0.
[455] Fix | Delete
julian = datetime_date(year, month, day).toordinal() - \
[456] Fix | Delete
datetime_date(year, 1, 1).toordinal() + 1
[457] Fix | Delete
else: # Assume that if they bothered to include Julian day it will
[458] Fix | Delete
# be accurate.
[459] Fix | Delete
datetime_result = datetime_date.fromordinal((julian - 1) + datetime_date(year, 1, 1).toordinal())
[460] Fix | Delete
year = datetime_result.year
[461] Fix | Delete
month = datetime_result.month
[462] Fix | Delete
day = datetime_result.day
[463] Fix | Delete
if weekday is None:
[464] Fix | Delete
weekday = datetime_date(year, month, day).weekday()
[465] Fix | Delete
if leap_year_fix:
[466] Fix | Delete
# the caller didn't supply a year but asked for Feb 29th. We couldn't
[467] Fix | Delete
# use the default of 1900 for computations. We set it back to ensure
[468] Fix | Delete
# that February 29th is smaller than March 1st.
[469] Fix | Delete
year = 1900
[470] Fix | Delete
[471] Fix | Delete
return (time.struct_time((year, month, day,
[472] Fix | Delete
hour, minute, second,
[473] Fix | Delete
weekday, julian, tz)), fraction)
[474] Fix | Delete
[475] Fix | Delete
def _strptime_time(data_string, format="%a %b %d %H:%M:%S %Y"):
[476] Fix | Delete
return _strptime(data_string, format)[0]
[477] Fix | Delete
[478] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function