def test_by_hand_input(self): # Test passed-in initialization value checks self.failUnless(_strptime.LocaleTime(f_weekday=range(7)), "Argument size check for f_weekday failed") self.assertRaises(TypeError, _strptime.LocaleTime, f_weekday=range(8)) self.assertRaises(TypeError, _strptime.LocaleTime, f_weekday=range(6)) self.failUnless(_strptime.LocaleTime(a_weekday=range(7)), "Argument size check for a_weekday failed") self.assertRaises(TypeError, _strptime.LocaleTime, a_weekday=range(8)) self.assertRaises(TypeError, _strptime.LocaleTime, a_weekday=range(6)) self.failUnless(_strptime.LocaleTime(f_month=range(12)), "Argument size check for f_month failed") self.assertRaises(TypeError, _strptime.LocaleTime, f_month=range(11)) self.assertRaises(TypeError, _strptime.LocaleTime, f_month=range(13)) self.failUnless( len(_strptime.LocaleTime(f_month=range(12)).f_month) == 13, "dummy value for f_month not added") self.failUnless(_strptime.LocaleTime(a_month=range(12)), "Argument size check for a_month failed") self.assertRaises(TypeError, _strptime.LocaleTime, a_month=range(11)) self.assertRaises(TypeError, _strptime.LocaleTime, a_month=range(13)) self.failUnless( len(_strptime.LocaleTime(a_month=range(12)).a_month) == 13, "dummy value for a_month not added") self.failUnless(_strptime.LocaleTime(am_pm=range(2)), "Argument size check for am_pm failed") self.assertRaises(TypeError, _strptime.LocaleTime, am_pm=range(1)) self.assertRaises(TypeError, _strptime.LocaleTime, am_pm=range(3)) self.failUnless(_strptime.LocaleTime(timezone=range(2)), "Argument size check for timezone failed") self.assertRaises(TypeError, _strptime.LocaleTime, timezone=range(1)) self.assertRaises(TypeError, _strptime.LocaleTime, timezone=range(3))
def test_blankpattern(self): # Make sure when tuple or something has no values no regex is generated. # Fixes bug #661354 test_locale = _strptime.LocaleTime(timezone=('', '')) self.failUnless( _strptime.TimeRE(test_locale).pattern("%Z") == '', "with timezone == ('',''), TimeRE().pattern('%Z') != ''")
def test_timezone(self): # Test timezone directives. # When gmtime() is used with %Z, entire result of strftime() is empty. # Check for equal timezone names deals with bad locale info when this # occurs; first found in FreeBSD 4.4. strp_output = _strptime.strptime("UTC", "%Z") self.failUnlessEqual(strp_output.tm_isdst, 0) strp_output = _strptime.strptime("GMT", "%Z") self.failUnlessEqual(strp_output.tm_isdst, 0) if sys.platform == "mac": # Timezones don't really work on MacOS9 return time_tuple = time.localtime() strf_output = time.strftime("%Z") #UTC does not have a timezone strp_output = _strptime.strptime(strf_output, "%Z") locale_time = _strptime.LocaleTime() if time.tzname[0] != time.tzname[1] or not time.daylight: self.failUnless( strp_output[8] == time_tuple[8], "timezone check failed; '%s' -> %s != %s" % (strf_output, strp_output[8], time_tuple[8])) else: self.failUnless( strp_output[8] == -1, "LocaleTime().timezone has duplicate values and " "time.daylight but timezone value not set to -1")
def test_date_time(self): # Check that LC_date_time, LC_date, and LC_time are correct # the magic date is used so as to not have issues with %c when day of # the month is a single digit and has a leading space. This is not an # issue since strptime still parses it correctly. The problem is # testing these directives for correctness by comparing strftime # output. magic_date = (1999, 3, 17, 22, 44, 55, 2, 76, 0) strftime_output = time.strftime("%c", magic_date) self.assertTrue( strftime_output == time.strftime(self.LT_ins.LC_date_time, magic_date), "LC_date_time incorrect") strftime_output = time.strftime("%x", magic_date) self.assertTrue( strftime_output == time.strftime(self.LT_ins.LC_date, magic_date), "LC_date incorrect") strftime_output = time.strftime("%X", magic_date) self.assertTrue( strftime_output == time.strftime(self.LT_ins.LC_time, magic_date), "LC_time incorrect") LT = _strptime.LocaleTime() LT.am_pm = ('', '') self.assertTrue( LT.LC_time, "LocaleTime's LC directives cannot handle " "empty strings")
def test_timezone(self): # Test timezone directives. # When gmtime() is used with %Z, entire result of strftime() is empty. # Check for equal timezone names deals with bad locale info when this # occurs; first found in FreeBSD 4.4. strp_output = _strptime._strptime_time("UTC", "%Z") self.assertEqual(strp_output.tm_isdst, 0) strp_output = _strptime._strptime_time("GMT", "%Z") self.assertEqual(strp_output.tm_isdst, 0) if test_support.due_to_ironpython_bug( "http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=409557" ): return time_tuple = time.localtime() strf_output = time.strftime("%Z") #UTC does not have a timezone strp_output = _strptime._strptime_time(strf_output, "%Z") locale_time = _strptime.LocaleTime() if time.tzname[0] != time.tzname[1] or not time.daylight: self.assertTrue( strp_output[8] == time_tuple[8], "timezone check failed; '%s' -> %s != %s" % (strf_output, strp_output[8], time_tuple[8])) else: self.assertTrue( strp_output[8] == -1, "LocaleTime().timezone has duplicate values and " "time.daylight but timezone value not set to -1")
def test_blankpattern(self): # Make sure when tuple or something has no values no regex is generated. # Fixes bug #661354 test_locale = _strptime.LocaleTime() test_locale.timezone = (frozenset(), frozenset()) self.assertEqual(_strptime.TimeRE(test_locale).pattern("%Z"), '', "with timezone == ('',''), TimeRE().pattern('%Z') != ''")
def test_unknowntimezone(self): # Handle timezone set to ('','') properly. # Fixes bug #661354 locale_time = _strptime.LocaleTime(timezone=('', '')) self.failUnless( "%Z" not in locale_time.LC_date, "when timezone == ('',''), string.replace('','%Z') is " "occuring")
def test_time_re_recreation(self): # Make sure cache is recreated when current locale does not match what # cached object was created with. _strptime.strptime("10", "%d") _strptime._locale_cache.locale_time = _strptime.LocaleTime(lang="Ni") original_time_re = id(_strptime._locale_cache) _strptime.strptime("10", "%d") self.failIfEqual(original_time_re, id(_strptime._locale_cache))
def test_locale_data_w_regex_metacharacters(self): locale_time = _strptime.LocaleTime() locale_time.timezone = frozenset(('utc', 'gmt', 'Tokyo (standard time)')), frozenset('Tokyo (daylight time)') time_re = _strptime.TimeRE(locale_time) self.assertTrue(time_re.compile('%Z').match('Tokyo (standard time)' ), 'locale data that contains regex metacharacters is not properly escaped' )
def test_new_localetime(self): # A new LocaleTime instance should be created when a new TimeRE object # is created. _strptime._locale_cache.locale_time = _strptime.LocaleTime(lang="Ni") locale_time_id = id(_strptime._locale_cache.locale_time) locale_time_lang = _strptime._locale_cache.locale_time.lang _strptime.strptime("10", "%d") self.failIfEqual(locale_time_id, id(_strptime._locale_cache.locale_time)) self.failIfEqual(locale_time_lang, _strptime._locale_cache.locale_time.lang)
def test_locale_data_w_regex_metacharacters(self): # Check that if locale data contains regex metacharacters they are # escaped properly. # Discovered by bug #1039270 . locale_time = _strptime.LocaleTime() locale_time.timezone = (frozenset(("utc", "gmt", "Tokyo (standard time)")), frozenset("Tokyo (daylight time)")) time_re = _strptime.TimeRE(locale_time) self.assertTrue(time_re.compile("%Z").match("Tokyo (standard time)"), "locale data that contains regex metacharacters is not" " properly escaped")
def test_date_time(self): magic_date = 1999, 3, 17, 22, 44, 55, 2, 76, 0 strftime_output = time.strftime('%c', magic_date) self.assertEqual(time.strftime(self.LT_ins.LC_date_time, magic_date ), strftime_output, 'LC_date_time incorrect') strftime_output = time.strftime('%x', magic_date) self.assertEqual(time.strftime(self.LT_ins.LC_date, magic_date), strftime_output, 'LC_date incorrect') strftime_output = time.strftime('%X', magic_date) self.assertEqual(time.strftime(self.LT_ins.LC_time, magic_date), strftime_output, 'LC_time incorrect') LT = _strptime.LocaleTime() LT.am_pm = '', '' self.assertTrue(LT.LC_time, "LocaleTime's LC directives cannot handle empty strings")
def test_timezone(self): strp_output = _strptime._strptime_time('UTC', '%Z') self.assertEqual(strp_output.tm_isdst, 0) strp_output = _strptime._strptime_time('GMT', '%Z') self.assertEqual(strp_output.tm_isdst, 0) time_tuple = time.localtime() strf_output = time.strftime('%Z') strp_output = _strptime._strptime_time(strf_output, '%Z') locale_time = _strptime.LocaleTime() if time.tzname[0] != time.tzname[1] or not time.daylight: self.assertTrue(strp_output[8] == time_tuple[8], "timezone check failed; '%s' -> %s != %s" % (strf_output, strp_output[8], time_tuple[8])) else: self.assertTrue(strp_output[8] == -1, 'LocaleTime().timezone has duplicate values and time.daylight but timezone value not set to -1' )
def test_bad_timezone(self): # Explicitly test possibility of bad timezone; # when time.tzname[0] == time.tzname[1] and time.daylight if sys.platform == "mac": return # MacOS9 has severely broken timezone support. try: original_tzname = time.tzname original_daylight = time.daylight time.tzname = ("PDT", "PDT") time.daylight = 1 # Need to make sure that timezone is not calculated since that # calls time.tzset and overrides temporary changes to time . _strptime._locale_cache = _strptime.TimeRE( _strptime.LocaleTime(timezone=("PDT", "PDT"))) _strptime._regex_cache.clear() tz_value = _strptime.strptime("PDT", "%Z")[8] self.failUnlessEqual(tz_value, -1) finally: time.tzname = original_tzname time.daylight = original_daylight _strptime._locale_cache = _strptime.TimeRE() _strptime._regex_cache.clear()
def setUp(self): """Construct generic TimeRE object.""" self.time_re = _strptime.TimeRE() self.locale_time = _strptime.LocaleTime()
def __init__(self, locale_time=None): """Create keys/values. Order of execution is important for dependency reasons. """ if locale_time: self.locale_time = locale_time else: self.locale_time = _actual_strptime.LocaleTime() base = super(_actual_strptime.TimeRE, self) base.__init__({ # The " \d" part of the regex is to make %c from ANSI C work 'd': r"(?P<d>3[0-2]|[1-2]\d|0[1-9]|[1-9]| [1-9])", 'f': r"(?P<f>[0-9]{1,6})", 'H': r"(?P<H>2[0-3]|[0-1]\d|\d)", 'I': r"(?P<I>1[0-2]|0[1-9]|[1-9])", '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])", 'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])", 'M': r"(?P<M>[0-5]\d|\d)", 'S': r"(?P<S>6[0-1]|[0-5]\d|\d)", 'U': r"(?P<U>5[0-3]|[0-4]\d|\d)", 'w': r"(?P<w>[0-6])", # W is set below by using 'U' 'y': r"(?P<y>\d\d)", # XXX: Does 'Y' need to worry about having less or more than # 4 digits? 'Y': r"(?P<Y>\d\d\d\d)", 'z': r"(?P<z>[+-]\d\d[0-5]\d)", 'A': self.__seqToRE(self.locale_time.f_weekday, 'A'), 'a': self.__seqToRE(self.locale_time.a_weekday, 'a'), 'B': self.__seqToRE(_FULLMONTHNAMES[1:], 'B'), 'b': self.__seqToRE(_MONTHNAMES[1:], 'b'), 'p': self.__seqToRE(self.locale_time.am_pm, 'p'), 'Z': self.__seqToRE((tz for tz_names in self.locale_time.timezone for tz in tz_names), 'Z'), '%': '%' }) base.__setitem__('W', base.__getitem__('U').replace('U', 'W')) base.__setitem__('c', self.pattern(self.locale_time.LC_date_time)) base.__setitem__('x', self.pattern(self.locale_time.LC_date)) base.__setitem__('X', self.pattern(self.locale_time.LC_time))
def __init__(self): self.locale_time = _strptime_module.LocaleTime() self.locale_time.LC_tznames = time.tzname super(Current_LangSet, self).__init__()
def setUp(self): """Create time tuple based on current time.""" self.time_tuple = time.localtime() self.LT_ins = _strptime.LocaleTime()
def test_blankpattern(self): test_locale = _strptime.LocaleTime() test_locale.timezone = frozenset(), frozenset() self.assertEqual(_strptime.TimeRE(test_locale).pattern('%Z'), '', "with timezone == ('',''), TimeRE().pattern('%Z') != ''")