cmlenz@1: # -*- coding: utf-8 -*- cmlenz@1: # cmlenz@12: # Copyright (C) 2007 Edgewall Software cmlenz@1: # All rights reserved. cmlenz@1: # cmlenz@1: # This software is licensed as described in the file COPYING, which cmlenz@1: # you should have received as part of this distribution. The terms cmlenz@1: # are also available at http://babel.edgewall.org/wiki/License. cmlenz@1: # cmlenz@1: # This software consists of voluntary contributions made by many cmlenz@1: # individuals. For the exact contribution history, see the revision cmlenz@1: # history and logs, available at http://babel.edgewall.org/log/. cmlenz@1: fschwarz@519: from datetime import date, datetime, time, timedelta cmlenz@1: import doctest fschwarz@512: import new cmlenz@1: import unittest cmlenz@1: cmlenz@29: from pytz import timezone cmlenz@29: cmlenz@1: from babel import dates fschwarz@512: from babel.util import FixedOffsetTimezone cmlenz@1: cmlenz@15: cmlenz@15: class DateTimeFormatTestCase(unittest.TestCase): cmlenz@15: cmlenz@396: def test_quarter_format(self): cmlenz@396: d = date(2006, 6, 8) cmlenz@396: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@396: self.assertEqual('2', fmt['Q']) cmlenz@396: self.assertEqual('2nd quarter', fmt['QQQQ']) cmlenz@396: d = date(2006, 12, 31) cmlenz@396: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@396: self.assertEqual('Q4', fmt['QQQ']) cmlenz@396: cmlenz@344: def test_month_context(self): cmlenz@344: d = date(2006, 1, 8) cmlenz@344: fmt = dates.DateTimeFormat(d, locale='cs_CZ') cmlenz@344: self.assertEqual('1', fmt['MMM']) cmlenz@344: fmt = dates.DateTimeFormat(d, locale='cs_CZ') cmlenz@344: self.assertEqual('1.', fmt['LLL']) cmlenz@344: cmlenz@375: def test_abbreviated_month_alias(self): cmlenz@375: d = date(2006, 3, 8) cmlenz@375: fmt = dates.DateTimeFormat(d, locale='de_DE') cmlenz@375: self.assertEqual(u'Mär', fmt['LLL']) cmlenz@375: cmlenz@240: def test_week_of_year_first(self): cmlenz@239: d = date(2006, 1, 8) cmlenz@239: fmt = dates.DateTimeFormat(d, locale='de_DE') cmlenz@239: self.assertEqual('1', fmt['w']) cmlenz@215: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@239: self.assertEqual('02', fmt['ww']) cmlenz@215: cmlenz@242: def test_week_of_year_first_with_year(self): cmlenz@242: d = date(2006, 1, 1) cmlenz@242: fmt = dates.DateTimeFormat(d, locale='de_DE') cmlenz@242: self.assertEqual('52', fmt['w']) cmlenz@242: self.assertEqual('2005', fmt['YYYY']) cmlenz@242: cmlenz@240: def test_week_of_year_last(self): cmlenz@240: d = date(2005, 12, 26) cmlenz@240: fmt = dates.DateTimeFormat(d, locale='de_DE') cmlenz@240: self.assertEqual('52', fmt['w']) cmlenz@215: fmt = dates.DateTimeFormat(d, locale='en_US') jruigrok@438: self.assertEqual('52', fmt['ww']) cmlenz@240: cmlenz@240: def test_week_of_month_first(self): cmlenz@240: d = date(2006, 1, 8) cmlenz@240: fmt = dates.DateTimeFormat(d, locale='de_DE') cmlenz@215: self.assertEqual('1', fmt['W']) cmlenz@240: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@240: self.assertEqual('2', fmt['W']) cmlenz@240: cmlenz@240: def test_week_of_month_last(self): cmlenz@240: d = date(2006, 1, 29) cmlenz@240: fmt = dates.DateTimeFormat(d, locale='de_DE') cmlenz@240: self.assertEqual('4', fmt['W']) cmlenz@240: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@240: self.assertEqual('5', fmt['W']) cmlenz@215: cmlenz@221: def test_day_of_year(self): cmlenz@221: d = date(2007, 4, 1) cmlenz@221: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@221: self.assertEqual('91', fmt['D']) cmlenz@221: cmlenz@221: def test_day_of_year_first(self): cmlenz@221: d = date(2007, 1, 1) cmlenz@221: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@221: self.assertEqual('001', fmt['DDD']) cmlenz@221: cmlenz@221: def test_day_of_year_last(self): cmlenz@221: d = date(2007, 12, 31) cmlenz@221: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@221: self.assertEqual('365', fmt['DDD']) cmlenz@221: cmlenz@241: def test_day_of_week_in_month(self): cmlenz@241: d = date(2007, 4, 15) cmlenz@241: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@241: self.assertEqual('3', fmt['F']) cmlenz@241: cmlenz@241: def test_day_of_week_in_month_first(self): cmlenz@241: d = date(2007, 4, 1) cmlenz@241: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@241: self.assertEqual('1', fmt['F']) cmlenz@241: cmlenz@241: def test_day_of_week_in_month_last(self): cmlenz@241: d = date(2007, 4, 29) cmlenz@241: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@241: self.assertEqual('5', fmt['F']) cmlenz@241: cmlenz@15: def test_local_day_of_week(self): cmlenz@217: d = date(2007, 4, 1) # a sunday cmlenz@15: fmt = dates.DateTimeFormat(d, locale='de_DE') cmlenz@15: self.assertEqual('7', fmt['e']) # monday is first day of week cmlenz@15: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@15: self.assertEqual('01', fmt['ee']) # sunday is first day of week cmlenz@15: fmt = dates.DateTimeFormat(d, locale='dv_MV') cmlenz@15: self.assertEqual('03', fmt['ee']) # friday is first day of week cmlenz@15: cmlenz@217: d = date(2007, 4, 2) # a monday cmlenz@15: fmt = dates.DateTimeFormat(d, locale='de_DE') cmlenz@15: self.assertEqual('1', fmt['e']) # monday is first day of week cmlenz@15: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@15: self.assertEqual('02', fmt['ee']) # sunday is first day of week cmlenz@15: fmt = dates.DateTimeFormat(d, locale='dv_MV') cmlenz@15: self.assertEqual('04', fmt['ee']) # friday is first day of week cmlenz@15: cmlenz@15: def test_local_day_of_week_standalone(self): cmlenz@217: d = date(2007, 4, 1) # a sunday cmlenz@15: fmt = dates.DateTimeFormat(d, locale='de_DE') cmlenz@15: self.assertEqual('7', fmt['c']) # monday is first day of week cmlenz@15: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@15: self.assertEqual('1', fmt['c']) # sunday is first day of week cmlenz@15: fmt = dates.DateTimeFormat(d, locale='dv_MV') cmlenz@15: self.assertEqual('3', fmt['c']) # friday is first day of week cmlenz@15: cmlenz@217: d = date(2007, 4, 2) # a monday cmlenz@15: fmt = dates.DateTimeFormat(d, locale='de_DE') cmlenz@15: self.assertEqual('1', fmt['c']) # monday is first day of week cmlenz@15: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@15: self.assertEqual('2', fmt['c']) # sunday is first day of week cmlenz@15: fmt = dates.DateTimeFormat(d, locale='dv_MV') cmlenz@15: self.assertEqual('4', fmt['c']) # friday is first day of week cmlenz@15: cmlenz@216: def test_fractional_seconds(self): cmlenz@217: t = time(15, 30, 12, 34567) cmlenz@217: fmt = dates.DateTimeFormat(t, locale='en_US') cmlenz@216: self.assertEqual('3457', fmt['SSSS']) cmlenz@216: cmlenz@216: def test_fractional_seconds_zero(self): cmlenz@217: t = time(15, 30, 0) cmlenz@217: fmt = dates.DateTimeFormat(t, locale='en_US') cmlenz@217: self.assertEqual('0000', fmt['SSSS']) cmlenz@217: cmlenz@217: def test_milliseconds_in_day(self): cmlenz@217: t = time(15, 30, 12, 345000) cmlenz@217: fmt = dates.DateTimeFormat(t, locale='en_US') cmlenz@217: self.assertEqual('55812345', fmt['AAAA']) cmlenz@217: cmlenz@217: def test_milliseconds_in_day_zero(self): cmlenz@217: d = time(0, 0, 0) cmlenz@216: fmt = dates.DateTimeFormat(d, locale='en_US') cmlenz@217: self.assertEqual('0000', fmt['AAAA']) cmlenz@216: cmlenz@29: def test_timezone_rfc822(self): cmlenz@29: tz = timezone('Europe/Berlin') cmlenz@29: t = time(15, 30, tzinfo=tz) cmlenz@29: fmt = dates.DateTimeFormat(t, locale='de_DE') cmlenz@29: self.assertEqual('+0100', fmt['Z']) cmlenz@29: cmlenz@29: def test_timezone_gmt(self): cmlenz@29: tz = timezone('Europe/Berlin') cmlenz@29: t = time(15, 30, tzinfo=tz) cmlenz@29: fmt = dates.DateTimeFormat(t, locale='de_DE') cmlenz@233: self.assertEqual('GMT+01:00', fmt['ZZZZ']) cmlenz@233: cmlenz@233: def test_timezone_no_uncommon(self): cmlenz@233: tz = timezone('Europe/Paris') cmlenz@233: dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz) cmlenz@233: fmt = dates.DateTimeFormat(dt, locale='fr_CA') cmlenz@233: self.assertEqual('France', fmt['v']) cmlenz@233: cmlenz@233: def test_timezone_with_uncommon(self): cmlenz@233: tz = timezone('Europe/Paris') cmlenz@233: dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz) cmlenz@233: fmt = dates.DateTimeFormat(dt, locale='fr_CA') cmlenz@233: self.assertEqual('HEC', fmt['V']) cmlenz@233: cmlenz@233: def test_timezone_location_format(self): cmlenz@233: tz = timezone('Europe/Paris') cmlenz@233: dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz) cmlenz@233: fmt = dates.DateTimeFormat(dt, locale='fr_FR') cmlenz@233: self.assertEqual('France', fmt['VVVV']) cmlenz@29: cmlenz@129: def test_timezone_walltime_short(self): cmlenz@129: tz = timezone('Europe/Paris') cmlenz@129: t = time(15, 30, tzinfo=tz) cmlenz@233: fmt = dates.DateTimeFormat(t, locale='fr_FR') cmlenz@233: self.assertEqual('HEC', fmt['v']) cmlenz@129: cmlenz@129: def test_timezone_walltime_long(self): cmlenz@129: tz = timezone('Europe/Paris') cmlenz@129: t = time(15, 30, tzinfo=tz) cmlenz@233: fmt = dates.DateTimeFormat(t, locale='fr_FR') jruigrok@430: self.assertEqual(u'Heure de l\u2019Europe centrale', fmt['vvvv']) cmlenz@29: jonas@273: def test_hour_formatting(self): jonas@273: l = 'en_US' jonas@273: t = time(0, 0, 0) jonas@273: self.assertEqual(dates.format_time(t, 'h a', locale=l), '12 AM') jonas@273: self.assertEqual(dates.format_time(t, 'H', locale=l), '0') jonas@273: self.assertEqual(dates.format_time(t, 'k', locale=l), '24') jonas@273: self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 AM') jonas@273: t = time(12, 0, 0) jonas@273: self.assertEqual(dates.format_time(t, 'h a', locale=l), '12 PM') jonas@273: self.assertEqual(dates.format_time(t, 'H', locale=l), '12') jonas@274: self.assertEqual(dates.format_time(t, 'k', locale=l), '12') jonas@273: self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 PM') jonas@273: cmlenz@15: cmlenz@19: class FormatDateTestCase(unittest.TestCase): cmlenz@19: cmlenz@19: def test_with_time_fields_in_pattern(self): cmlenz@19: self.assertRaises(AttributeError, dates.format_date, date(2007, 04, 01), cmlenz@19: "yyyy-MM-dd HH:mm", locale='en_US') cmlenz@19: cmlenz@19: def test_with_time_fields_in_pattern_and_datetime_param(self): cmlenz@19: self.assertRaises(AttributeError, dates.format_date, cmlenz@19: datetime(2007, 04, 01, 15, 30), cmlenz@19: "yyyy-MM-dd HH:mm", locale='en_US') cmlenz@19: cmlenz@19: cmlenz@19: class FormatTimeTestCase(unittest.TestCase): cmlenz@19: cmlenz@348: def test_with_naive_datetime_and_tzinfo(self): cmlenz@348: string = dates.format_time(datetime(2007, 4, 1, 15, 30), cmlenz@348: 'long', tzinfo=timezone('US/Eastern'), cmlenz@348: locale='en') cmlenz@348: self.assertEqual('11:30:00 AM EDT', string) cmlenz@348: cmlenz@19: def test_with_date_fields_in_pattern(self): cmlenz@19: self.assertRaises(AttributeError, dates.format_time, date(2007, 04, 01), cmlenz@19: "yyyy-MM-dd HH:mm", locale='en_US') cmlenz@19: cmlenz@19: def test_with_date_fields_in_pattern_and_datetime_param(self): cmlenz@19: self.assertRaises(AttributeError, dates.format_time, cmlenz@19: datetime(2007, 04, 01, 15, 30), cmlenz@19: "yyyy-MM-dd HH:mm", locale='en_US') cmlenz@19: cmlenz@19: cmlenz@395: class FormatTimedeltaTestCase(unittest.TestCase): cmlenz@395: cmlenz@395: def test_zero_seconds(self): cmlenz@395: string = dates.format_timedelta(timedelta(seconds=0), locale='en') fschwarz@519: self.assertEqual('0 secs', string) cmlenz@395: string = dates.format_timedelta(timedelta(seconds=0), cmlenz@395: granularity='hour', locale='en') fschwarz@519: self.assertEqual('0 hrs', string) cmlenz@395: cmlenz@395: def test_small_value_with_granularity(self): cmlenz@395: string = dates.format_timedelta(timedelta(seconds=42), cmlenz@395: granularity='hour', locale='en') fschwarz@519: self.assertEqual('1 hr', string) cmlenz@395: cmlenz@395: fschwarz@512: class TimeZoneAdjustTestCase(unittest.TestCase): fschwarz@512: def _utc(self): fschwarz@512: UTC = FixedOffsetTimezone(0, 'UTC') fschwarz@512: def fake_localize(self, dt, is_dst=False): fschwarz@512: raise NotImplementedError() fschwarz@512: UTC.localize = new.instancemethod(fake_localize, UTC, UTC.__class__) fschwarz@512: # This is important to trigger the actual bug (#257) fschwarz@512: self.assertEqual(False, hasattr(UTC, 'normalize')) fschwarz@512: return UTC fschwarz@512: fschwarz@512: def test_can_format_time_with_non_pytz_timezone(self): fschwarz@512: # regression test for #257 fschwarz@512: utc = self._utc() fschwarz@512: t = datetime(2007, 4, 1, 15, 30, tzinfo=utc) fschwarz@512: formatted_time = dates.format_time(t, 'long', tzinfo=utc, locale='en') fschwarz@512: self.assertEqual('3:30:00 PM +0000', formatted_time) fschwarz@512: fschwarz@512: cmlenz@1: def suite(): cmlenz@1: suite = unittest.TestSuite() cmlenz@1: suite.addTest(doctest.DocTestSuite(dates)) cmlenz@15: suite.addTest(unittest.makeSuite(DateTimeFormatTestCase)) cmlenz@19: suite.addTest(unittest.makeSuite(FormatDateTestCase)) cmlenz@19: suite.addTest(unittest.makeSuite(FormatTimeTestCase)) fschwarz@519: suite.addTest(unittest.makeSuite(FormatTimedeltaTestCase)) fschwarz@512: suite.addTest(unittest.makeSuite(TimeZoneAdjustTestCase)) cmlenz@1: return suite cmlenz@1: cmlenz@395: cmlenz@1: if __name__ == '__main__': cmlenz@1: unittest.main(defaultTest='suite')