Skip to content

Commit

Permalink
Merge pull request #11062 from saimn/leap-second-locale
Browse files Browse the repository at this point in the history
Fix leap second update when using a non english locale
  • Loading branch information
mhvk authored and astrofrog committed Mar 15, 2021
1 parent 8d7d963 commit 903cd39
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -58,6 +58,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 @@ -1233,9 +1233,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 @@ -78,6 +78,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 @@ -1042,9 +1045,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')
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 @@ -110,8 +110,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 @@ -120,11 +120,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

0 comments on commit 903cd39

Please sign in to comment.