whitespace_replacement = re_compile('\s+') format = whitespace_replacement.sub('\s+', format) while '%' in format: directive_index = format.index('%') + 1 processed_format = "%s%s%s" % (processed_format, format[:directive_index - 1], self[format[directive_index]]) format = format[directive_index + 1:] return "%s%s" % (processed_format, format) def compile(self, format): """Return a compiled re object for the format string.""" return re_compile(self.pattern(format), IGNORECASE) _cache_lock = _thread_allocate_lock() # DO NOT modify _TimeRE_cache or _regex_cache without acquiring the cache lock # first! _TimeRE_cache = TimeRE() _CACHE_MAX_SIZE = 5 # Max number of regexes stored in _regex_cache _regex_cache = {} def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon): """Calculate the Julian day based on the year, week of the year, and day of the week, with week_start_day representing whether the week of the year assumes the week starts on Sunday or Monday (6 or 0).""" first_weekday = datetime_date(year, 1, 1).weekday() # If we are dealing with the %U directive (week starts on Sunday), it's # easier to just shift the view to Sunday being the first day of the # week.
format = regex_chars.sub(r"\\\1", format) whitespace_replacement = re_compile(r'\s+') format = whitespace_replacement.sub(r'\\s+', format) while '%' in format: directive_index = format.index('%')+1 processed_format = "%s%s%s" % (processed_format, format[:directive_index-1], self[format[directive_index]]) format = format[directive_index+1:] return "%s%s" % (processed_format, format) def compile(self, format): """Return a compiled re object for the format string.""" return re_compile(self.pattern(format), IGNORECASE) _cache_lock = _thread_allocate_lock() # DO NOT modify _TimeRE_cache or _regex_cache without acquiring the cache lock # first! _TimeRE_cache = TimeRE() _CACHE_MAX_SIZE = 5 # Max number of regexes stored in _regex_cache _regex_cache = {} def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon): """Calculate the Julian day based on the year, week of the year, and day of the week, with week_start_day representing whether the week of the year assumes the week starts on Sunday or Monday (6 or 0).""" first_weekday = datetime_date(year, 1, 1).weekday() # If we are dealing with the %U directive (week starts on Sunday), it's # easier to just shift the view to Sunday being the first day of the # week. if not week_starts_Mon:
return key, symbol # if it's an extended match, return the unextended version elif len(key) == 3 and key[0:2] == '%!': return key, key[-1] # otherwise, remove it else: return key, '' DEFAULT_TO_WRITE = dict(map(auto_convert, FLEXIBLE_REGEX.keys())) DEFAULT_TO_WRITE.update(HIDE_CHOICES) # thread-safe caching CACHE_MAX_SIZE = 100 _CACHE_LOCK = _thread_allocate_lock() _CACHED_REGEXP = lru_cache(maxsize=CACHE_MAX_SIZE)(_to_regexp) def to_regexp(fmt, substitutions=None): with _CACHE_LOCK: return _CACHED_REGEXP(fmt, substitutions) # the main logic to construct a date/time from the matched data, lifted # verbatim from the python source. the only changes are to check that # a group has actually matched (since now some may be optional), the # modified handling for y50, and uzing -ve indices for z minutes. def to_time_tuple(found_dict): '''Closely based on _strptime in standard Python.''' year = None