|
24 | 24 | from babel.util import FixedOffsetTimezone |
25 | 25 |
|
26 | 26 |
|
| 27 | +@pytest.fixture(params=["pytz.timezone", "zoneinfo.ZoneInfo"]) |
| 28 | +def timezone_getter(request): |
| 29 | + if request.param == "pytz.timezone": |
| 30 | + return timezone |
| 31 | + elif request.param == "zoneinfo.ZoneInfo": |
| 32 | + try: |
| 33 | + import zoneinfo |
| 34 | + except ImportError: |
| 35 | + try: |
| 36 | + from backports import zoneinfo |
| 37 | + except ImportError: |
| 38 | + pytest.skip("zoneinfo not available") |
| 39 | + return zoneinfo.ZoneInfo |
| 40 | + else: |
| 41 | + raise NotImplementedError |
| 42 | + |
| 43 | + |
27 | 44 | class DateTimeFormatTestCase(unittest.TestCase): |
28 | 45 |
|
29 | 46 | def test_quarter_format(self): |
@@ -583,60 +600,92 @@ def test_get_timezone_gmt(): |
583 | 600 | assert dates.get_timezone_gmt(dt, 'long', locale='fr_FR') == u'UTC-07:00' |
584 | 601 |
|
585 | 602 |
|
586 | | -def test_get_timezone_location(): |
587 | | - tz = timezone('America/St_Johns') |
| 603 | +def test_get_timezone_location(timezone_getter): |
| 604 | + tz = timezone_getter('America/St_Johns') |
588 | 605 | assert (dates.get_timezone_location(tz, locale='de_DE') == |
589 | 606 | u"Kanada (St. John\u2019s) Zeit") |
590 | 607 | assert (dates.get_timezone_location(tz, locale='en') == |
591 | 608 | u'Canada (St. John’s) Time') |
592 | 609 | assert (dates.get_timezone_location(tz, locale='en', return_city=True) == |
593 | 610 | u'St. John’s') |
594 | 611 |
|
595 | | - tz = timezone('America/Mexico_City') |
| 612 | + tz = timezone_getter('America/Mexico_City') |
596 | 613 | assert (dates.get_timezone_location(tz, locale='de_DE') == |
597 | 614 | u'Mexiko (Mexiko-Stadt) Zeit') |
598 | 615 |
|
599 | | - tz = timezone('Europe/Berlin') |
| 616 | + tz = timezone_getter('Europe/Berlin') |
600 | 617 | assert (dates.get_timezone_location(tz, locale='de_DE') == |
601 | 618 | u'Deutschland (Berlin) Zeit') |
602 | 619 |
|
603 | 620 |
|
604 | | -def test_get_timezone_name(): |
605 | | - dt = time(15, 30, tzinfo=timezone('America/Los_Angeles')) |
606 | | - assert (dates.get_timezone_name(dt, locale='en_US') == |
607 | | - u'Pacific Standard Time') |
608 | | - assert (dates.get_timezone_name(dt, locale='en_US', return_zone=True) == |
609 | | - u'America/Los_Angeles') |
610 | | - assert dates.get_timezone_name(dt, width='short', locale='en_US') == u'PST' |
611 | | - |
612 | | - tz = timezone('America/Los_Angeles') |
613 | | - assert dates.get_timezone_name(tz, locale='en_US') == u'Pacific Time' |
614 | | - assert dates.get_timezone_name(tz, 'short', locale='en_US') == u'PT' |
615 | | - |
616 | | - tz = timezone('Europe/Berlin') |
617 | | - assert (dates.get_timezone_name(tz, locale='de_DE') == |
618 | | - u'Mitteleurop\xe4ische Zeit') |
619 | | - assert (dates.get_timezone_name(tz, locale='pt_BR') == |
620 | | - u'Hor\xe1rio da Europa Central') |
621 | | - |
622 | | - tz = timezone('America/St_Johns') |
623 | | - assert dates.get_timezone_name(tz, locale='de_DE') == u'Neufundland-Zeit' |
624 | | - |
625 | | - tz = timezone('America/Los_Angeles') |
626 | | - assert dates.get_timezone_name(tz, locale='en', width='short', |
627 | | - zone_variant='generic') == u'PT' |
628 | | - assert dates.get_timezone_name(tz, locale='en', width='short', |
629 | | - zone_variant='standard') == u'PST' |
630 | | - assert dates.get_timezone_name(tz, locale='en', width='short', |
631 | | - zone_variant='daylight') == u'PDT' |
632 | | - assert dates.get_timezone_name(tz, locale='en', width='long', |
633 | | - zone_variant='generic') == u'Pacific Time' |
634 | | - assert dates.get_timezone_name(tz, locale='en', width='long', |
635 | | - zone_variant='standard') == u'Pacific Standard Time' |
636 | | - assert dates.get_timezone_name(tz, locale='en', width='long', |
637 | | - zone_variant='daylight') == u'Pacific Daylight Time' |
638 | | - |
639 | | - localnow = datetime.utcnow().replace(tzinfo=timezone('UTC')).astimezone(dates.LOCALTZ) |
| 621 | +@pytest.mark.parametrize( |
| 622 | + "tzname, params, expected", |
| 623 | + [ |
| 624 | + ("America/Los_Angeles", {"locale": "en_US"}, u"Pacific Time"), |
| 625 | + ("America/Los_Angeles", {"width": "short", "locale": "en_US"}, u"PT"), |
| 626 | + ("Europe/Berlin", {"locale": "de_DE"}, u"Mitteleurop\xe4ische Zeit"), |
| 627 | + ("Europe/Berlin", {"locale": "pt_BR"}, u"Hor\xe1rio da Europa Central"), |
| 628 | + ("America/St_Johns", {"locale": "de_DE"}, u"Neufundland-Zeit"), |
| 629 | + ( |
| 630 | + "America/Los_Angeles", |
| 631 | + {"locale": "en", "width": "short", "zone_variant": "generic"}, |
| 632 | + u"PT", |
| 633 | + ), |
| 634 | + ( |
| 635 | + "America/Los_Angeles", |
| 636 | + {"locale": "en", "width": "short", "zone_variant": "standard"}, |
| 637 | + u"PST", |
| 638 | + ), |
| 639 | + ( |
| 640 | + "America/Los_Angeles", |
| 641 | + {"locale": "en", "width": "short", "zone_variant": "daylight"}, |
| 642 | + u"PDT", |
| 643 | + ), |
| 644 | + ( |
| 645 | + "America/Los_Angeles", |
| 646 | + {"locale": "en", "width": "long", "zone_variant": "generic"}, |
| 647 | + u"Pacific Time", |
| 648 | + ), |
| 649 | + ( |
| 650 | + "America/Los_Angeles", |
| 651 | + {"locale": "en", "width": "long", "zone_variant": "standard"}, |
| 652 | + u"Pacific Standard Time", |
| 653 | + ), |
| 654 | + ( |
| 655 | + "America/Los_Angeles", |
| 656 | + {"locale": "en", "width": "long", "zone_variant": "daylight"}, |
| 657 | + u"Pacific Daylight Time", |
| 658 | + ), |
| 659 | + ("Europe/Berlin", {"locale": "en_US"}, u"Central European Time"), |
| 660 | + ], |
| 661 | +) |
| 662 | +def test_get_timezone_name_tzinfo(timezone_getter, tzname, params, expected): |
| 663 | + tz = timezone_getter(tzname) |
| 664 | + assert dates.get_timezone_name(tz, **params) == expected |
| 665 | + |
| 666 | + |
| 667 | +@pytest.mark.parametrize("timezone_getter", ["pytz.timezone"], indirect=True) |
| 668 | +@pytest.mark.parametrize( |
| 669 | + "tzname, params, expected", |
| 670 | + [ |
| 671 | + ("America/Los_Angeles", {"locale": "en_US"}, u"Pacific Standard Time"), |
| 672 | + ( |
| 673 | + "America/Los_Angeles", |
| 674 | + {"locale": "en_US", "return_zone": True}, |
| 675 | + u"America/Los_Angeles", |
| 676 | + ), |
| 677 | + ("America/Los_Angeles", {"width": "short", "locale": "en_US"}, u"PST"), |
| 678 | + ], |
| 679 | +) |
| 680 | +def test_get_timezone_name_time_pytz(timezone_getter, tzname, params, expected): |
| 681 | + """pytz (by design) can't determine if the time is in DST or not, |
| 682 | + so it will always return Standard time""" |
| 683 | + dt = time(15, 30, tzinfo=timezone_getter(tzname)) |
| 684 | + assert dates.get_timezone_name(dt, **params) == expected |
| 685 | + |
| 686 | + |
| 687 | +def test_get_timezone_name_misc(timezone_getter): |
| 688 | + localnow = datetime.utcnow().replace(tzinfo=timezone_getter('UTC')).astimezone(dates.LOCALTZ) |
640 | 689 | assert (dates.get_timezone_name(None, locale='en_US') == |
641 | 690 | dates.get_timezone_name(localnow, locale='en_US')) |
642 | 691 |
|
|
0 commit comments