skyfielders / python-skyfield

Elegant astronomy for Python
MIT License
1.41k stars 211 forks source link

Possible problem with setting locale German when parsing leap seconds file #217

Closed mworion closed 5 years ago

mworion commented 5 years ago

Hi Brandon,

first of all many thanks for all the learning I received from watching your talks at PyCon’s the last years. I developed a mount model build package in python and recognized now that my python was less than beginner. With that said I started again and stepped over Skyfield (I used astropy for some use cases before) and I'm using this package now.

On the way I did some test automation on travis and fell over an possible issue. I’m doing tests with pytest in my IDE (PyCharm), they run fine. Once moving because of travis-ci to command line, I got there following errors in Skyfield when reading the LeapSeconds.dat while parsing the expiration date in the file:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mountcontrol/qtmount.py:144: in __init__ verbose=verbose, /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mountcontrol/mount.py:86: in __init__ verbose=self.verbose, /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mountcontrol/obsSite.py:136: in __init__ self.loadTimescale() /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mountcontrol/obsSite.py:168: in loadTimescale self.ts = load.timescale() /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/skyfield/iokit.py:274: in timescale leap_dates, leap_offsets = self('Leap_Second.dat') /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/skyfield/iokit.py:151: in __call__ expiration_date, data = parser(f) /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/skyfield/iokit.py:372: in parse_leap_seconds dt = datetime.strptime(line, '# File expires on %d %B %Y\n') /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py:565: in _strptime_datetime tt, fraction = _strptime(data_string, format)

From some tests my guess is that the reason for this behavior is related to the setting of locale environment. I’m German, so my settings are in „de_DE“ obviously. When having set locale to this mode (as well in testing):

locale.setlocale(locale.LC_ALL, „de_DE“)

the test throw the error above. When moving to

locale.setlocale(locale.LC_ALL, "en_US")

the parser reads the right language (as the file is written in English). To be more precise the error happens when parsing the month of year with "%B" statement in the following line:

371. dt = datetime.strptime(line, '# File expires on %d %B %Y\n')

in iokit.py. Keeping US locale would conflict with my application and computer settings, which are German. Does that sound right? Any feedback appreciated,

Michel

brandon-rhodes commented 5 years ago

Thank you for pointing out this problem! I found ideas here for a fix:

https://stackoverflow.com/questions/18593661/how-do-i-strftime-a-date-object-in-a-different-locale

So if you now install Skyfield from its master branch, you should be able to check if it's really fixed for you:

pip install https://github.com/skyfielders/python-skyfield/archive/master.zip

Please report back if it's really fixed for you now; if not, then we can re-open this issue and try again.

mworion commented 5 years ago

Brandon,

I could verify the fix success. Many thanks !

Michel

brandon-rhodes commented 5 years ago

Thank you for checking back so quickly — I would never have run into this myself, and I'm glad to have it fixed!

LIII-XXII commented 5 years ago

Same problem with a Japanese locale here. The fix works for me too.