Lucas Aimaretto
Lucas Aimaretto

Reputation: 1489

Confusion converting from epoch timestamp in Python

I have the following simple script (using Python 3.4).

from dateutil.parser import *
import datetime
d1 = "1490917274299"
d2 = "1490917274"

1) When executing datetime.datetime.fromtimestamp(int(d1)).strftime('%c'), the following error raises:

>>> datetime.datetime.fromtimestamp(int(d1)).strftime('%c')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: timestamp out of range for platform time_t

2) I've read that avoiding milliseconds solves the issue. So I divide d1/1000 to get d2 and voilà!

>>> datetime.datetime.fromtimestamp(int(d2)).strftime('%c')
'Thu Mar 30 23:41:14 2017'

3) However, if I want to use parse(d2) I get an error.

>>> parse(d2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/dateutil/parser.py", line 1168, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/dateutil/parser.py", line 578, in parse
    if cday > monthrange(cyear, cmonth)[1]:
  File "/usr/lib/python3.4/calendar.py", line 121, in monthrange
    day1 = weekday(year, month, 1)
  File "/usr/lib/python3.4/calendar.py", line 113, in weekday
    return datetime.date(year, month, day).weekday()
ValueError: year is out of range

4) If you try to parse(d1) you also get an error:

>>> parse(d1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/dateutil/parser.py", line 1168, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/dateutil/parser.py", line 578, in parse
    if cday > monthrange(cyear, cmonth)[1]:
  File "/usr/lib/python3.4/calendar.py", line 121, in monthrange
    day1 = weekday(year, month, 1)
  File "/usr/lib/python3.4/calendar.py", line 113, in weekday
    return datetime.date(year, month, day).weekday()
OverflowError: Python int too large to convert to C long

5) Finally, if you use d1 in https://www.epochconverter.com/, you'll correctly get the intended date.

Why is this happening at all? I just wanted a way of checking if a string is a datetime by the use of parse() but is not working eventhouhg the epoch string is fine (at least d2).

On the other hand, why is d1 not fine as epoch?

Thanks!

Lucas

Upvotes: 1

Views: 2621

Answers (1)

Mattwmaster58
Mattwmaster58

Reputation: 2576

You've probably figured this out by now, but here it goes anyway:

dateutil.parser's parse function cannot parse Unix time — See this somewhat related issue.

So that answers 3 & 4.

Now on to 1 & 2. The reason why you can't parse d1 is because it isn't Unix time. Unix time is defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, minus the number of leap seconds that have taken place since then (Thanks Wikipedia!). If you want to include specify milliseconds, add them after the decimal, like this:

d1 = "1490917274.299"

Be sure to use float instead of int when you are parsing

datetime.datetime.fromtimestamp(float(d1)).strftime('%c')

Upvotes: 2

Related Questions