def test_get_timezone_location(): set_locale(Locale('de', 'DE')) tz = pytz.timezone('America/St_Johns') assert i18n.get_timezone_location(tz) == u'Kanada (St. John’s)' set_locale(Locale('de', 'DE')) tz = pytz.timezone('America/Mexico_City') assert i18n.get_timezone_location(tz) == 'Mexiko (Mexiko-Stadt)'
def test_get_month_names(): set_locale(Locale('en', 'US')) assert i18n.get_month_names('wide')[1] == 'January' set_locale(Locale('es')) assert i18n.get_month_names('abbreviated')[1] == 'ene.' set_locale(Locale('de', 'DE')) assert i18n.get_month_names('narrow', context='stand-alone')[1] == 'J'
def test_get_day_names(): set_locale(Locale('en', 'US')) assert i18n.get_day_names('wide')[1] == 'Tuesday' set_locale(Locale('es')) assert i18n.get_day_names('abbreviated')[1] == 'mar.' set_locale(Locale('de', 'DE')) assert i18n.get_day_names('narrow', context='stand-alone')[1] == 'D'
def test_parse_decimal(): set_locale(Locale('en', 'US')) assert i18n.parse_decimal('1,099.98') == Decimal('1099.98') set_locale(Locale('de', 'DE')) assert i18n.parse_decimal('1.099,98') == Decimal('1099.98') set_locale(Locale('de', 'DE')) with pytest.raises(NumberFormatError): i18n.parse_decimal('2,109,998')
def test_get_official_languages(): set_locale(Locale('en', 'US')) assert i18n.get_official_languages() == () assert i18n.get_official_languages(de_facto=True) == ('en',) assert i18n.get_official_languages(regional=True) == ('es', 'haw') set_locale(Locale('fr', 'FR')) assert i18n.get_official_languages() == ('fr',) assert i18n.get_official_languages(de_facto=True) == ('fr',) assert i18n.get_official_languages(regional=True) == ('fr',)
def test_parse_number(): set_locale(Locale('en', 'US')) assert i18n.parse_number('1,099') == 1099 set_locale(Locale('de', 'DE')) assert i18n.parse_number('1.099') == 1099 set_locale(Locale('de', 'DE')) with pytest.raises(NumberFormatError): i18n.parse_number('1.099,98')
def test_format_time(): t = datetime.time(15, 30) set_locale(Locale('en', 'US')) assert i18n.format_time(t) == '3:30:00 PM' set_locale(Locale('de', 'DE')) assert i18n.format_time(t, format='short') == '15:30' set_locale(Locale('en')) assert i18n.format_time(t, "hh 'o''clock' a") == "03 o'clock PM"
def test_format_decimal(): set_locale(Locale('en', 'US')) assert i18n.format_decimal(1.2345) == '1.234' assert i18n.format_decimal(1.2346) == '1.235' assert i18n.format_decimal(1.2346, decimal_quantization=False) == '1.2346' assert i18n.format_decimal(-1.2346) == '-1.235' set_locale(Locale('sv', 'SE')) assert i18n.format_decimal(1.2345) == '1,234' set_locale(Locale('de')) assert i18n.format_decimal(12345) == '12.345'
def test_format_date(): d = datetime.date(2007, 4, 1) set_locale(Locale('en', 'US')) assert i18n.format_date(d) == 'Apr 1, 2007' set_locale(Locale('de', 'DE')) assert i18n.format_date(d, format='full') == 'Sonntag, 1. April 2007' set_locale(Locale('en')) assert i18n.format_date(d, "EEE, MMM d, ''yy") == "Sun, Apr 1, '07"
def test_format_list(): set_locale(Locale('en')) assert i18n.format_list(['apples', 'oranges', 'pears']) == 'apples, oranges, and pears' set_locale(Locale('zh')) assert i18n.format_list(['apples', 'oranges', 'pears']) == u'apples\u3001oranges\u548cpears' set_locale(Locale('fi')) assert i18n.format_list(['omena', 'peruna', 'aplari'], style='or') == 'omena, peruna tai aplari'
def test_format_skeleton(): t = datetime.datetime(2007, 4, 1, 15, 30) set_locale(Locale('fr')) assert i18n.format_skeleton('MMMEd', t) == 'dim. 1 avr.' set_locale(Locale('en')) assert i18n.format_skeleton('MMMEd', t) == 'Sun, Apr 1' set_locale(Locale('fi')) assert i18n.format_skeleton('yMMd', t) == '1.4.2007' with pytest.raises(KeyError, match='yMMd'): i18n.format_skeleton('yMMd', t, fuzzy=False)
def test_get_territory_currencies(): set_locale(Locale(territory='AT')) assert i18n.get_territory_currencies(datetime.date(1995, 1, 1), datetime.date(2011, 1, 1)) == ['ATS', 'EUR'] assert i18n.get_territory_currencies(datetime.date(1995, 1, 1)) == ['ATS'] assert i18n.get_territory_currencies(datetime.date(2011, 1, 1)) == ['EUR'] set_locale(Locale('en', 'US')) assert i18n.get_territory_currencies() == ['USD'] assert i18n.get_territory_currencies(tender=False, non_tender=True, start_date=datetime.date( 2014, 1, 1)) == ['USN', 'USS']
def test_format_percent(): set_locale(Locale('en', 'US')) assert i18n.format_percent(0.34) == '34%' assert i18n.format_percent(25.1234) == '2,512%' set_locale(Locale('sv', 'SE')) assert i18n.format_percent(25.1234) == u'2\xa0512\xa0%' set_locale(Locale('en', 'US')) assert i18n.format_percent(25.1234, u'#,##0\u2030') == u'25,123\u2030' set_locale(Locale('en', 'US')) assert i18n.format_percent(23.9876) == '2,399%' assert i18n.format_percent(23.9876, decimal_quantization=False) == '2,398.76%'
def test_format_interval(): set_locale(Locale('fi')) assert i18n.format_interval(datetime.datetime(2016, 1, 15), datetime.datetime(2016, 1, 17), 'yMd') == u'15.\u201317.1.2016' set_locale(Locale('en', 'GB')) assert i18n.format_interval(datetime.time(12, 12), datetime.time(16, 16), 'Hm') == u'12:12\u201316:16' set_locale(Locale('en', 'US')) assert i18n.format_interval(datetime.time(5, 12), datetime.time(16, 16), 'hm') == u'5:12 AM \u2013 4:16 PM' set_locale(Locale('it')) assert i18n.format_interval(datetime.time(16, 18), datetime.time(16, 24), 'Hm') == u'16:18\u201316:24' assert i18n.format_interval(datetime.time(16, 18), datetime.time(16, 18), 'Hm') == '16:18' set_locale(Locale('ja')) assert i18n.format_interval(datetime.date(2015, 1, 1), datetime.date(2017, 1, 1), 'wzq') == u'2015/01/01\uff5e2017/01/01' set_locale(Locale('ja')) assert i18n.format_interval(datetime.time(16, 18), datetime.time(16, 24), 'xxx') == u'16:18:00\uff5e16:24:00' set_locale(Locale('de')) assert i18n.format_interval(datetime.date(2016, 1, 15), datetime.date(2016, 1, 17), 'xxx') == u'15.01.2016 \u2013 17.01.2016'
def test_format_currency(): set_locale(Locale('en', 'US')) assert i18n.format_currency(1099.98, 'USD') == '$1,099.98' set_locale(Locale('es', 'CO')) assert i18n.format_currency(1099.98, 'USD') == u'US$\xa01.099,98' set_locale(Locale('de', 'DE')) assert i18n.format_currency(1099.98, 'EUR') == u'1.099,98\xa0\u20ac' set_locale(Locale('en', 'US')) assert i18n.format_currency(1099.98, 'EUR', u'\xa4\xa4 #,##0.00') == 'EUR 1,099.98' set_locale(Locale('en', 'US')) assert i18n.format_currency(1099.98, 'JPY', currency_digits=False) == u'\xa51,099.98' set_locale(Locale('es', 'ES')) assert i18n.format_currency(1099.98, 'COP', '#,##0.00', currency_digits=False) == '1.099,98' set_locale(Locale('en', 'US')) assert i18n.format_currency(1099.9876, 'USD') == '$1,099.99' assert i18n.format_currency(1099.9876, 'USD', decimal_quantization=False) == '$1,099.9876'
def test_get_timezone_name(): utc_date = datetime.datetime(2007, 4, 1, 15, 30) tz = pytz.timezone('Pacific/Pitcairn') d = Locale('en', timezone=tz).to_timezone(utc_date) assert i18n.get_timezone_name(d) == u'heure des îles Pitcairn' assert i18n.get_timezone_name(d, width='long') == u'heure des îles Pitcairn' assert i18n.get_timezone_name(d, width='short') == '-0800'
def test_get_timezone_gmt(): dt = datetime.datetime(2007, 4, 1, 15, 30) set_locale(Locale('en')) assert i18n.get_timezone_gmt(dt) == 'GMT+00:00' assert i18n.get_timezone_gmt(dt, return_z=True) == 'Z' assert i18n.get_timezone_gmt(dt, width='iso8601_short') == '+00' dt = Locale(timezone='America/Los_Angeles').to_timezone( datetime.datetime(2007, 4, 1, 15, 30)) assert i18n.get_timezone_gmt(dt) == 'GMT-07:00' set_locale(Locale('en')) assert i18n.get_timezone_gmt(dt, 'short') == '-0700' assert i18n.get_timezone_gmt(dt, width='iso8601_short') == '-07' set_locale(Locale('fr', 'FR')) assert i18n.get_timezone_gmt(dt, 'long') == 'UTC-07:00'
def test_format_timedelta(): set_locale(Locale('en', 'US')) assert i18n.format_timedelta(datetime.timedelta(weeks=12)) == '3 months' set_locale(Locale('es')) assert i18n.format_timedelta(1) == u'1 segundo' set_locale(Locale('en', 'US')) assert i18n.format_timedelta(datetime.timedelta(hours=3), granularity='day') == '1 day' set_locale(Locale('en', 'US')) assert i18n.format_timedelta(datetime.timedelta(hours=23), threshold=0.9) == '1 day' assert i18n.format_timedelta(datetime.timedelta(hours=23), threshold=1.1) == '23 hours' set_locale(Locale('fr', 'FR')) assert i18n.format_timedelta(datetime.timedelta(hours=1), add_direction=True) == 'dans 1 heure' assert i18n.format_timedelta(datetime.timedelta(hours=-1), add_direction=True) == 'il y a 1 heure' set_locale(Locale('en')) assert i18n.format_timedelta(datetime.timedelta(hours=3), format='short') == '3 hr' assert i18n.format_timedelta(datetime.timedelta(hours=3), format='narrow') == '3h'
def test_format_datetime(): set_locale(Locale('en', 'US')) dt = datetime.datetime(2007, 4, 1, 15, 30) assert i18n.format_datetime(dt) == 'Apr 1, 2007, 3:30:00 PM' set_locale( Locale('fr', 'FR', timezone=pytz.timezone('Europe/Paris'), default_timezone=pytz.UTC)) dt = datetime.datetime(2007, 4, 1, 15, 30) assert i18n.format_datetime( dt, 'full' ) == u'dimanche 1 avril 2007 à 17:30:00 heure d’été d’Europe centrale' set_locale( Locale('en', timezone=pytz.timezone('US/Eastern'), default_timezone=pytz.UTC)) assert i18n.format_datetime( dt, "yyyy.MM.dd G 'at' HH:mm:ss zzz") == '2007.04.01 AD at 11:30:00 EDT'
def test_get_timezone_name(): dt = Locale(timezone='America/Los_Angeles').to_timezone( datetime.datetime.now()) set_locale(Locale('en', 'US')) assert i18n.get_timezone_name(dt) in ('Pacific Standard Time', 'Pacific Daylight Time') assert i18n.get_timezone_name(dt, return_zone=True) == 'America/Los_Angeles' assert i18n.get_timezone_name(dt, width='short') in ('PST', 'PDT') tz = pytz.timezone('America/Los_Angeles') assert i18n.get_timezone_name(tz) == 'Pacific Time' assert i18n.get_timezone_name(tz, 'short') == 'PT' set_locale(Locale('de', 'DE')) tz = pytz.timezone('Europe/Berlin') assert i18n.get_timezone_name(tz) == u'Mitteleurop\xe4ische Zeit' set_locale(Locale('pt', 'BR')) assert i18n.get_timezone_name(tz) == u'Hor\xe1rio da Europa Central' set_locale(Locale('de', 'DE')) tz = pytz.timezone('America/St_Johns') assert i18n.get_timezone_name(tz) == u'Neufundland-Zeit'
def test_get_timezone_gmt(): utc_date = datetime.datetime(2007, 4, 1, 15, 30) tz = pytz.timezone('Pacific/Pitcairn') d = Locale('en', timezone=tz).to_timezone(utc_date) assert i18n.get_timezone_gmt(d, width='long') == u'UTC-08:00' assert i18n.get_timezone_gmt(d) == u'UTC-08:00' assert i18n.get_timezone_gmt(d, width='short') == '-0800' locale = i18n.Locale('en', 'US') assert locale.get_timezone_gmt(d, width='long') == u'GMT-08:00' assert locale.get_timezone_gmt(d) == u'GMT-08:00' assert locale.get_timezone_gmt(d, width='short') == '-0800'
def test_get_period_names(): set_locale(Locale('en', 'US')) assert i18n.get_period_names()['am'] == 'AM'
def test_get_quarter_names(): set_locale(Locale('en', 'US')) assert i18n.get_quarter_names('wide')[1] == '1st quarter' set_locale(Locale('de', 'DE')) assert i18n.get_quarter_names('abbreviated')[1] == 'Q1'
def test_get_era_names(): set_locale(Locale('en', 'US')) assert i18n.get_era_names('wide')[1] == 'Anno Domini' set_locale(Locale('de', 'DE')) assert i18n.get_era_names('abbreviated')[1] == 'n. Chr.'
def test_get_group_symbol(): set_locale(Locale('en', 'US')) assert i18n.get_group_symbol() == ','
def test_get_date_format(): set_locale(Locale('en', 'US')) assert str(i18n.get_date_format()) == 'MMM d, y' set_locale(Locale('de', 'DE')) assert str(i18n.get_date_format()) == 'dd.MM.y'
def test_parse_time(): set_locale(Locale('en', 'US')) assert i18n.parse_time('15:30:00') == datetime.time(15, 30)
def test_parse_date(): set_locale(Locale('en', 'US')) assert i18n.parse_date('4/1/04') == datetime.date(2004, 4, 1) set_locale(Locale('de', 'DE')) assert i18n.parse_date('01.04.2004') == datetime.date(2004, 4, 1)
def test_get_time_format(): set_locale(Locale('en', 'US')) assert str(i18n.get_time_format()) == 'h:mm:ss a' set_locale(Locale('de', 'DE')) assert str(i18n.get_time_format('full')) == 'HH:mm:ss zzzz'
def test_get_datetime_format(): set_locale(Locale('en', 'US')) assert i18n.get_datetime_format() == '{1}, {0}'