Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests fail on days when EST/EDT swap happens #988

Open
mweinelt opened this issue Mar 12, 2023 · 6 comments
Open

Tests fail on days when EST/EDT swap happens #988

mweinelt opened this issue Mar 12, 2023 · 6 comments

Comments

@mweinelt
Copy link

mweinelt commented Mar 12, 2023

Overview Description

Timezone related test fails on Babel 1.12.1 around when eastern daylight and eastern standard time swap.

Steps to Reproduce

  1. Run the tests on the 2nd sunday of march or the 1st sunday in november

Actual Results

_____________________ test_format_time[zoneinfo.ZoneInfo] ______________________

timezone_getter = <class 'zoneinfo.ZoneInfo'>

    def test_format_time(timezone_getter):
        t = time(15, 30)
        assert dates.format_time(t, locale='en_US') == '3:30:00\u202fPM'
        assert dates.format_time(t, format='short', locale='de_DE') == '15:30'
    
        assert (dates.format_time(t, "hh 'o''clock' a", locale='en') ==
                "03 o'clock PM")
    
        paris = timezone_getter('Europe/Paris')
        eastern = timezone_getter('US/Eastern')
    
        t = _localize(paris, datetime(2007, 4, 1, 15, 30))
        fr = dates.format_time(t, format='full', tzinfo=paris, locale='fr_FR')
        assert fr == '15:30:00 heure d’été d’Europe centrale'
    
        custom = dates.format_time(t, "hh 'o''clock' a, zzzz", tzinfo=eastern, locale='en')
        assert custom == "09 o'clock AM, Eastern Daylight Time"
    
        t = time(15, 30)
        paris = dates.format_time(t, format='full', tzinfo=paris, locale='fr_FR')
        assert paris == '15:30:00 heure normale d’Europe centrale'
    
        us_east = dates.format_time(t, format='full', tzinfo=eastern, locale='en_US')
>       assert us_east == '3:30:00\u202fPM Eastern Standard Time'
E       AssertionError: assert '3:30:00\u202...Daylight Time' == '3:30:00\u202...Standard Time'
E         - 3:30:00PM Eastern Standard Time
E         ?                    ^ ------
E         + 3:30:00PM Eastern Daylight Time
E         ?                    ^^^^^^^

tests/test_dates.py:609: AssertionError

Expected Results

Tests should likely be frozen in time.

Reproducibility

Around when the daylight saving time goes in and out of effect.

Additional Information

@akx
Copy link
Member

akx commented Mar 12, 2023

Yep, tests are known to do weird things in non-UTC time: #757 #935

Thanks for the additional data point though – and PRs are welcome :)

@mweinelt
Copy link
Author

We are running in UTC time and exporting TZ=UTC does not work around that issue.

The two doctest tests were fixed by providing pytz to the test environment. So I've updated the original post.

mgorny added a commit to mgorny/babel that referenced this issue Apr 7, 2023
Freeze the date when performing the tests for format_time() with
a timezone specified.  Since the time object does not specify a date,
the formatter uses the format string specific to the current date.
As a result, if the current DST state is different than when the test
was last updated, it failed.

This fix covers only regular tests.  I have no idea how to do the same
for doctests.

Issue python-babel#988
@mgorny
Copy link
Contributor

mgorny commented Apr 7, 2023

I've submitted #998 as a partial solution. It freezes the date to get predictable format strings.

That said, I've no clue how to do the same for doctests.

@hroncok
Copy link
Contributor

hroncok commented Apr 13, 2023

Interestingly, the doctests only fail for me without pytz.

@mgorny
Copy link
Contributor

mgorny commented Apr 13, 2023

Interestingly, the doctests only fail for me without pytz.

Ah, maybe that's why I couldn't reproduce it at first — I (wrongly) guessed that we aren't running doctests at all on Gentoo. So perhaps fixing just the main suite will be good enough.

@tornaria
Copy link

With #998, tests pass when pytz is installed, but there are still two tests failures when pytz is not installed:

=================================== FAILURES ===================================
______________________ [doctest] babel.dates.format_time _______________________
767 
768     These timezone calculations are **not** performed if the value is of type
769     ``datetime.time``, as without date information there's no way to determine
770     what a given time would translate to in a different timezone without
771     information about whether daylight savings time is in effect or not. This
772     means that time values are left as-is, and the value of the `tzinfo`
773     parameter is only used to display the timezone name if needed:
774 
775     >>> t = time(15, 30)
776     >>> format_time(t, format='full', tzinfo=get_timezone('Europe/Paris'),
Expected:
    u'15:30:00 heure normale d\u2019Europe centrale'
Got:
    '15:30:00 heure d’été d’Europe centrale'

/builddir/python3-Babel-2.12.1/babel/dates.py:776: DocTestFailure
___________________ [doctest] babel.dates.get_timezone_name ____________________
536 Return the localized display name for the given timezone. The timezone
537     may be specified using a ``datetime`` or `tzinfo` object.
538 
539     >>> from datetime import time
540     >>> dt = time(15, 30, tzinfo=get_timezone('America/Los_Angeles'))
541     >>> get_timezone_name(dt, locale='en_US')
Expected:
    u'Pacific Standard Time'
Got:
    'Pacific Daylight Time'

/builddir/python3-Babel-2.12.1/babel/dates.py:541: DocTestFailure

The second one has an easy fix, change time(15, 30, ...) for datetime(2023, 1, 1, 15, 30, ...), but I'm not sure about the first one.

But just changing the date here (as done in #998) seems just hiding that there is a discrepancy for timezones with pytz vs zoneinfo:

  1. when pytz is not installed:
$ python
Python 3.11.3 (main, Apr  6 2023, 22:56:38) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import time
>>> from babel.dates import get_timezone, get_timezone_name
>>> dt = time(15, 30, tzinfo=get_timezone('America/Los_Angeles'))
>>> get_timezone_name(dt, locale='en_US')
'Pacific Daylight Time'
>>> 
  1. when pytz is installed:
$ python
Python 3.11.3 (main, Apr  6 2023, 22:56:38) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import time
>>> from babel.dates import get_timezone, get_timezone_name
>>> dt = time(15, 30, tzinfo=get_timezone('America/Los_Angeles'))
>>> get_timezone_name(dt, locale='en_US')
'Pacific Standard Time'

Which one is the correct behaviour?

bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this issue May 9, 2023
https://build.opensuse.org/request/show/1085498
by user dgarcia + dimstar_suse
- Add fix-tests.patch to make tests work with EST/EDT swap,
  gh#python-babel/babel#988
- Update to 2.12.1:
  * Include py.typed file in wheels by @AlexWaygood in #975
  * Become 2.12.1 by @akx in #976
- 2.12.0:
  * docs(changes): Fix broken issue links by @DenverCoder1 in #922
  * Update docs build configuration by @akx in #924
  * feat: Adds Format.compact_decimal utility by @DenverCoder1 in #921
  * Remove vestigial Python 2 long check by @akx in #925
  * feat: Support for short compact currency formats by @DenverCoder1 in #926
  * Drop support for EOL Python 3.6 by @akx in #919
  * Cast number to Decimal in _get_compact_format by @DenverCoder1 in #930
  * Replace %/.format/concatenation with f-strings where feasible by @akx in #927
  * ci: Fix testing dependencies by @DenverCoder1 in #9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants