hinnerk / py-tai64

A simple UTC => TAI converter and hex-encoded TAI (as used by DJBDNS) => UTC datetime.datetime decoder.
BSD 3-Clause "New" or "Revised" License
16 stars 7 forks source link

SyntaxError: invalid token: 01 #6

Open kseistrup opened 7 years ago

kseistrup commented 7 years ago

Is this module meant for Python 2 or Python 3? With Python 3 I get this exception:

$ python
Python 3.6.0 (default, Jan 16 2017, 12:12:55)
>>> import tai64n
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/site-packages/tai64n/__init__.py", line 32
    conversion_table = [(datetime(1972, 01,  1), 10.0),
                                         ^
SyntaxError: invalid token
kseistrup commented 7 years ago

The peculiar zero-padding of the months in conversion_table only works because we're talking months before August. Because of the initial 0 the number is taken as an octal by Python 2. The Python 2 octal 01 is 0o1 or 0o01 in Python 3.

Had the IERS decided to put leap seconds in August or September, the current code would also have failed in Python 2:

>>> from datetime import datetime
>>> datetime(1972,  08,  1)
  File "<stdin>", line 1
    datetime(1972,  08,  1)
                     ^
SyntaxError: invalid token
AndydeCleyre commented 5 years ago

The "new" octal syntax is supported by Python 2.7 as well. So if that value is meant to be octal, please use the 0o syntax.

Either way, this issue makes this library completely useless for Python 3, and it's the only one I know of for the task.