14adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao"""PyUnit testing against strptime"""
24adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
34adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport unittest
44adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport time
54adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport locale
64adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport re
74adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport sys
84adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaofrom test import test_support
94adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaofrom datetime import date as datetime_date
104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport _strptime
124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass getlang_Tests(unittest.TestCase):
144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """Test _getlang"""
154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_basic(self):
164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(_strptime._getlang(), locale.getlocale(locale.LC_TIME))
174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass LocaleTime_Tests(unittest.TestCase):
194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """Tests for _strptime.LocaleTime.
204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    All values are lower-cased when stored in LocaleTime, so make sure to
224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    compare values after running ``lower`` on them.
234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """
254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def setUp(self):
274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Create time tuple based on current time."""
284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.time_tuple = time.localtime()
294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.LT_ins = _strptime.LocaleTime()
304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def compare_against_time(self, testing, directive, tuple_position,
324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                             error_msg):
334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Helper method that tests testing against directive based on the
344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        tuple_position of time_tuple.  Uses error_msg as error message.
354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strftime_output = time.strftime(directive, self.time_tuple).lower()
384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        comparison = testing[self.time_tuple[tuple_position]]
394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertIn(strftime_output, testing,
404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                      "%s: not found in tuple" % error_msg)
414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(comparison, strftime_output,
424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         "%s: position within tuple incorrect; %s != %s" %
434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         (error_msg, comparison, strftime_output))
444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_weekday(self):
464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure that full and abbreviated weekday names are correct in
474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # both string and position with tuple
484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.compare_against_time(self.LT_ins.f_weekday, '%A', 6,
494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                  "Testing of full weekday name failed")
504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.compare_against_time(self.LT_ins.a_weekday, '%a', 6,
514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                  "Testing of abbreviated weekday name failed")
524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_month(self):
544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test full and abbreviated month names; both string and position
554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # within the tuple
564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.compare_against_time(self.LT_ins.f_month, '%B', 1,
574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                  "Testing against full month name failed")
584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.compare_against_time(self.LT_ins.a_month, '%b', 1,
594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                  "Testing against abbreviated month name failed")
604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_am_pm(self):
624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure AM/PM representation done properly
634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strftime_output = time.strftime("%p", self.time_tuple).lower()
644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertIn(strftime_output, self.LT_ins.am_pm,
654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                      "AM/PM representation not in tuple")
664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if self.time_tuple[3] < 12: position = 0
674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        else: position = 1
684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(self.LT_ins.am_pm[position], strftime_output,
694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         "AM/PM representation in the wrong position within the tuple")
704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_timezone(self):
724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure timezone is correct
734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        timezone = time.strftime("%Z", self.time_tuple).lower()
744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if timezone:
754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.assertTrue(timezone in self.LT_ins.timezone[0] or
764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            timezone in self.LT_ins.timezone[1],
774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            "timezone %s not found in %s" %
784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            (timezone, self.LT_ins.timezone))
794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_date_time(self):
814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Check that LC_date_time, LC_date, and LC_time are correct
824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # the magic date is used so as to not have issues with %c when day of
834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        #  the month is a single digit and has a leading space.  This is not an
844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        #  issue since strptime still parses it correctly.  The problem is
854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        #  testing these directives for correctness by comparing strftime
864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        #  output.
874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        magic_date = (1999, 3, 17, 22, 44, 55, 2, 76, 0)
884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strftime_output = time.strftime("%c", magic_date)
894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(time.strftime(self.LT_ins.LC_date_time, magic_date),
904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         strftime_output, "LC_date_time incorrect")
914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strftime_output = time.strftime("%x", magic_date)
924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(time.strftime(self.LT_ins.LC_date, magic_date),
934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         strftime_output, "LC_date incorrect")
944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strftime_output = time.strftime("%X", magic_date)
954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(time.strftime(self.LT_ins.LC_time, magic_date),
964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         strftime_output, "LC_time incorrect")
974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        LT = _strptime.LocaleTime()
984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        LT.am_pm = ('', '')
994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(LT.LC_time, "LocaleTime's LC directives cannot handle "
1004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                    "empty strings")
1014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_lang(self):
1034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure lang is set to what _getlang() returns
1044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Assuming locale has not changed between now and when self.LT_ins was created
1054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(self.LT_ins.lang, _strptime._getlang())
1064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass TimeRETests(unittest.TestCase):
1094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """Tests for TimeRE."""
1104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def setUp(self):
1124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Construct generic TimeRE object."""
1134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.time_re = _strptime.TimeRE()
1144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.locale_time = _strptime.LocaleTime()
1154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_pattern(self):
1174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test TimeRE.pattern
1184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        pattern_string = self.time_re.pattern(r"%a %A %d")
1194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(pattern_string.find(self.locale_time.a_weekday[2]) != -1,
1204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "did not find abbreviated weekday in pattern string '%s'" %
1214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         pattern_string)
1224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(pattern_string.find(self.locale_time.f_weekday[4]) != -1,
1234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "did not find full weekday in pattern string '%s'" %
1244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         pattern_string)
1254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(pattern_string.find(self.time_re['d']) != -1,
1264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "did not find 'd' directive pattern string '%s'" %
1274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         pattern_string)
1284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_pattern_escaping(self):
1304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure any characters in the format string that might be taken as
1314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # regex syntax is escaped.
1324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        pattern_string = self.time_re.pattern("\d+")
1334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertIn(r"\\d\+", pattern_string,
1344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                      "%s does not have re characters escaped properly" %
1354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                      pattern_string)
1364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_compile(self):
1384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Check that compiled regex is correct
1394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        found = self.time_re.compile(r"%A").match(self.locale_time.f_weekday[6])
1404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(found and found.group('A') == self.locale_time.f_weekday[6],
1414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "re object for '%A' failed")
1424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        compiled = self.time_re.compile(r"%a %b")
1434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        found = compiled.match("%s %s" % (self.locale_time.a_weekday[4],
1444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                               self.locale_time.a_month[4]))
1454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(found,
1464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            "Match failed with '%s' regex and '%s' string" %
1474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao             (compiled.pattern, "%s %s" % (self.locale_time.a_weekday[4],
1484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                           self.locale_time.a_month[4])))
1494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(found.group('a') == self.locale_time.a_weekday[4] and
1504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         found.group('b') == self.locale_time.a_month[4],
1514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "re object couldn't find the abbreviated weekday month in "
1524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         "'%s' using '%s'; group 'a' = '%s', group 'b' = %s'" %
1534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         (found.string, found.re.pattern, found.group('a'),
1544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                          found.group('b')))
1554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for directive in ('a','A','b','B','c','d','H','I','j','m','M','p','S',
1564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                          'U','w','W','x','X','y','Y','Z','%'):
1574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            compiled = self.time_re.compile("%" + directive)
1584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            found = compiled.match(time.strftime("%" + directive))
1594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.assertTrue(found, "Matching failed on '%s' using '%s' regex" %
1604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                    (time.strftime("%" + directive),
1614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                     compiled.pattern))
1624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_blankpattern(self):
1644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure when tuple or something has no values no regex is generated.
1654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Fixes bug #661354
1664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_locale = _strptime.LocaleTime()
1674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_locale.timezone = (frozenset(), frozenset())
1684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(_strptime.TimeRE(test_locale).pattern("%Z"), '',
1694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         "with timezone == ('',''), TimeRE().pattern('%Z') != ''")
1704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_matching_with_escapes(self):
1724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure a format that requires escaping of characters works
1734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        compiled_re = self.time_re.compile("\w+ %m")
1744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        found = compiled_re.match("\w+ 10")
1754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(found, "Escaping failed of format '\w+ 10'")
1764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_locale_data_w_regex_metacharacters(self):
1784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Check that if locale data contains regex metacharacters they are
1794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # escaped properly.
1804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Discovered by bug #1039270 .
1814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        locale_time = _strptime.LocaleTime()
1824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        locale_time.timezone = (frozenset(("utc", "gmt",
1834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                            "Tokyo (standard time)")),
1844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                frozenset("Tokyo (daylight time)"))
1854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        time_re = _strptime.TimeRE(locale_time)
1864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(time_re.compile("%Z").match("Tokyo (standard time)"),
1874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "locale data that contains regex metacharacters is not"
1884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        " properly escaped")
1894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_whitespace_substitution(self):
1914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # When pattern contains whitespace, make sure it is taken into account
1924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # so as to not allow to subpatterns to end up next to each other and
1934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # "steal" characters from each other.
1944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        pattern = self.time_re.pattern('%j %H')
1954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertFalse(re.match(pattern, "180"))
1964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(re.match(pattern, "18 0"))
1974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass StrptimeTests(unittest.TestCase):
2004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """Tests for _strptime.strptime."""
2014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def setUp(self):
2034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Create testing time tuple."""
2044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.time_tuple = time.gmtime()
2054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_ValueError(self):
2074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure ValueError is raised when match fails or format is bad
2084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertRaises(ValueError, _strptime._strptime_time, data_string="%d",
2094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                          format="%A")
2104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for bad_format in ("%", "% ", "%e"):
2114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            try:
2124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                _strptime._strptime_time("2005", bad_format)
2134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            except ValueError:
2144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                continue
2154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            except Exception, err:
2164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                self.fail("'%s' raised %s, not ValueError" %
2174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            (bad_format, err.__class__.__name__))
2184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            else:
2194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                self.fail("'%s' did not raise ValueError" % bad_format)
2204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_unconverteddata(self):
2224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Check ValueError is raised when there is unconverted data
2234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertRaises(ValueError, _strptime._strptime_time, "10 12", "%m")
2244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def helper(self, directive, position):
2264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Helper fxn in testing."""
2274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strf_output = time.strftime("%" + directive, self.time_tuple)
2284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strp_output = _strptime._strptime_time(strf_output, "%" + directive)
2294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(strp_output[position] == self.time_tuple[position],
2304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "testing of '%s' directive failed; '%s' -> %s != %s" %
2314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         (directive, strf_output, strp_output[position],
2324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                          self.time_tuple[position]))
2334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_year(self):
2354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test that the year is handled properly
2364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for directive in ('y', 'Y'):
2374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.helper(directive, 0)
2384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Must also make sure %y values are correct for bounds set by Open Group
2394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for century, bounds in ((1900, ('69', '99')), (2000, ('00', '68'))):
2404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            for bound in bounds:
2414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                strp_output = _strptime._strptime_time(bound, '%y')
2424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                expected_result = century + int(bound)
2434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                self.assertTrue(strp_output[0] == expected_result,
2444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                "'y' test failed; passed in '%s' "
2454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                "and returned '%s'" % (bound, strp_output[0]))
2464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_month(self):
2484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test for month directives
2494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for directive in ('B', 'b', 'm'):
2504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.helper(directive, 1)
2514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_day(self):
2534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test for day directives
2544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.helper('d', 2)
2554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_hour(self):
2574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test hour directives
2584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.helper('H', 3)
2594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strf_output = time.strftime("%I %p", self.time_tuple)
2604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strp_output = _strptime._strptime_time(strf_output, "%I %p")
2614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(strp_output[3] == self.time_tuple[3],
2624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "testing of '%%I %%p' directive failed; '%s' -> %s != %s" %
2634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         (strf_output, strp_output[3], self.time_tuple[3]))
2644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_minute(self):
2664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test minute directives
2674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.helper('M', 4)
2684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_second(self):
2704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test second directives
2714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.helper('S', 5)
2724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_fraction(self):
2744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test microseconds
2754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        import datetime
2764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        d = datetime.datetime(2012, 12, 20, 12, 34, 56, 78987)
2774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        tup, frac = _strptime._strptime(str(d), format="%Y-%m-%d %H:%M:%S.%f")
2784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(frac, d.microsecond)
2794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_weekday(self):
2814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test weekday directives
2824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for directive in ('A', 'a', 'w'):
2834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.helper(directive,6)
2844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_julian(self):
2864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test julian directives
2874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.helper('j', 7)
2884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_timezone(self):
2904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test timezone directives.
2914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # When gmtime() is used with %Z, entire result of strftime() is empty.
2924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Check for equal timezone names deals with bad locale info when this
2934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # occurs; first found in FreeBSD 4.4.
2944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strp_output = _strptime._strptime_time("UTC", "%Z")
2954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(strp_output.tm_isdst, 0)
2964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strp_output = _strptime._strptime_time("GMT", "%Z")
2974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(strp_output.tm_isdst, 0)
2984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        time_tuple = time.localtime()
2994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strf_output = time.strftime("%Z")  #UTC does not have a timezone
3004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strp_output = _strptime._strptime_time(strf_output, "%Z")
3014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        locale_time = _strptime.LocaleTime()
3024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if time.tzname[0] != time.tzname[1] or not time.daylight:
3034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.assertTrue(strp_output[8] == time_tuple[8],
3044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            "timezone check failed; '%s' -> %s != %s" %
3054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                             (strf_output, strp_output[8], time_tuple[8]))
3064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        else:
3074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.assertTrue(strp_output[8] == -1,
3084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            "LocaleTime().timezone has duplicate values and "
3094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                             "time.daylight but timezone value not set to -1")
3104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_bad_timezone(self):
3124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Explicitly test possibility of bad timezone;
3134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # when time.tzname[0] == time.tzname[1] and time.daylight
3144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        tz_name = time.tzname[0]
3154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if tz_name.upper() in ("UTC", "GMT"):
3164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            return
3174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        try:
3184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            original_tzname = time.tzname
3194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            original_daylight = time.daylight
3204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            time.tzname = (tz_name, tz_name)
3214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            time.daylight = 1
3224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            tz_value = _strptime._strptime_time(tz_name, "%Z")[8]
3234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.assertEqual(tz_value, -1,
3244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    "%s lead to a timezone value of %s instead of -1 when "
3254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    "time.daylight set to %s and passing in %s" %
3264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    (time.tzname, tz_value, time.daylight, tz_name))
3274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        finally:
3284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            time.tzname = original_tzname
3294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            time.daylight = original_daylight
3304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_date_time(self):
3324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test %c directive
3334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for position in range(6):
3344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.helper('c', position)
3354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_date(self):
3374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test %x directive
3384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for position in range(0,3):
3394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.helper('x', position)
3404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_time(self):
3424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test %X directive
3434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for position in range(3,6):
3444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.helper('X', position)
3454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_percent(self):
3474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure % signs are handled properly
3484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strf_output = time.strftime("%m %% %Y", self.time_tuple)
3494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strp_output = _strptime._strptime_time(strf_output, "%m %% %Y")
3504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(strp_output[0] == self.time_tuple[0] and
3514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         strp_output[1] == self.time_tuple[1],
3524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "handling of percent sign failed")
3534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_caseinsensitive(self):
3554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Should handle names case-insensitively.
3564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strf_output = time.strftime("%B", self.time_tuple)
3574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(_strptime._strptime_time(strf_output.upper(), "%B"),
3584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "strptime does not handle ALL-CAPS names properly")
3594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(_strptime._strptime_time(strf_output.lower(), "%B"),
3604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "strptime does not handle lowercase names properly")
3614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(_strptime._strptime_time(strf_output.capitalize(), "%B"),
3624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "strptime does not handle capword names properly")
3634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_defaults(self):
3654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Default return value should be (1900, 1, 1, 0, 0, 0, 0, 1, 0)
3664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        defaults = (1900, 1, 1, 0, 0, 0, 0, 1, -1)
3674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        strp_output = _strptime._strptime_time('1', '%m')
3684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(strp_output == defaults,
3694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "Default values for strptime() are incorrect;"
3704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        " %s != %s" % (strp_output, defaults))
3714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_escaping(self):
3734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure all characters that have regex significance are escaped.
3744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Parentheses are in a purposeful order; will cause an error of
3754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # unbalanced parentheses when the regex is compiled if they are not
3764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # escaped.
3774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test instigated by bug #796149 .
3784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        need_escaping = ".^$*+?{}\[]|)("
3794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(_strptime._strptime_time(need_escaping, need_escaping))
3804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_feb29_on_leap_year_without_year(self):
3824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        time.strptime("Feb 29", "%b %d")
3834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_mar1_comes_after_feb29_even_when_omitting_the_year(self):
3854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertLess(
3864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                time.strptime("Feb 29", "%b %d"),
3874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                time.strptime("Mar 1", "%b %d"))
3884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass Strptime12AMPMTests(unittest.TestCase):
3904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
3914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_twelve_noon_midnight(self):
3934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        eq = self.assertEqual
3944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        eq(time.strptime('12 PM', '%I %p')[3], 12)
3954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        eq(time.strptime('12 AM', '%I %p')[3], 0)
3964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        eq(_strptime._strptime_time('12 PM', '%I %p')[3], 12)
3974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        eq(_strptime._strptime_time('12 AM', '%I %p')[3], 0)
3984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass JulianTests(unittest.TestCase):
4014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """Test a _strptime regression that all julian (1-366) are accepted"""
4024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_all_julian_days(self):
4044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        eq = self.assertEqual
4054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for i in range(1, 367):
4064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # use 2004, since it is a leap year, we have 366 days
4074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            eq(_strptime._strptime_time('%d 2004' % i, '%j %Y')[7], i)
4084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass CalculationTests(unittest.TestCase):
4104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """Test that strptime() fills in missing info correctly"""
4114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def setUp(self):
4134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.time_tuple = time.gmtime()
4144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_julian_calculation(self):
4164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure that when Julian is missing that it is calculated
4174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        format_string = "%Y %m %d %H %M %S %w %Z"
4184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        result = _strptime._strptime_time(time.strftime(format_string, self.time_tuple),
4194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                    format_string)
4204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(result.tm_yday == self.time_tuple.tm_yday,
4214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "Calculation of tm_yday failed; %s != %s" %
4224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         (result.tm_yday, self.time_tuple.tm_yday))
4234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_gregorian_calculation(self):
4254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test that Gregorian date can be calculated from Julian day
4264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        format_string = "%Y %H %M %S %w %j %Z"
4274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        result = _strptime._strptime_time(time.strftime(format_string, self.time_tuple),
4284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                    format_string)
4294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(result.tm_year == self.time_tuple.tm_year and
4304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         result.tm_mon == self.time_tuple.tm_mon and
4314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         result.tm_mday == self.time_tuple.tm_mday,
4324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "Calculation of Gregorian date failed;"
4334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         "%s-%s-%s != %s-%s-%s" %
4344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         (result.tm_year, result.tm_mon, result.tm_mday,
4354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                          self.time_tuple.tm_year, self.time_tuple.tm_mon,
4364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                          self.time_tuple.tm_mday))
4374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_day_of_week_calculation(self):
4394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Test that the day of the week is calculated as needed
4404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        format_string = "%Y %m %d %H %S %j %Z"
4414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        result = _strptime._strptime_time(time.strftime(format_string, self.time_tuple),
4424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                    format_string)
4434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertTrue(result.tm_wday == self.time_tuple.tm_wday,
4444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "Calculation of day of the week failed;"
4454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                         "%s != %s" % (result.tm_wday, self.time_tuple.tm_wday))
4464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_week_of_year_and_day_of_week_calculation(self):
4484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Should be able to infer date if given year, week of year (%U or %W)
4494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # and day of the week
4504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        def test_helper(ymd_tuple, test_reason):
4514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            for directive in ('W', 'U'):
4524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                format_string = "%%Y %%%s %%w" % directive
4534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                dt_date = datetime_date(*ymd_tuple)
4544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                strp_input = dt_date.strftime(format_string)
4554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                strp_output = _strptime._strptime_time(strp_input, format_string)
4564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                self.assertTrue(strp_output[:3] == ymd_tuple,
4574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        "%s(%s) test failed w/ '%s': %s != %s (%s != %s)" %
4584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            (test_reason, directive, strp_input,
4594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                strp_output[:3], ymd_tuple,
4604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                strp_output[7], dt_date.timetuple()[7]))
4614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1901, 1, 3), "week 0")
4624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1901, 1, 8), "common case")
4634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1901, 1, 13), "day on Sunday")
4644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1901, 1, 14), "day on Monday")
4654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1905, 1, 1), "Jan 1 on Sunday")
4664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1906, 1, 1), "Jan 1 on Monday")
4674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1906, 1, 7), "first Sunday in a year starting on Monday")
4684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1905, 12, 31), "Dec 31 on Sunday")
4694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1906, 12, 31), "Dec 31 on Monday")
4704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((2008, 12, 29), "Monday in the last week of the year")
4714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((2008, 12, 22), "Monday in the second-to-last week of the "
4724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                    "year")
4734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1978, 10, 23), "randomly chosen date")
4744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((2004, 12, 18), "randomly chosen date")
4754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1978, 10, 23), "year starting and ending on Monday while "
4764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                        "date not on Sunday or Monday")
4774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1917, 12, 17), "year starting and ending on Monday with "
4784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                        "a Monday not at the beginning or end "
4794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                        "of the year")
4804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((1917, 12, 31), "Dec 31 on Monday with year starting and "
4814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                        "ending on Monday")
4824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((2007, 01, 07), "First Sunday of 2007")
4834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((2007, 01, 14), "Second Sunday of 2007")
4844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((2006, 12, 31), "Last Sunday of 2006")
4854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        test_helper((2006, 12, 24), "Second to last Sunday of 2006")
4864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass CacheTests(unittest.TestCase):
4894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """Test that caching works properly."""
4904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_time_re_recreation(self):
4924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure cache is recreated when current locale does not match what
4934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # cached object was created with.
4944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        _strptime._strptime_time("10", "%d")
4954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        _strptime._strptime_time("2005", "%Y")
4964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        _strptime._TimeRE_cache.locale_time.lang = "Ni"
4974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        original_time_re = _strptime._TimeRE_cache
4984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        _strptime._strptime_time("10", "%d")
4994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertIsNot(original_time_re, _strptime._TimeRE_cache)
5004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(len(_strptime._regex_cache), 1)
5014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_regex_cleanup(self):
5034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure cached regexes are discarded when cache becomes "full".
5044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        try:
5054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            del _strptime._regex_cache['%d']
5064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        except KeyError:
5074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            pass
5084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        bogus_key = 0
5094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        while len(_strptime._regex_cache) <= _strptime._CACHE_MAX_SIZE:
5104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            _strptime._regex_cache[bogus_key] = None
5114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            bogus_key += 1
5124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        _strptime._strptime_time("10", "%d")
5134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(len(_strptime._regex_cache), 1)
5144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_new_localetime(self):
5164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # A new LocaleTime instance should be created when a new TimeRE object
5174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # is created.
5184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        locale_time_id = _strptime._TimeRE_cache.locale_time
5194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        _strptime._TimeRE_cache.locale_time.lang = "Ni"
5204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        _strptime._strptime_time("10", "%d")
5214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time)
5224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_TimeRE_recreation(self):
5244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # The TimeRE instance should be recreated upon changing the locale.
5254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        locale_info = locale.getlocale(locale.LC_TIME)
5264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        try:
5274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            locale.setlocale(locale.LC_TIME, ('en_US', 'UTF8'))
5284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        except locale.Error:
5294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            return
5304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        try:
5314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            _strptime._strptime_time('10', '%d')
5324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # Get id of current cache object.
5334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            first_time_re = _strptime._TimeRE_cache
5344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            try:
5354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                # Change the locale and force a recreation of the cache.
5364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                locale.setlocale(locale.LC_TIME, ('de_DE', 'UTF8'))
5374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                _strptime._strptime_time('10', '%d')
5384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                # Get the new cache object's id.
5394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                second_time_re = _strptime._TimeRE_cache
5404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                # They should not be equal.
5414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                self.assertIsNot(first_time_re, second_time_re)
5424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # Possible test locale is not supported while initial locale is.
5434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # If this is the case just suppress the exception and fall-through
5444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # to the resetting to the original locale.
5454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            except locale.Error:
5464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                pass
5474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Make sure we don't trample on the locale setting once we leave the
5484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # test.
5494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        finally:
5504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            locale.setlocale(locale.LC_TIME, locale_info)
5514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaodef test_main():
5544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    test_support.run_unittest(
5554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        getlang_Tests,
5564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        LocaleTime_Tests,
5574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        TimeRETests,
5584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        StrptimeTests,
5594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Strptime12AMPMTests,
5604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        JulianTests,
5614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        CalculationTests,
5624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        CacheTests
5634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    )
5644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoif __name__ == '__main__':
5674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    test_main()
568