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

Fix leap second update when using a non english locale #11062

Merged
merged 2 commits into from Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci_workflows.yml
Expand Up @@ -68,9 +68,9 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- name: Install language-pack-de and tzdata
- name: Install language-pack-fr and tzdata
if: startsWith(matrix.os, 'ubuntu')
run: sudo apt-get install language-pack-de tzdata
run: sudo apt-get install language-pack-fr tzdata
- name: Install Python dependencies
run: python -m pip install --upgrade tox codecov
- name: Run tests
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -1110,6 +1110,8 @@ astropy.tests
astropy.time
^^^^^^^^^^^^

- Fix leap second update when using a non english locale. [#11062]

astropy.timeseries
^^^^^^^^^^^^^^^^^^

Expand Down
4 changes: 2 additions & 2 deletions astropy/io/ascii/tests/test_read.py
Expand Up @@ -1290,9 +1290,9 @@ def test_non_C_locale_with_fast_reader():

try:
if platform.system() == 'Darwin':
locale.setlocale(locale.LC_ALL, 'de_DE')
locale.setlocale(locale.LC_ALL, 'fr_FR')
else:
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
locale.setlocale(locale.LC_ALL, 'fr_FR.utf8')

for fast_reader in (True,
False,
Expand Down
10 changes: 7 additions & 3 deletions astropy/utils/iers/iers.py
Expand Up @@ -76,6 +76,9 @@
conf.auto_max_age = None
"""

MONTH_ABBR = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec']


def download_file(*args, **kwargs):
"""
Expand Down Expand Up @@ -1031,9 +1034,10 @@ def _read_leap_seconds(cls, file, **kwargs):
for line in lines:
match = cls._re_expires.match(line)
if match:
expires = Time.strptime(match.groups()[0], '%d %B %Y',
scale='tai', format='iso',
out_subfmt='date')
day, month, year = match.groups()[0].split()
month_nb = MONTH_ABBR.index(month[:3]) + 1
expires = Time(f'{year}-{month_nb:02d}-{day}',
scale='tai', out_subfmt='date')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhvk - Something like this ? 🔝

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

break
else:
raise ValueError(f'did not find expiration date in {file}')
Expand Down
20 changes: 20 additions & 0 deletions astropy/utils/iers/tests/test_leap_second.py
@@ -1,6 +1,8 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import urllib.request
import os
import locale
import platform

import pytest
import numpy as np
Expand Down Expand Up @@ -48,6 +50,24 @@ def test_read_leap_second_dat(self):
assert ls['tai_utc'][-1] >= 37
self.verify_day_month_year(ls)

def test_read_leap_second_dat_locale(self):
current = locale.setlocale(locale.LC_ALL)
try:
if platform.system() == 'Darwin':
locale.setlocale(locale.LC_ALL, 'fr_FR')
else:
locale.setlocale(locale.LC_ALL, 'fr_FR.utf8')

ls = iers.LeapSeconds.from_iers_leap_seconds(
iers.IERS_LEAP_SECOND_FILE)
except locale.Error as e:
pytest.skip(f'Locale error: {e}')
finally:
locale.setlocale(locale.LC_ALL, current)

# Below, >= to take into account we might ship and updated file.
assert ls.expires >= Time('2020-06-28', scale='tai')

def test_open_leap_second_dat(self):
ls = iers.LeapSeconds.from_iers_leap_seconds(
iers.IERS_LEAP_SECOND_FILE)
Expand Down
10 changes: 5 additions & 5 deletions astropy/utils/tests/test_misc.py
Expand Up @@ -89,8 +89,8 @@ def test_set_locale():
# First, test if the required locales are available
current = locale.setlocale(locale.LC_ALL)
try:
locale.setlocale(locale.LC_ALL, 'en_US')
locale.setlocale(locale.LC_ALL, 'de_DE')
locale.setlocale(locale.LC_ALL, 'en_US.utf8')
locale.setlocale(locale.LC_ALL, 'fr_FR.utf8')
except locale.Error as e:
pytest.skip(f'Locale error: {e}')
finally:
Expand All @@ -99,11 +99,11 @@ def test_set_locale():
date = datetime(2000, 10, 1, 0, 0, 0)
day_mon = date.strftime('%a, %b')

with misc._set_locale('en_US'):
with misc._set_locale('en_US.utf8'):
assert date.strftime('%a, %b') == 'Sun, Oct'

with misc._set_locale('de_DE'):
assert date.strftime('%a, %b') == 'So, Okt'
with misc._set_locale('fr_FR.utf8'):
assert date.strftime('%a, %b') == 'dim., oct.'

# Back to original
assert date.strftime('%a, %b') == day_mon
Expand Down