| 1 |
"""PyUnit testing against strptime""" |
| 2 |
|
| 3 |
import unittest |
| 4 |
import time |
| 5 |
import locale |
| 6 |
import re |
| 7 |
import os |
| 8 |
import platform |
| 9 |
import sys |
| 10 |
from test import support |
| 11 |
from test.support import warnings_helper |
| 12 |
from test.support import skip_if_buggy_ucrt_strfptime, run_with_locales |
| 13 |
from datetime import date as datetime_date |
| 14 |
|
| 15 |
import _strptime |
| 16 |
|
| 17 |
libc_ver = platform.libc_ver() |
| 18 |
if libc_ver[0] == 'glibc': |
| 19 |
glibc_ver = tuple(map(int, libc_ver[1].split('.'))) |
| 20 |
else: |
| 21 |
glibc_ver = None |
| 22 |
|
| 23 |
|
| 24 |
class getlang_Tests(unittest.TestCase): |
| 25 |
"""Test _getlang""" |
| 26 |
def test_basic(self): |
| 27 |
self.assertEqual(_strptime._getlang(), locale.getlocale(locale.LC_TIME)) |
| 28 |
|
| 29 |
class LocaleTime_Tests(unittest.TestCase): |
| 30 |
"""Tests for _strptime.LocaleTime. |
| 31 |
|
| 32 |
All values are lower-cased when stored in LocaleTime, so make sure to |
| 33 |
compare values after running ``lower`` on them. |
| 34 |
|
| 35 |
""" |
| 36 |
|
| 37 |
def setUp(self): |
| 38 |
"""Create time tuple based on current time.""" |
| 39 |
self.time_tuple = time.localtime() |
| 40 |
self.LT_ins = _strptime.LocaleTime() |
| 41 |
|
| 42 |
def compare_against_time(self, testing, directive, tuple_position, |
| 43 |
error_msg): |
| 44 |
"""Helper method that tests testing against directive based on the |
| 45 |
tuple_position of time_tuple. Uses error_msg as error message. |
| 46 |
|
| 47 |
""" |
| 48 |
strftime_output = time.strftime(directive, self.time_tuple).lower() |
| 49 |
comparison = testing[self.time_tuple[tuple_position]] |
| 50 |
self.assertIn(strftime_output, testing, |
| 51 |
"%s: not found in tuple" % error_msg) |
| 52 |
self.assertEqual(comparison, strftime_output, |
| 53 |
"%s: position within tuple incorrect; %s != %s" % |
| 54 |
(error_msg, comparison, strftime_output)) |
| 55 |
|
| 56 |
def test_weekday(self): |
| 57 |
# Make sure that full and abbreviated weekday names are correct in |
| 58 |
# both string and position with tuple |
| 59 |
self.compare_against_time(self.LT_ins.f_weekday, '%A', 6, |
| 60 |
"Testing of full weekday name failed") |
| 61 |
self.compare_against_time(self.LT_ins.a_weekday, '%a', 6, |
| 62 |
"Testing of abbreviated weekday name failed") |
| 63 |
|
| 64 |
def test_month(self): |
| 65 |
# Test full and abbreviated month names; both string and position |
| 66 |
# within the tuple |
| 67 |
self.compare_against_time(self.LT_ins.f_month, '%B', 1, |
| 68 |
"Testing against full month name failed") |
| 69 |
self.compare_against_time(self.LT_ins.a_month, '%b', 1, |
| 70 |
"Testing against abbreviated month name failed") |
| 71 |
|
| 72 |
def test_am_pm(self): |
| 73 |
# Make sure AM/PM representation done properly |
| 74 |
strftime_output = time.strftime("%p", self.time_tuple).lower() |
| 75 |
self.assertIn(strftime_output, self.LT_ins.am_pm, |
| 76 |
"AM/PM representation not in tuple") |
| 77 |
if self.time_tuple[3] < 12: position = 0 |
| 78 |
else: position = 1 |
| 79 |
self.assertEqual(self.LT_ins.am_pm[position], strftime_output, |
| 80 |
"AM/PM representation in the wrong position within the tuple") |
| 81 |
|
| 82 |
def test_timezone(self): |
| 83 |
# Make sure timezone is correct |
| 84 |
timezone = time.strftime("%Z", self.time_tuple).lower() |
| 85 |
if timezone: |
| 86 |
self.assertTrue(timezone in self.LT_ins.timezone[0] or |
| 87 |
timezone in self.LT_ins.timezone[1], |
| 88 |
"timezone %s not found in %s" % |
| 89 |
(timezone, self.LT_ins.timezone)) |
| 90 |
|
| 91 |
def test_date_time(self): |
| 92 |
# Check that LC_date_time, LC_date, and LC_time are correct |
| 93 |
# the magic date is used so as to not have issues with %c when day of |
| 94 |
# the month is a single digit and has a leading space. This is not an |
| 95 |
# issue since strptime still parses it correctly. The problem is |
| 96 |
# testing these directives for correctness by comparing strftime |
| 97 |
# output. |
| 98 |
magic_date = (1999, 3, 17, 22, 44, 55, 2, 76, 0) |
| 99 |
strftime_output = time.strftime("%c", magic_date) |
| 100 |
self.assertEqual(time.strftime(self.LT_ins.LC_date_time, magic_date), |
| 101 |
strftime_output, "LC_date_time incorrect") |
| 102 |
strftime_output = time.strftime("%x", magic_date) |
| 103 |
self.assertEqual(time.strftime(self.LT_ins.LC_date, magic_date), |
| 104 |
strftime_output, "LC_date incorrect") |
| 105 |
strftime_output = time.strftime("%X", magic_date) |
| 106 |
self.assertEqual(time.strftime(self.LT_ins.LC_time, magic_date), |
| 107 |
strftime_output, "LC_time incorrect") |
| 108 |
LT = _strptime.LocaleTime() |
| 109 |
LT.am_pm = ('', '') |
| 110 |
self.assertTrue(LT.LC_time, "LocaleTime's LC directives cannot handle " |
| 111 |
"empty strings") |
| 112 |
|
| 113 |
def test_lang(self): |
| 114 |
# Make sure lang is set to what _getlang() returns |
| 115 |
# Assuming locale has not changed between now and when self.LT_ins was created |
| 116 |
self.assertEqual(self.LT_ins.lang, _strptime._getlang()) |
| 117 |
|
| 118 |
|
| 119 |
class TimeRETests(unittest.TestCase): |
| 120 |
"""Tests for TimeRE.""" |
| 121 |
|
| 122 |
def setUp(self): |
| 123 |
"""Construct generic TimeRE object.""" |
| 124 |
self.time_re = _strptime.TimeRE() |
| 125 |
self.locale_time = _strptime.LocaleTime() |
| 126 |
|
| 127 |
def test_pattern(self): |
| 128 |
# Test TimeRE.pattern |
| 129 |
pattern_string = self.time_re.pattern(r"%a %A %d %Y") |
| 130 |
self.assertTrue(pattern_string.find(self.locale_time.a_weekday[2]) != -1, |
| 131 |
"did not find abbreviated weekday in pattern string '%s'" % |
| 132 |
pattern_string) |
| 133 |
self.assertTrue(pattern_string.find(self.locale_time.f_weekday[4]) != -1, |
| 134 |
"did not find full weekday in pattern string '%s'" % |
| 135 |
pattern_string) |
| 136 |
self.assertTrue(pattern_string.find(self.time_re['d']) != -1, |
| 137 |
"did not find 'd' directive pattern string '%s'" % |
| 138 |
pattern_string) |
| 139 |
|
| 140 |
def test_pattern_escaping(self): |
| 141 |
# Make sure any characters in the format string that might be taken as |
| 142 |
# regex syntax is escaped. |
| 143 |
pattern_string = self.time_re.pattern(r"\d+") |
| 144 |
self.assertIn(r"\\d\+", pattern_string, |
| 145 |
"%s does not have re characters escaped properly" % |
| 146 |
pattern_string) |
| 147 |
|
| 148 |
@skip_if_buggy_ucrt_strfptime |
| 149 |
def test_compile(self): |
| 150 |
# Check that compiled regex is correct |
| 151 |
found = self.time_re.compile(r"%A").match(self.locale_time.f_weekday[6]) |
| 152 |
self.assertTrue(found and found.group('A') == self.locale_time.f_weekday[6], |
| 153 |
"re object for '%A' failed") |
| 154 |
compiled = self.time_re.compile(r"%a %b") |
| 155 |
found = compiled.match("%s %s" % (self.locale_time.a_weekday[4], |
| 156 |
self.locale_time.a_month[4])) |
| 157 |
self.assertTrue(found, |
| 158 |
"Match failed with '%s' regex and '%s' string" % |
| 159 |
(compiled.pattern, "%s %s" % (self.locale_time.a_weekday[4], |
| 160 |
self.locale_time.a_month[4]))) |
| 161 |
self.assertTrue(found.group('a') == self.locale_time.a_weekday[4] and |
| 162 |
found.group('b') == self.locale_time.a_month[4], |
| 163 |
"re object couldn't find the abbreviated weekday month in " |
| 164 |
"'%s' using '%s'; group 'a' = '%s', group 'b' = %s'" % |
| 165 |
(found.string, found.re.pattern, found.group('a'), |
| 166 |
found.group('b'))) |
| 167 |
for directive in ('a','A','b','B','c','d','G','H','I','j','m','M','p', |
| 168 |
'S','u','U','V','w','W','x','X','y','Y','Z','%'): |
| 169 |
fmt = "%d %Y" if directive == 'd' else "%" + directive |
| 170 |
compiled = self.time_re.compile(fmt) |
| 171 |
found = compiled.match(time.strftime(fmt)) |
| 172 |
self.assertTrue(found, "Matching failed on '%s' using '%s' regex" % |
| 173 |
(time.strftime(fmt), |
| 174 |
compiled.pattern)) |
| 175 |
|
| 176 |
def test_blankpattern(self): |
| 177 |
# Make sure when tuple or something has no values no regex is generated. |
| 178 |
# Fixes bug #661354 |
| 179 |
test_locale = _strptime.LocaleTime() |
| 180 |
test_locale.timezone = (frozenset(), frozenset()) |
| 181 |
self.assertEqual(_strptime.TimeRE(test_locale).pattern("%Z"), '', |
| 182 |
"with timezone == ('',''), TimeRE().pattern('%Z') != ''") |
| 183 |
|
| 184 |
def test_matching_with_escapes(self): |
| 185 |
# Make sure a format that requires escaping of characters works |
| 186 |
compiled_re = self.time_re.compile(r"\w+ %m") |
| 187 |
found = compiled_re.match(r"\w+ 10") |
| 188 |
self.assertTrue(found, r"Escaping failed of format '\w+ 10'") |
| 189 |
|
| 190 |
def test_locale_data_w_regex_metacharacters(self): |
| 191 |
# Check that if locale data contains regex metacharacters they are |
| 192 |
# escaped properly. |
| 193 |
# Discovered by bug #1039270 . |
| 194 |
locale_time = _strptime.LocaleTime() |
| 195 |
locale_time.timezone = (frozenset(("utc", "gmt", |
| 196 |
"Tokyo (standard time)")), |
| 197 |
frozenset("Tokyo (daylight time)")) |
| 198 |
time_re = _strptime.TimeRE(locale_time) |
| 199 |
self.assertTrue(time_re.compile("%Z").match("Tokyo (standard time)"), |
| 200 |
"locale data that contains regex metacharacters is not" |
| 201 |
" properly escaped") |
| 202 |
|
| 203 |
def test_whitespace_substitution(self): |
| 204 |
# When pattern contains whitespace, make sure it is taken into account |
| 205 |
# so as to not allow subpatterns to end up next to each other and |
| 206 |
# "steal" characters from each other. |
| 207 |
pattern = self.time_re.pattern('%j %H') |
| 208 |
self.assertFalse(re.match(pattern, "180")) |
| 209 |
self.assertTrue(re.match(pattern, "18 0")) |
| 210 |
|
| 211 |
|
| 212 |
class StrptimeTests(unittest.TestCase): |
| 213 |
"""Tests for _strptime.strptime.""" |
| 214 |
|
| 215 |
def setUp(self): |
| 216 |
"""Create testing time tuples.""" |
| 217 |
self.time_tuple = time.localtime() |
| 218 |
|
| 219 |
def test_ValueError(self): |
| 220 |
# Make sure ValueError is raised when match fails or format is bad |
| 221 |
self.assertRaises(ValueError, _strptime._strptime_time, data_string="%d", |
| 222 |
format="%A") |
| 223 |
for bad_format in ("%", "% ", "%\n"): |
| 224 |
with (self.subTest(format=bad_format), |
| 225 |
self.assertRaisesRegex(ValueError, "stray % in format ")): |
| 226 |
_strptime._strptime_time("2005", bad_format) |
| 227 |
for bad_format in ("%i", "%Oi", "%O", "%O ", "%Ee", "%E", "%E ", |
| 228 |
"%.", "%+", "%~", "%\\", |
| 229 |
"%O.", "%O+", "%O_", "%O~", "%O\\"): |
| 230 |
directive = bad_format[1:].rstrip() |
| 231 |
with (self.subTest(format=bad_format), |
| 232 |
self.assertRaisesRegex(ValueError, |
| 233 |
f"'{re.escape(directive)}' is a bad directive in format ")): |
| 234 |
_strptime._strptime_time("2005", bad_format) |
| 235 |
|
| 236 |
msg_week_no_year_or_weekday = r"ISO week directive '%V' must be used with " \ |
| 237 |
r"the ISO year directive '%G' and a weekday directive " \ |
| 238 |
r"\('%A', '%a', '%w', or '%u'\)." |
| 239 |
msg_week_not_compatible = r"ISO week directive '%V' is incompatible with " \ |
| 240 |
r"the year directive '%Y'. Use the ISO year '%G' instead." |
| 241 |
msg_julian_not_compatible = r"Day of the year directive '%j' is not " \ |
| 242 |
r"compatible with ISO year directive '%G'. Use '%Y' instead." |
| 243 |
msg_year_no_week_or_weekday = r"ISO year directive '%G' must be used with " \ |
| 244 |
r"the ISO week directive '%V' and a weekday directive " \ |
| 245 |
r"\('%A', '%a', '%w', or '%u'\)." |
| 246 |
|
| 247 |
locale_time = _strptime.LocaleTime() |
| 248 |
|
| 249 |
# Ambiguous or incomplete cases using ISO year/week/weekday directives |
| 250 |
subtests = [ |
| 251 |
# 1. ISO week (%V) is specified, but the year is specified with %Y |
| 252 |
# instead of %G |
| 253 |
("1999 50", "%Y %V", msg_week_no_year_or_weekday), |
| 254 |
("1999 50 5", "%Y %V %u", msg_week_not_compatible), |
| 255 |
# 2. ISO year (%G) and ISO week (%V) are specified, but weekday is not |
| 256 |
("1999 51", "%G %V", msg_year_no_week_or_weekday), |
| 257 |
# 3. ISO year (%G) and weekday are specified, but ISO week (%V) is not |
| 258 |
("1999 {}".format(locale_time.f_weekday[5]), "%G %A", |
| 259 |
msg_year_no_week_or_weekday), |
| 260 |
("1999 {}".format(locale_time.a_weekday[5]), "%G %a", |
| 261 |
msg_year_no_week_or_weekday), |
| 262 |
("1999 5", "%G %w", msg_year_no_week_or_weekday), |
| 263 |
("1999 5", "%G %u", msg_year_no_week_or_weekday), |
| 264 |
# 4. ISO year is specified alone (e.g. time.strptime('2015', '%G')) |
| 265 |
("2015", "%G", msg_year_no_week_or_weekday), |
| 266 |
# 5. Julian/ordinal day (%j) is specified with %G, but not %Y |
| 267 |
("1999 256", "%G %j", msg_julian_not_compatible), |
| 268 |
("1999 50 5 256", "%G %V %u %j", msg_julian_not_compatible), |
| 269 |
# ISO week specified alone |
| 270 |
("50", "%V", msg_week_no_year_or_weekday), |
| 271 |
# ISO year is unspecified, falling back to year |
| 272 |
("50 5", "%V %u", msg_week_no_year_or_weekday), |
| 273 |
# 6. Invalid ISO weeks |
| 274 |
("2019-00-1", "%G-%V-%u", |
| 275 |
"time data '2019-00-1' does not match format '%G-%V-%u'"), |
| 276 |
("2019-54-1", "%G-%V-%u", |
| 277 |
"time data '2019-54-1' does not match format '%G-%V-%u'"), |
| 278 |
("2021-53-1", "%G-%V-%u", "Invalid week: 53"), |
| 279 |
] |
| 280 |
|
| 281 |
for (data_string, format, message) in subtests: |
| 282 |
with self.subTest(data_string=data_string, format=format): |
| 283 |
with self.assertRaisesRegex(ValueError, message): |
| 284 |
_strptime._strptime(data_string, format) |
| 285 |
|
| 286 |
def test_strptime_exception_context(self): |
| 287 |
# check that this doesn't chain exceptions needlessly (see #17572) |
| 288 |
with self.assertRaises(ValueError) as e: |
| 289 |
_strptime._strptime_time('', '%D') |
| 290 |
self.assertTrue(e.exception.__suppress_context__) |
| 291 |
# additional check for stray % branch |
| 292 |
with self.assertRaises(ValueError) as e: |
| 293 |
_strptime._strptime_time('%', '%') |
| 294 |
self.assertTrue(e.exception.__suppress_context__) |
| 295 |
|
| 296 |
def test_unconverteddata(self): |
| 297 |
# Check ValueError is raised when there is unconverted data |
| 298 |
self.assertRaises(ValueError, _strptime._strptime_time, "10 12", "%m") |
| 299 |
|
| 300 |
def roundtrip(self, fmt, position, time_tuple=None): |
| 301 |
"""Helper fxn in testing.""" |
| 302 |
if time_tuple is None: |
| 303 |
time_tuple = self.time_tuple |
| 304 |
strf_output = time.strftime(fmt, time_tuple) |
| 305 |
strp_output = _strptime._strptime_time(strf_output, fmt) |
| 306 |
self.assertEqual(strp_output[position], time_tuple[position], |
| 307 |
"testing of %r format failed; %r -> %r != %r" % |
| 308 |
(fmt, strf_output, strp_output[position], |
| 309 |
time_tuple[position])) |
| 310 |
if support.verbose >= 3: |
| 311 |
print("testing of %r format: %r -> %r" % |
| 312 |
(fmt, strf_output, strp_output[position])) |
| 313 |
|
| 314 |
def test_year(self): |
| 315 |
# Test that the year is handled properly |
| 316 |
self.roundtrip('%Y', 0) |
| 317 |
self.roundtrip('%y', 0) |
| 318 |
self.roundtrip('%Y', 0, (1900, 1, 1, 0, 0, 0, 0, 1, 0)) |
| 319 |
|
| 320 |
# Must also make sure %y values are correct for bounds set by Open Group |
| 321 |
strptime = _strptime._strptime_time |
| 322 |
self.assertEqual(strptime('00', '%y')[0], 2000) |
| 323 |
self.assertEqual(strptime('68', '%y')[0], 2068) |
| 324 |
self.assertEqual(strptime('69', '%y')[0], 1969) |
| 325 |
self.assertEqual(strptime('99', '%y')[0], 1999) |
| 326 |
|
| 327 |
def test_month(self): |
| 328 |
# Test for month directives |
| 329 |
self.roundtrip('%m', 1) |
| 330 |
|
| 331 |
@run_with_locales('LC_TIME', 'C', 'en_US', 'fr_FR', 'de_DE', 'ja_JP', 'he_IL', '') |
| 332 |
def test_month_locale(self): |
| 333 |
# Test for month directives |
| 334 |
self.roundtrip('%B', 1) |
| 335 |
self.roundtrip('%b', 1) |
| 336 |
for m in range(1, 13): |
| 337 |
self.roundtrip('%B', 1, (1900, m, 1, 0, 0, 0, 0, 1, 0)) |
| 338 |
self.roundtrip('%b', 1, (1900, m, 1, 0, 0, 0, 0, 1, 0)) |
| 339 |
|
| 340 |
@run_with_locales('LC_TIME', 'az_AZ', 'ber_DZ', 'ber_MA', 'crh_UA') |
| 341 |
def test_month_locale2(self): |
| 342 |
# Test for month directives |
| 343 |
# Month name contains 'İ' ('\u0130') |
| 344 |
self.roundtrip('%B', 1, (2025, 6, 1, 0, 0, 0, 6, 152, 0)) |
| 345 |
self.roundtrip('%b', 1, (2025, 6, 1, 0, 0, 0, 6, 152, 0)) |
| 346 |
self.roundtrip('%B', 1, (2025, 7, 1, 0, 0, 0, 1, 182, 0)) |
| 347 |
self.roundtrip('%b', 1, (2025, 7, 1, 0, 0, 0, 1, 182, 0)) |
| 348 |
|
| 349 |
def test_day(self): |
| 350 |
# Test for day directives |
| 351 |
self.roundtrip('%d %Y', 2) |
| 352 |
|
| 353 |
def test_hour(self): |
| 354 |
# Test hour directives |
| 355 |
self.roundtrip('%H', 3) |
| 356 |
|
| 357 |
# NB: Only works on locales with AM/PM |
| 358 |
@run_with_locales('LC_TIME', 'C', 'en_US', 'ja_JP') |
| 359 |
def test_hour_locale(self): |
| 360 |
# Test hour directives |
| 361 |
self.roundtrip('%I %p', 3) |
| 362 |
|
| 363 |
def test_minute(self): |
| 364 |
# Test minute directives |
| 365 |
self.roundtrip('%M', 4) |
| 366 |
|
| 367 |
def test_second(self): |
| 368 |
# Test second directives |
| 369 |
self.roundtrip('%S', 5) |
| 370 |
|
| 371 |
def test_fraction(self): |
| 372 |
# Test microseconds |
| 373 |
import datetime |
| 374 |
d = datetime.datetime(2012, 12, 20, 12, 34, 56, 78987) |
| 375 |
tup, frac, _ = _strptime._strptime(str(d), format="%Y-%m-%d %H:%M:%S.%f") |
| 376 |
self.assertEqual(frac, d.microsecond) |
| 377 |
|
| 378 |
def test_weekday(self): |
| 379 |
# Test weekday directives |
| 380 |
self.roundtrip('%w', 6) |
| 381 |
self.roundtrip('%u', 6) |
| 382 |
|
| 383 |
@run_with_locales('LC_TIME', 'C', 'en_US', 'fr_FR', 'de_DE', 'ja_JP', '') |
| 384 |
def test_weekday_locale(self): |
| 385 |
# Test weekday directives |
| 386 |
self.roundtrip('%A', 6) |
| 387 |
self.roundtrip('%a', 6) |
| 388 |
|
| 389 |
def test_julian(self): |
| 390 |
# Test julian directives |
| 391 |
self.roundtrip('%j', 7) |
| 392 |
|
| 393 |
def test_offset(self): |
| 394 |
one_hour = 60 * 60 |
| 395 |
half_hour = 30 * 60 |
| 396 |
half_minute = 30 |
| 397 |
(*_, offset), _, offset_fraction = _strptime._strptime("+0130", "%z") |
| 398 |
self.assertEqual(offset, one_hour + half_hour) |
| 399 |
self.assertEqual(offset_fraction, 0) |
| 400 |
(*_, offset), _, offset_fraction = _strptime._strptime("-0100", "%z") |
| 401 |
self.assertEqual(offset, -one_hour) |
| 402 |
self.assertEqual(offset_fraction, 0) |
| 403 |
(*_, offset), _, offset_fraction = _strptime._strptime("-013030", "%z") |
| 404 |
self.assertEqual(offset, -(one_hour + half_hour + half_minute)) |
| 405 |
self.assertEqual(offset_fraction, 0) |
| 406 |
(*_, offset), _, offset_fraction = _strptime._strptime("-013030.000001", "%z") |
| 407 |
self.assertEqual(offset, -(one_hour + half_hour + half_minute)) |
| 408 |
self.assertEqual(offset_fraction, -1) |
| 409 |
(*_, offset), _, offset_fraction = _strptime._strptime("+01:00", "%z") |
| 410 |
self.assertEqual(offset, one_hour) |
| 411 |
self.assertEqual(offset_fraction, 0) |
| 412 |
(*_, offset), _, offset_fraction = _strptime._strptime("-01:30", "%z") |
| 413 |
self.assertEqual(offset, -(one_hour + half_hour)) |
| 414 |
self.assertEqual(offset_fraction, 0) |
| 415 |
(*_, offset), _, offset_fraction = _strptime._strptime("-01:30:30", "%z") |
| 416 |
self.assertEqual(offset, -(one_hour + half_hour + half_minute)) |
| 417 |
self.assertEqual(offset_fraction, 0) |
| 418 |
(*_, offset), _, offset_fraction = _strptime._strptime("-01:30:30.000001", "%z") |
| 419 |
self.assertEqual(offset, -(one_hour + half_hour + half_minute)) |
| 420 |
self.assertEqual(offset_fraction, -1) |
| 421 |
(*_, offset), _, offset_fraction = _strptime._strptime("+01:30:30.001", "%z") |
| 422 |
self.assertEqual(offset, one_hour + half_hour + half_minute) |
| 423 |
self.assertEqual(offset_fraction, 1000) |
| 424 |
(*_, offset), _, offset_fraction = _strptime._strptime("Z", "%z") |
| 425 |
self.assertEqual(offset, 0) |
| 426 |
self.assertEqual(offset_fraction, 0) |
| 427 |
|
| 428 |
def test_bad_offset(self): |
| 429 |
with self.assertRaises(ValueError): |
| 430 |
_strptime._strptime("-01:30:30.", "%z") |
| 431 |
with self.assertRaises(ValueError): |
| 432 |
_strptime._strptime("-0130:30", "%z") |
| 433 |
with self.assertRaises(ValueError): |
| 434 |
_strptime._strptime("-01:30:30.1234567", "%z") |
| 435 |
with self.assertRaises(ValueError): |
| 436 |
_strptime._strptime("-01:30:30:123456", "%z") |
| 437 |
with self.assertRaises(ValueError) as err: |
| 438 |
_strptime._strptime("-01:3030", "%z") |
| 439 |
self.assertEqual("Inconsistent use of : in -01:3030", str(err.exception)) |
| 440 |
|
| 441 |
@skip_if_buggy_ucrt_strfptime |
| 442 |
def test_timezone(self): |
| 443 |
# Test timezone directives. |
| 444 |
# When gmtime() is used with %Z, entire result of strftime() is empty. |
| 445 |
# Check for equal timezone names deals with bad locale info when this |
| 446 |
# occurs; first found in FreeBSD 4.4. |
| 447 |
strp_output = _strptime._strptime_time("UTC", "%Z") |
| 448 |
self.assertEqual(strp_output.tm_isdst, 0) |
| 449 |
strp_output = _strptime._strptime_time("GMT", "%Z") |
| 450 |
self.assertEqual(strp_output.tm_isdst, 0) |
| 451 |
time_tuple = time.localtime() |
| 452 |
strf_output = time.strftime("%Z") #UTC does not have a timezone |
| 453 |
strp_output = _strptime._strptime_time(strf_output, "%Z") |
| 454 |
locale_time = _strptime.LocaleTime() |
| 455 |
if time.tzname[0] != time.tzname[1] or not time.daylight: |
| 456 |
self.assertTrue(strp_output[8] == time_tuple[8], |
| 457 |
"timezone check failed; '%s' -> %s != %s" % |
| 458 |
(strf_output, strp_output[8], time_tuple[8])) |
| 459 |
else: |
| 460 |
self.assertTrue(strp_output[8] == -1, |
| 461 |
"LocaleTime().timezone has duplicate values and " |
| 462 |
"time.daylight but timezone value not set to -1") |
| 463 |
|
| 464 |
@unittest.skipUnless( |
| 465 |
hasattr(time, "tzset"), "time module has no attribute tzset" |
| 466 |
) |
| 467 |
def test_bad_timezone(self): |
| 468 |
# Explicitly test possibility of bad timezone; |
| 469 |
# when time.tzname[0] == time.tzname[1] and time.daylight |
| 470 |
tz_name = time.tzname[0] |
| 471 |
if tz_name.upper() in ("UTC", "GMT"): |
| 472 |
self.skipTest('need non-UTC/GMT timezone') |
| 473 |
|
| 474 |
with support.swap_attr(time, 'tzname', (tz_name, tz_name)), \ |
| 475 |
support.swap_attr(time, 'daylight', 1), \ |
| 476 |
support.swap_attr(time, 'tzset', lambda: None): |
| 477 |
time.tzname = (tz_name, tz_name) |
| 478 |
time.daylight = 1 |
| 479 |
tz_value = _strptime._strptime_time(tz_name, "%Z")[8] |
| 480 |
self.assertEqual(tz_value, -1, |
| 481 |
"%s lead to a timezone value of %s instead of -1 when " |
| 482 |
"time.daylight set to %s and passing in %s" % |
| 483 |
(time.tzname, tz_value, time.daylight, tz_name)) |
| 484 |
|
| 485 |
# NB: Does not roundtrip in some locales due to the ambiguity of |
| 486 |
# the date and time representation (bugs in locales?): |
| 487 |
# * Seconds are not included: bem_ZM, bokmal, ff_SN, nb_NO, nn_NO, |
| 488 |
# no_NO, norwegian, nynorsk. |
| 489 |
# * Hours are in 12-hour notation without AM/PM indication: hy_AM, |
| 490 |
# id_ID, ms_MY. |
| 491 |
# * Year is not included: ha_NG. |
| 492 |
# * Use non-Gregorian calendar: lo_LA, thai, th_TH. |
| 493 |
# On Windows: ar_IN, ar_SA, fa_IR, ps_AF. |
| 494 |
@run_with_locales('LC_TIME', 'C', 'en_US', 'fr_FR', 'de_DE', 'ja_JP', |
| 495 |
'he_IL', 'eu_ES', 'ar_AE', 'mfe_MU', 'yo_NG', |
| 496 |
'csb_PL', 'br_FR', 'gez_ET', 'brx_IN', |
| 497 |
'my_MM', 'or_IN', 'shn_MM', 'az_IR', |
| 498 |
'byn_ER', 'wal_ET', 'lzh_TW') |
| 499 |
def test_date_time_locale(self): |
| 500 |
# Test %c directive |
| 501 |
loc = locale.getlocale(locale.LC_TIME)[0] |
| 502 |
if glibc_ver and glibc_ver < (2, 31) and loc == 'br_FR': |
| 503 |
self.skipTest('%c in locale br_FR does not include time') |
| 504 |
now = time.time() |
| 505 |
self.roundtrip('%c', slice(0, 6), time.localtime(now)) |
| 506 |
# 1 hour 20 minutes 30 seconds ago |
| 507 |
self.roundtrip('%c', slice(0, 6), time.localtime(now - 4830)) |
| 508 |
# 12 hours ago |
| 509 |
self.roundtrip('%c', slice(0, 6), time.localtime(now - 12*3600)) |
| 510 |
# different days of the week |
| 511 |
for i in range(1, 7): |
| 512 |
self.roundtrip('%c', slice(0, 6), time.localtime(now - i*24*3600)) |
| 513 |
# different months |
| 514 |
for i in range(1, 12): |
| 515 |
self.roundtrip('%c', slice(0, 6), time.localtime(now - i*30*24*3600)) |
| 516 |
# different year |
| 517 |
self.roundtrip('%c', slice(0, 6), time.localtime(now - 366*24*3600)) |
| 518 |
|
| 519 |
# NB: Dates before 1969 do not roundtrip on some locales: |
| 520 |
# az_IR, bo_CN, bo_IN, dz_BT, eu_ES, eu_FR, fa_IR, or_IN. |
| 521 |
@support.run_with_tz('STD-1DST,M4.1.0,M10.1.0') |
| 522 |
@run_with_locales('LC_TIME', 'C', 'en_US', 'fr_FR', 'de_DE', 'ja_JP', |
| 523 |
'he_IL', 'ar_AE', 'mfe_MU', 'yo_NG', |
| 524 |
'csb_PL', 'br_FR', 'gez_ET', 'brx_IN', |
| 525 |
'my_MM', 'shn_MM') |
| 526 |
def test_date_time_locale2(self): |
| 527 |
# Test %c directive |
| 528 |
loc = locale.getlocale(locale.LC_TIME)[0] |
| 529 |
if sys.platform.startswith('sunos'): |
| 530 |
if loc in ('ar_AE',): |
| 531 |
self.skipTest(f'locale {loc!r} may not work on this platform') |
| 532 |
self.roundtrip('%c', slice(0, 6), (1900, 1, 1, 0, 0, 0, 0, 1, 0)) |
| 533 |
self.roundtrip('%c', slice(0, 6), (1800, 1, 1, 0, 0, 0, 0, 1, 0)) |
| 534 |
|
| 535 |
# NB: Does not roundtrip because use non-Gregorian calendar: |
| 536 |
# lo_LA, thai, th_TH. On Windows: ar_IN, ar_SA, fa_IR, ps_AF. |
| 537 |
@run_with_locales('LC_TIME', 'C', 'en_US', 'fr_FR', 'de_DE', 'ja_JP', |
| 538 |
'he_IL', 'eu_ES', 'ar_AE', |
| 539 |
'az_IR', 'my_MM', 'or_IN', 'shn_MM', 'lzh_TW') |
| 540 |
def test_date_locale(self): |
| 541 |
# Test %x directive |
| 542 |
now = time.time() |
| 543 |
self.roundtrip('%x', slice(0, 3), time.localtime(now)) |
| 544 |
# different days of the week |
| 545 |
for i in range(1, 7): |
| 546 |
self.roundtrip('%x', slice(0, 3), time.localtime(now - i*24*3600)) |
| 547 |
# different months |
| 548 |
for i in range(1, 12): |
| 549 |
self.roundtrip('%x', slice(0, 3), time.localtime(now - i*30*24*3600)) |
| 550 |
# different year |
| 551 |
self.roundtrip('%x', slice(0, 3), time.localtime(now - 366*24*3600)) |
| 552 |
|
| 553 |
# NB: Dates before 1969 do not roundtrip on many locales, including C. |
| 554 |
@unittest.skipIf(support.linked_to_musl(), "musl libc issue, bpo-46390") |
| 555 |
@run_with_locales('LC_TIME', 'en_US', 'fr_FR', 'de_DE', 'ja_JP', |
| 556 |
'eu_ES', 'ar_AE', 'my_MM', 'shn_MM', 'lzh_TW') |
| 557 |
def test_date_locale2(self): |
| 558 |
# Test %x directive |
| 559 |
loc = locale.getlocale(locale.LC_TIME)[0] |
| 560 |
if sys.platform.startswith(('sunos', 'aix')): |
| 561 |
if loc in ('en_US', 'de_DE', 'ar_AE'): |
| 562 |
self.skipTest(f'locale {loc!r} may not work on this platform') |
| 563 |
self.roundtrip('%x', slice(0, 3), (1900, 1, 1, 0, 0, 0, 0, 1, 0)) |
| 564 |
self.roundtrip('%x', slice(0, 3), (1800, 1, 1, 0, 0, 0, 0, 1, 0)) |
| 565 |
|
| 566 |
# NB: Does not roundtrip in some locales due to the ambiguity of |
| 567 |
# the time representation (bugs in locales?): |
| 568 |
# * Seconds are not included: bokmal, ff_SN, nb_NO, nn_NO, no_NO, |
| 569 |
# norwegian, nynorsk. |
| 570 |
# * Hours are in 12-hour notation without AM/PM indication: hy_AM, |
| 571 |
# ms_MY, sm_WS. |
| 572 |
@run_with_locales('LC_TIME', 'C', 'en_US', 'fr_FR', 'de_DE', 'ja_JP', |
| 573 |
'aa_ET', 'am_ET', 'az_IR', 'byn_ER', 'fa_IR', 'gez_ET', |
| 574 |
'my_MM', 'om_ET', 'or_IN', 'shn_MM', 'sid_ET', 'so_SO', |
| 575 |
'ti_ET', 'tig_ER', 'wal_ET', 'lzh_TW', |
| 576 |
'ar_SA', 'bg_BG') |
| 577 |
def test_time_locale(self): |
| 578 |
# Test %X directive |
| 579 |
loc = locale.getlocale(locale.LC_TIME)[0] |
| 580 |
pos = slice(3, 6) |
| 581 |
if glibc_ver and glibc_ver < (2, 29) and loc in { |
| 582 |
'aa_ET', 'am_ET', 'byn_ER', 'gez_ET', 'om_ET', |
| 583 |
'sid_ET', 'so_SO', 'ti_ET', 'tig_ER', 'wal_ET'}: |
| 584 |
# Hours are in 12-hour notation without AM/PM indication. |
| 585 |
# Ignore hours. |
| 586 |
pos = slice(4, 6) |
| 587 |
now = time.time() |
| 588 |
self.roundtrip('%X', pos, time.localtime(now)) |
| 589 |
# 1 hour 20 minutes 30 seconds ago |
| 590 |
self.roundtrip('%X', pos, time.localtime(now - 4830)) |
| 591 |
# 12 hours ago |
| 592 |
self.roundtrip('%X', pos, time.localtime(now - 12*3600)) |
| 593 |
|
| 594 |
def test_percent(self): |
| 595 |
# Make sure % signs are handled properly |
| 596 |
strf_output = time.strftime("%m %% %Y", self.time_tuple) |
| 597 |
strp_output = _strptime._strptime_time(strf_output, "%m %% %Y") |
| 598 |
self.assertTrue(strp_output[0] == self.time_tuple[0] and |
| 599 |
strp_output[1] == self.time_tuple[1], |
| 600 |
"handling of percent sign failed") |
| 601 |
|
| 602 |
def test_caseinsensitive(self): |
| 603 |
# Should handle names case-insensitively. |
| 604 |
strf_output = time.strftime("%B", self.time_tuple) |
| 605 |
self.assertTrue(_strptime._strptime_time(strf_output.upper(), "%B"), |
| 606 |
"strptime does not handle ALL-CAPS names properly") |
| 607 |
self.assertTrue(_strptime._strptime_time(strf_output.lower(), "%B"), |
| 608 |
"strptime does not handle lowercase names properly") |
| 609 |
self.assertTrue(_strptime._strptime_time(strf_output.capitalize(), "%B"), |
| 610 |
"strptime does not handle capword names properly") |
| 611 |
|
| 612 |
def test_defaults(self): |
| 613 |
# Default return value should be (1900, 1, 1, 0, 0, 0, 0, 1, 0) |
| 614 |
defaults = (1900, 1, 1, 0, 0, 0, 0, 1, -1) |
| 615 |
strp_output = _strptime._strptime_time('1', '%m') |
| 616 |
self.assertTrue(strp_output == defaults, |
| 617 |
"Default values for strptime() are incorrect;" |
| 618 |
" %s != %s" % (strp_output, defaults)) |
| 619 |
|
| 620 |
def test_escaping(self): |
| 621 |
# Make sure all characters that have regex significance are escaped. |
| 622 |
# Parentheses are in a purposeful order; will cause an error of |
| 623 |
# unbalanced parentheses when the regex is compiled if they are not |
| 624 |
# escaped. |
| 625 |
# Test instigated by bug #796149 . |
| 626 |
need_escaping = r".^$*+?{}\[]|)(" |
| 627 |
self.assertTrue(_strptime._strptime_time(need_escaping, need_escaping)) |
| 628 |
|
| 629 |
@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-70647 |
| 630 |
def test_feb29_on_leap_year_without_year(self): |
| 631 |
time.strptime("Feb 29", "%b %d") |
| 632 |
|
| 633 |
@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-70647 |
| 634 |
def test_mar1_comes_after_feb29_even_when_omitting_the_year(self): |
| 635 |
self.assertLess( |
| 636 |
time.strptime("Feb 29", "%b %d"), |
| 637 |
time.strptime("Mar 1", "%b %d")) |
| 638 |
|
| 639 |
class Strptime12AMPMTests(unittest.TestCase): |
| 640 |
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)""" |
| 641 |
|
| 642 |
def test_twelve_noon_midnight(self): |
| 643 |
eq = self.assertEqual |
| 644 |
eq(time.strptime('12 PM', '%I %p')[3], 12) |
| 645 |
eq(time.strptime('12 AM', '%I %p')[3], 0) |
| 646 |
eq(_strptime._strptime_time('12 PM', '%I %p')[3], 12) |
| 647 |
eq(_strptime._strptime_time('12 AM', '%I %p')[3], 0) |
| 648 |
|
| 649 |
|
| 650 |
class JulianTests(unittest.TestCase): |
| 651 |
"""Test a _strptime regression that all julian (1-366) are accepted""" |
| 652 |
|
| 653 |
def test_all_julian_days(self): |
| 654 |
eq = self.assertEqual |
| 655 |
for i in range(1, 367): |
| 656 |
# use 2004, since it is a leap year, we have 366 days |
| 657 |
eq(_strptime._strptime_time('%d 2004' % i, '%j %Y')[7], i) |
| 658 |
|
| 659 |
class CalculationTests(unittest.TestCase): |
| 660 |
"""Test that strptime() fills in missing info correctly""" |
| 661 |
|
| 662 |
def setUp(self): |
| 663 |
self.time_tuple = time.gmtime() |
| 664 |
|
| 665 |
@skip_if_buggy_ucrt_strfptime |
| 666 |
def test_julian_calculation(self): |
| 667 |
# Make sure that when Julian is missing that it is calculated |
| 668 |
format_string = "%Y %m %d %H %M %S %w %Z" |
| 669 |
result = _strptime._strptime_time(time.strftime(format_string, self.time_tuple), |
| 670 |
format_string) |
| 671 |
self.assertTrue(result.tm_yday == self.time_tuple.tm_yday, |
| 672 |
"Calculation of tm_yday failed; %s != %s" % |
| 673 |
(result.tm_yday, self.time_tuple.tm_yday)) |
| 674 |
|
| 675 |
@skip_if_buggy_ucrt_strfptime |
| 676 |
def test_gregorian_calculation(self): |
| 677 |
# Test that Gregorian date can be calculated from Julian day |
| 678 |
format_string = "%Y %H %M %S %w %j %Z" |
| 679 |
result = _strptime._strptime_time(time.strftime(format_string, self.time_tuple), |
| 680 |
format_string) |
| 681 |
self.assertTrue(result.tm_year == self.time_tuple.tm_year and |
| 682 |
result.tm_mon == self.time_tuple.tm_mon and |
| 683 |
result.tm_mday == self.time_tuple.tm_mday, |
| 684 |
"Calculation of Gregorian date failed; " |
| 685 |
"%s-%s-%s != %s-%s-%s" % |
| 686 |
(result.tm_year, result.tm_mon, result.tm_mday, |
| 687 |
self.time_tuple.tm_year, self.time_tuple.tm_mon, |
| 688 |
self.time_tuple.tm_mday)) |
| 689 |
|
| 690 |
@skip_if_buggy_ucrt_strfptime |
| 691 |
def test_day_of_week_calculation(self): |
| 692 |
# Test that the day of the week is calculated as needed |
| 693 |
format_string = "%Y %m %d %H %S %j %Z" |
| 694 |
result = _strptime._strptime_time(time.strftime(format_string, self.time_tuple), |
| 695 |
format_string) |
| 696 |
self.assertTrue(result.tm_wday == self.time_tuple.tm_wday, |
| 697 |
"Calculation of day of the week failed; " |
| 698 |
"%s != %s" % (result.tm_wday, self.time_tuple.tm_wday)) |
| 699 |
|
| 700 |
if support.is_android: |
| 701 |
# Issue #26929: strftime() on Android incorrectly formats %V or %G for |
| 702 |
# the last or the first incomplete week in a year. |
| 703 |
_ymd_excluded = ((1905, 1, 1), (1906, 12, 31), (2008, 12, 29), |
| 704 |
(1917, 12, 31)) |
| 705 |
_formats_excluded = ('%G %V',) |
| 706 |
else: |
| 707 |
_ymd_excluded = () |
| 708 |
_formats_excluded = () |
| 709 |
|
| 710 |
@unittest.skipIf(sys.platform.startswith('aix'), |
| 711 |
'bpo-29972: broken test on AIX') |
| 712 |
def test_week_of_year_and_day_of_week_calculation(self): |
| 713 |
# Should be able to infer date if given year, week of year (%U or %W) |
| 714 |
# and day of the week |
| 715 |
def test_helper(ymd_tuple, test_reason): |
| 716 |
for year_week_format in ('%Y %W', '%Y %U', '%G %V'): |
| 717 |
if (year_week_format in self._formats_excluded and |
| 718 |
ymd_tuple in self._ymd_excluded): |
| 719 |
return |
| 720 |
for weekday_format in ('%w', '%u', '%a', '%A'): |
| 721 |
format_string = year_week_format + ' ' + weekday_format |
| 722 |
with self.subTest(test_reason, |
| 723 |
date=ymd_tuple, |
| 724 |
format=format_string): |
| 725 |
dt_date = datetime_date(*ymd_tuple) |
| 726 |
strp_input = dt_date.strftime(format_string) |
| 727 |
strp_output = _strptime._strptime_time(strp_input, |
| 728 |
format_string) |
| 729 |
msg = "%r: %s != %s" % (strp_input, |
| 730 |
strp_output[7], |
| 731 |
dt_date.timetuple()[7]) |
| 732 |
self.assertEqual(strp_output[:3], ymd_tuple, msg) |
| 733 |
test_helper((1901, 1, 3), "week 0") |
| 734 |
test_helper((1901, 1, 8), "common case") |
| 735 |
test_helper((1901, 1, 13), "day on Sunday") |
| 736 |
test_helper((1901, 1, 14), "day on Monday") |
| 737 |
test_helper((1905, 1, 1), "Jan 1 on Sunday") |
| 738 |
test_helper((1906, 1, 1), "Jan 1 on Monday") |
| 739 |
test_helper((1906, 1, 7), "first Sunday in a year starting on Monday") |
| 740 |
test_helper((1905, 12, 31), "Dec 31 on Sunday") |
| 741 |
test_helper((1906, 12, 31), "Dec 31 on Monday") |
| 742 |
test_helper((2008, 12, 29), "Monday in the last week of the year") |
| 743 |
test_helper((2008, 12, 22), "Monday in the second-to-last week of the " |
| 744 |
"year") |
| 745 |
test_helper((1978, 10, 23), "randomly chosen date") |
| 746 |
test_helper((2004, 12, 18), "randomly chosen date") |
| 747 |
test_helper((1978, 10, 23), "year starting and ending on Monday while " |
| 748 |
"date not on Sunday or Monday") |
| 749 |
test_helper((1917, 12, 17), "year starting and ending on Monday with " |
| 750 |
"a Monday not at the beginning or end " |
| 751 |
"of the year") |
| 752 |
test_helper((1917, 12, 31), "Dec 31 on Monday with year starting and " |
| 753 |
"ending on Monday") |
| 754 |
test_helper((2007, 1, 7), "First Sunday of 2007") |
| 755 |
test_helper((2007, 1, 14), "Second Sunday of 2007") |
| 756 |
test_helper((2006, 12, 31), "Last Sunday of 2006") |
| 757 |
test_helper((2006, 12, 24), "Second to last Sunday of 2006") |
| 758 |
|
| 759 |
def test_week_0(self): |
| 760 |
def check(value, format, *expected): |
| 761 |
self.assertEqual(_strptime._strptime_time(value, format)[:-1], expected) |
| 762 |
check('2015 0 0', '%Y %U %w', 2014, 12, 28, 0, 0, 0, 6, 362) |
| 763 |
check('2015 0 0', '%Y %W %w', 2015, 1, 4, 0, 0, 0, 6, 4) |
| 764 |
check('2015 1 1', '%G %V %u', 2014, 12, 29, 0, 0, 0, 0, 363) |
| 765 |
check('2015 0 1', '%Y %U %w', 2014, 12, 29, 0, 0, 0, 0, 363) |
| 766 |
check('2015 0 1', '%Y %W %w', 2014, 12, 29, 0, 0, 0, 0, 363) |
| 767 |
check('2015 1 2', '%G %V %u', 2014, 12, 30, 0, 0, 0, 1, 364) |
| 768 |
check('2015 0 2', '%Y %U %w', 2014, 12, 30, 0, 0, 0, 1, 364) |
| 769 |
check('2015 0 2', '%Y %W %w', 2014, 12, 30, 0, 0, 0, 1, 364) |
| 770 |
check('2015 1 3', '%G %V %u', 2014, 12, 31, 0, 0, 0, 2, 365) |
| 771 |
check('2015 0 3', '%Y %U %w', 2014, 12, 31, 0, 0, 0, 2, 365) |
| 772 |
check('2015 0 3', '%Y %W %w', 2014, 12, 31, 0, 0, 0, 2, 365) |
| 773 |
check('2015 1 4', '%G %V %u', 2015, 1, 1, 0, 0, 0, 3, 1) |
| 774 |
check('2015 0 4', '%Y %U %w', 2015, 1, 1, 0, 0, 0, 3, 1) |
| 775 |
check('2015 0 4', '%Y %W %w', 2015, 1, 1, 0, 0, 0, 3, 1) |
| 776 |
check('2015 1 5', '%G %V %u', 2015, 1, 2, 0, 0, 0, 4, 2) |
| 777 |
check('2015 0 5', '%Y %U %w', 2015, 1, 2, 0, 0, 0, 4, 2) |
| 778 |
check('2015 0 5', '%Y %W %w', 2015, 1, 2, 0, 0, 0, 4, 2) |
| 779 |
check('2015 1 6', '%G %V %u', 2015, 1, 3, 0, 0, 0, 5, 3) |
| 780 |
check('2015 0 6', '%Y %U %w', 2015, 1, 3, 0, 0, 0, 5, 3) |
| 781 |
check('2015 0 6', '%Y %W %w', 2015, 1, 3, 0, 0, 0, 5, 3) |
| 782 |
check('2015 1 7', '%G %V %u', 2015, 1, 4, 0, 0, 0, 6, 4) |
| 783 |
|
| 784 |
check('2009 0 0', '%Y %U %w', 2008, 12, 28, 0, 0, 0, 6, 363) |
| 785 |
check('2009 0 0', '%Y %W %w', 2009, 1, 4, 0, 0, 0, 6, 4) |
| 786 |
check('2009 1 1', '%G %V %u', 2008, 12, 29, 0, 0, 0, 0, 364) |
| 787 |
check('2009 0 1', '%Y %U %w', 2008, 12, 29, 0, 0, 0, 0, 364) |
| 788 |
check('2009 0 1', '%Y %W %w', 2008, 12, 29, 0, 0, 0, 0, 364) |
| 789 |
check('2009 1 2', '%G %V %u', 2008, 12, 30, 0, 0, 0, 1, 365) |
| 790 |
check('2009 0 2', '%Y %U %w', 2008, 12, 30, 0, 0, 0, 1, 365) |
| 791 |
check('2009 0 2', '%Y %W %w', 2008, 12, 30, 0, 0, 0, 1, 365) |
| 792 |
check('2009 1 3', '%G %V %u', 2008, 12, 31, 0, 0, 0, 2, 366) |
| 793 |
check('2009 0 3', '%Y %U %w', 2008, 12, 31, 0, 0, 0, 2, 366) |
| 794 |
check('2009 0 3', '%Y %W %w', 2008, 12, 31, 0, 0, 0, 2, 366) |
| 795 |
check('2009 1 4', '%G %V %u', 2009, 1, 1, 0, 0, 0, 3, 1) |
| 796 |
check('2009 0 4', '%Y %U %w', 2009, 1, 1, 0, 0, 0, 3, 1) |
| 797 |
check('2009 0 4', '%Y %W %w', 2009, 1, 1, 0, 0, 0, 3, 1) |
| 798 |
check('2009 1 5', '%G %V %u', 2009, 1, 2, 0, 0, 0, 4, 2) |
| 799 |
check('2009 0 5', '%Y %U %w', 2009, 1, 2, 0, 0, 0, 4, 2) |
| 800 |
check('2009 0 5', '%Y %W %w', 2009, 1, 2, 0, 0, 0, 4, 2) |
| 801 |
check('2009 1 6', '%G %V %u', 2009, 1, 3, 0, 0, 0, 5, 3) |
| 802 |
check('2009 0 6', '%Y %U %w', 2009, 1, 3, 0, 0, 0, 5, 3) |
| 803 |
check('2009 0 6', '%Y %W %w', 2009, 1, 3, 0, 0, 0, 5, 3) |
| 804 |
check('2009 1 7', '%G %V %u', 2009, 1, 4, 0, 0, 0, 6, 4) |
| 805 |
|
| 806 |
|
| 807 |
class CacheTests(unittest.TestCase): |
| 808 |
"""Test that caching works properly.""" |
| 809 |
|
| 810 |
def test_time_re_recreation(self): |
| 811 |
# Make sure cache is recreated when current locale does not match what |
| 812 |
# cached object was created with. |
| 813 |
_strptime._strptime_time("10 2004", "%d %Y") |
| 814 |
_strptime._strptime_time("2005", "%Y") |
| 815 |
_strptime._TimeRE_cache.locale_time.lang = "Ni" |
| 816 |
original_time_re = _strptime._TimeRE_cache |
| 817 |
_strptime._strptime_time("10 2004", "%d %Y") |
| 818 |
self.assertIsNot(original_time_re, _strptime._TimeRE_cache) |
| 819 |
self.assertEqual(len(_strptime._regex_cache), 1) |
| 820 |
|
| 821 |
def test_regex_cleanup(self): |
| 822 |
# Make sure cached regexes are discarded when cache becomes "full". |
| 823 |
try: |
| 824 |
del _strptime._regex_cache['%d %Y'] |
| 825 |
except KeyError: |
| 826 |
pass |
| 827 |
bogus_key = 0 |
| 828 |
while len(_strptime._regex_cache) <= _strptime._CACHE_MAX_SIZE: |
| 829 |
_strptime._regex_cache[bogus_key] = None |
| 830 |
bogus_key += 1 |
| 831 |
_strptime._strptime_time("10 2004", "%d %Y") |
| 832 |
self.assertEqual(len(_strptime._regex_cache), 1) |
| 833 |
|
| 834 |
def test_new_localetime(self): |
| 835 |
# A new LocaleTime instance should be created when a new TimeRE object |
| 836 |
# is created. |
| 837 |
locale_time_id = _strptime._TimeRE_cache.locale_time |
| 838 |
_strptime._TimeRE_cache.locale_time.lang = "Ni" |
| 839 |
_strptime._strptime_time("10 2004", "%d %Y") |
| 840 |
self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time) |
| 841 |
|
| 842 |
def test_TimeRE_recreation_locale(self): |
| 843 |
# The TimeRE instance should be recreated upon changing the locale. |
| 844 |
with support.run_with_locale('LC_TIME', 'en_US.UTF8'): |
| 845 |
_strptime._strptime_time('10 2004', '%d %Y') |
| 846 |
# Get id of current cache object. |
| 847 |
first_time_re = _strptime._TimeRE_cache |
| 848 |
try: |
| 849 |
# Change the locale and force a recreation of the cache. |
| 850 |
locale.setlocale(locale.LC_TIME, ('de_DE', 'UTF8')) |
| 851 |
_strptime._strptime_time('10 2004', '%d %Y') |
| 852 |
# Get the new cache object's id. |
| 853 |
second_time_re = _strptime._TimeRE_cache |
| 854 |
# They should not be equal. |
| 855 |
self.assertIsNot(first_time_re, second_time_re) |
| 856 |
# Possible test locale is not supported while initial locale is. |
| 857 |
# If this is the case just suppress the exception and fall-through |
| 858 |
# to the resetting to the original locale. |
| 859 |
except locale.Error: |
| 860 |
self.skipTest('test needs de_DE.UTF8 locale') |
| 861 |
|
| 862 |
@support.run_with_tz('STD-1DST,M4.1.0,M10.1.0') |
| 863 |
def test_TimeRE_recreation_timezone(self): |
| 864 |
# The TimeRE instance should be recreated upon changing the timezone. |
| 865 |
oldtzname = time.tzname |
| 866 |
tm = _strptime._strptime_time(time.tzname[0], '%Z') |
| 867 |
self.assertEqual(tm.tm_isdst, 0) |
| 868 |
tm = _strptime._strptime_time(time.tzname[1], '%Z') |
| 869 |
self.assertEqual(tm.tm_isdst, 1) |
| 870 |
# Get id of current cache object. |
| 871 |
first_time_re = _strptime._TimeRE_cache |
| 872 |
# Change the timezone and force a recreation of the cache. |
| 873 |
os.environ['TZ'] = 'EST+05EDT,M3.2.0,M11.1.0' |
| 874 |
time.tzset() |
| 875 |
tm = _strptime._strptime_time(time.tzname[0], '%Z') |
| 876 |
self.assertEqual(tm.tm_isdst, 0) |
| 877 |
tm = _strptime._strptime_time(time.tzname[1], '%Z') |
| 878 |
self.assertEqual(tm.tm_isdst, 1) |
| 879 |
# Get the new cache object's id. |
| 880 |
second_time_re = _strptime._TimeRE_cache |
| 881 |
# They should not be equal. |
| 882 |
self.assertIsNot(first_time_re, second_time_re) |
| 883 |
# Make sure old names no longer accepted. |
| 884 |
with self.assertRaises(ValueError): |
| 885 |
_strptime._strptime_time(oldtzname[0], '%Z') |
| 886 |
with self.assertRaises(ValueError): |
| 887 |
_strptime._strptime_time(oldtzname[1], '%Z') |
| 888 |
|
| 889 |
|
| 890 |
if __name__ == '__main__': |
| 891 |
unittest.main() |