Пример #1
0
    def test_strftime_locale_dependent_turkish(self):
        # store current locale
        old_locale = locale.setlocale(locale.LC_ALL)

        if platform == 'win32':
            locale.setlocale(locale.LC_ALL, 'Turkish')
        else:
            locale.setlocale(locale.LC_ALL, 'tr_TR.UTF-8')

        d = utils.SafeDatetime(2012, 8, 29)

        # simple
        self.assertEqual(utils.strftime(d, '%d %B %Y'), '29 Ağustos 2012')
        self.assertEqual(utils.strftime(d, '%A, %d %B %Y'),
                         'Çarşamba, 29 Ağustos 2012')

        # with text
        self.assertEqual(
            utils.strftime(d, 'Yayınlanma tarihi: %A, %d %B %Y'),
            'Yayınlanma tarihi: Çarşamba, 29 Ağustos 2012')

        # non-ascii format candidate (someone might pass it… for some reason)
        self.assertEqual(
            utils.strftime(d, '%Y yılında %üretim artışı'),
            '2012 yılında %üretim artışı')

        # restore locale back
        locale.setlocale(locale.LC_ALL, old_locale)
Пример #2
0
    def test_strftime_locale_dependent_french(self):
        # store current locale
        old_locale = locale.setlocale(locale.LC_ALL)

        if platform == 'win32':
            locale.setlocale(locale.LC_ALL, str('French'))
        else:
            locale.setlocale(locale.LC_ALL, str('fr_FR.UTF-8'))

        d = utils.SafeDatetime(2012, 8, 29)

        # simple
        self.assertEqual(utils.strftime(d, '%d %B %Y'), '29 août 2012')

        # depending on OS, the first letter is m or M
        self.assertTrue(utils.strftime(d, '%A') in ('mercredi', 'Mercredi'))

        # with text
        self.assertEqual(utils.strftime(d, 'Écrit le %d %B %Y'),
                         'Écrit le 29 août 2012')

        # non-ascii format candidate (someone might pass it... for some reason)
        self.assertEqual(utils.strftime(d, '%écrits en %Y'), '%écrits en 2012')

        # restore locale back
        locale.setlocale(locale.LC_ALL, old_locale)
Пример #3
0
    def setUp(self):
        # prepare a temp content and output folder
        self.temp_content = mkdtemp(prefix='pelicantests.')
        self.temp_output = mkdtemp(prefix='pelicantests.')

        # prepare a template file
        template_dir = os.path.join(self.temp_content, 'template')
        template_path = os.path.join(template_dir, 'source.html')
        os.makedirs(template_dir)
        with open(template_path, 'w') as template_file:
            template_file.write('date = {{ date|strftime("%A, %d %B %Y") }}')
        self.date = utils.SafeDatetime(2012, 8, 29)
Пример #4
0
    def test_strftime(self):
        d = utils.SafeDatetime(2012, 8, 29)

        # simple formatting
        self.assertEqual(utils.strftime(d, '%d/%m/%y'), '29/08/12')
        self.assertEqual(utils.strftime(d, '%d/%m/%Y'), '29/08/2012')

        # RFC 3339
        self.assertEqual(
            utils.strftime(d, '%Y-%m-%dT%H:%M:%SZ'),
            '2012-08-29T00:00:00Z')

        # % escaped
        self.assertEqual(utils.strftime(d, '%d%%%m%%%y'), '29%08%12')
        self.assertEqual(utils.strftime(d, '%d %% %m %% %y'), '29 % 08 % 12')
        # not valid % formatter
        self.assertEqual(utils.strftime(d, '10% reduction in %Y'),
                         '10% reduction in 2012')
        self.assertEqual(utils.strftime(d, '%10 reduction in %Y'),
                         '%10 reduction in 2012')

        # with text
        self.assertEqual(utils.strftime(d, 'Published in %d-%m-%Y'),
                         'Published in 29-08-2012')

        # with non-ascii text
        self.assertEqual(
            utils.strftime(d, '%d/%m/%Y Øl trinken beim Besäufnis'),
            '29/08/2012 Øl trinken beim Besäufnis')

        # alternative formatting options
        self.assertEqual(utils.strftime(d, '%-d/%-m/%y'), '29/8/12')
        self.assertEqual(utils.strftime(d, '%-H:%-M:%-S'), '0:0:0')

        d = utils.SafeDatetime(2012, 8, 9)
        self.assertEqual(utils.strftime(d, '%-d/%-m/%y'), '9/8/12')

        d = utils.SafeDatetime(2021, 1, 8)
        self.assertEqual(utils.strftime(d, '%G - %-V - %u'), '2021 - 1 - 5')
Пример #5
0
 def test_french_strftime(self):
     # This test tries to reproduce an issue that occured with python3.3 under macos10 only
     locale.setlocale(locale.LC_ALL, str('fr_FR.UTF-8'))
     date = utils.SafeDatetime(2014,8,14)
     # we compare the lower() dates since macos10 returns "Jeudi" for %A whereas linux reports "jeudi"
     self.assertEqual( u'jeudi, 14 août 2014', utils.strftime(date, date_format="%A, %d %B %Y").lower() )
     df = utils.DateFormatter()
     self.assertEqual( u'jeudi, 14 août 2014', df(date, date_format="%A, %d %B %Y").lower() )
     # Let us now set the global locale to C:
     locale.setlocale(locale.LC_ALL, str('C'))
     # DateFormatter should still work as expected since it is the whole point of DateFormatter
     # (This is where pre-2014/4/15 code fails on macos10)
     df_date = df(date, date_format="%A, %d %B %Y").lower()
     self.assertEqual( u'jeudi, 14 août 2014', df_date )
Пример #6
0
    def test_get_date(self):
        # valid ones
        date = utils.SafeDatetime(year=2012, month=11, day=22)
        date_hour = utils.SafeDatetime(year=2012,
                                       month=11,
                                       day=22,
                                       hour=22,
                                       minute=11)
        date_hour_z = utils.SafeDatetime(year=2012,
                                         month=11,
                                         day=22,
                                         hour=22,
                                         minute=11,
                                         tzinfo=pytz.timezone('UTC'))
        date_hour_est = utils.SafeDatetime(year=2012,
                                           month=11,
                                           day=22,
                                           hour=22,
                                           minute=11,
                                           tzinfo=pytz.timezone('EST'))
        date_hour_sec = utils.SafeDatetime(year=2012,
                                           month=11,
                                           day=22,
                                           hour=22,
                                           minute=11,
                                           second=10)
        date_hour_sec_z = utils.SafeDatetime(year=2012,
                                             month=11,
                                             day=22,
                                             hour=22,
                                             minute=11,
                                             second=10,
                                             tzinfo=pytz.timezone('UTC'))
        date_hour_sec_est = utils.SafeDatetime(year=2012,
                                               month=11,
                                               day=22,
                                               hour=22,
                                               minute=11,
                                               second=10,
                                               tzinfo=pytz.timezone('EST'))
        date_hour_sec_frac_z = utils.SafeDatetime(year=2012,
                                                  month=11,
                                                  day=22,
                                                  hour=22,
                                                  minute=11,
                                                  second=10,
                                                  microsecond=123000,
                                                  tzinfo=pytz.timezone('UTC'))
        dates = {
            '2012-11-22': date,
            '2012/11/22': date,
            '2012-11-22 22:11': date_hour,
            '2012/11/22 22:11': date_hour,
            '22-11-2012': date,
            '22/11/2012': date,
            '22.11.2012': date,
            '22.11.2012 22:11': date_hour,
            '2012-11-22T22:11Z': date_hour_z,
            '2012-11-22T22:11-0500': date_hour_est,
            '2012-11-22 22:11:10': date_hour_sec,
            '2012-11-22T22:11:10Z': date_hour_sec_z,
            '2012-11-22T22:11:10-0500': date_hour_sec_est,
            '2012-11-22T22:11:10.123Z': date_hour_sec_frac_z,
        }

        # examples from http://www.w3.org/TR/NOTE-datetime
        iso_8601_date = utils.SafeDatetime(year=1997, month=7, day=16)
        iso_8601_date_hour_tz = utils.SafeDatetime(year=1997,
                                                   month=7,
                                                   day=16,
                                                   hour=19,
                                                   minute=20,
                                                   tzinfo=pytz.timezone('CET'))
        iso_8601_date_hour_sec_tz = utils.SafeDatetime(
            year=1997,
            month=7,
            day=16,
            hour=19,
            minute=20,
            second=30,
            tzinfo=pytz.timezone('CET'))
        iso_8601_date_hour_sec_ms_tz = utils.SafeDatetime(
            year=1997,
            month=7,
            day=16,
            hour=19,
            minute=20,
            second=30,
            microsecond=450000,
            tzinfo=pytz.timezone('CET'))
        iso_8601 = {
            '1997-07-16': iso_8601_date,
            '1997-07-16T19:20+01:00': iso_8601_date_hour_tz,
            '1997-07-16T19:20:30+01:00': iso_8601_date_hour_sec_tz,
            '1997-07-16T19:20:30.45+01:00': iso_8601_date_hour_sec_ms_tz,
        }

        # invalid ones
        invalid_dates = ['2010-110-12', 'yay']

        for value, expected in dates.items():
            self.assertEqual(utils.get_date(value), expected, value)

        for value, expected in iso_8601.items():
            self.assertEqual(utils.get_date(value), expected, value)

        for item in invalid_dates:
            self.assertRaises(ValueError, utils.get_date, item)
    def test_get_date_returns_correct_value(self):
        date = utils.SafeDatetime(year=2020, month=0o4, day=30)
        date_hour = utils.SafeDatetime(year=2020,
                                       month=0o4,
                                       day=30,
                                       hour=20,
                                       minute=10)
        date_hour_utc = utils.SafeDatetime(year=2020,
                                           month=0o4,
                                           day=30,
                                           hour=20,
                                           minute=10,
                                           tzinfo=pytz.timezone('UTC'))
        date_hour_est = utils.SafeDatetime(year=2020,
                                           month=0o4,
                                           day=30,
                                           hour=20,
                                           minute=10,
                                           tzinfo=pytz.timezone('EST'))
        date_hour_seconds = utils.SafeDatetime(
            year=2020,
            month=0o4,
            day=30,
            hour=20,
            minute=10,
            second=20,
        )
        date_hour_seconds_utc = utils.SafeDatetime(year=2020,
                                                   month=0o4,
                                                   day=30,
                                                   hour=20,
                                                   minute=10,
                                                   second=20,
                                                   tzinfo=pytz.timezone('UTC'))
        date_hour_seconds_est = utils.SafeDatetime(year=2020,
                                                   month=0o4,
                                                   day=30,
                                                   hour=20,
                                                   minute=10,
                                                   second=20,
                                                   tzinfo=pytz.timezone('EST'))
        date_hour_seconds_usecs_utc = utils.SafeDatetime(
            year=2020,
            month=0o4,
            day=30,
            hour=20,
            minute=10,
            second=20,
            microsecond=123000,
            tzinfo=pytz.timezone('UTC'))

        dates = {
            '2020/04/30': date,
            '2020-04-30': date,
            '30/04/2020': date,
            '30-04-2020': date,
            '30.04.2020': date,
            '2020/04/30 20:10': date_hour,
            '2020-04-30 20:10': date_hour,
            '30.04.2020 20:10': date_hour,
            '2020-04-30T20:10Z': date_hour_utc,
            '2020-04-30T20:10-0500': date_hour_est,
            '2020/04/30 20:10:20': date_hour_seconds,
            '2020-04-30T20:10:20Z': date_hour_seconds_utc,
            '2020/04/30T20:10:20-0500': date_hour_seconds_est,
            '2020-04-30T20:10:20.123Z': date_hour_seconds_usecs_utc,
        }

        # ISO 8601 datetime format
        iso_8601_date = utils.SafeDatetime(
            year=1997,
            month=7,
            day=15,
        )
        iso_8601_date_hour_timezone = utils.SafeDatetime(
            year=1997,
            month=7,
            day=15,
            hour=19,
            minute=20,
            tzinfo=pytz.timezone('CET'))
        iso_8601_date_hour_secs_timezone = utils.SafeDatetime(
            year=1997,
            month=7,
            day=15,
            hour=19,
            minute=20,
            second=30,
            tzinfo=pytz.timezone('CET'))
        iso_8601_date_hour_secs_ms = utils.SafeDatetime(
            year=1997,
            month=7,
            day=15,
            hour=19,
            minute=20,
            second=30,
            microsecond=120000,
            tzinfo=pytz.timezone('CET'))

        iso_8601_format = {
            '1997-07-15': iso_8601_date,
            '1997-07-15T19:20+01:00': iso_8601_date_hour_timezone,
            '1997-07-15T19:20:30+01:00': iso_8601_date_hour_secs_timezone,
            '1997-07-15T19:20:30.12+01:00': iso_8601_date_hour_secs_ms,
        }

        # Invalid dates
        invalid_dates = ['2040-123-3,' 'wrongdate', '2.12.ac', '001/1001']

        for value, expected in dates.items():
            self.assertEqual(utils.get_date(value), expected, value)
            self.assertTrue(utils.get_date(value) is not None)

        for value, expected in iso_8601_format.items():
            self.assertEqual(utils.get_date(value), expected, value)

        for date in invalid_dates:
            self.assertRaises(ValueError, utils.get_date, date)