Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Windows, how to convert a timestamps BEFORE 1970 into something manageable?

Tags:

python

windows

Summary: "negative" timestamps on Mac work fine, but on Windows I can't convert them into something usable.

Details: I can have a file on Windows whose modification time is, say 1904:

$ ls -l peter.txt
-rw-r--r--    1 sync     Administ        1 Jan  1  1904 peter.txt

In python:

>>> import os
>>> ss = os.stat('peter.txt')
>>> ss.st_mtime
-2082816000.0

Great. But I can't figure out how to turn that negative timestamp into a date/time string. On Mac this code works fine.

>>> datetime.fromtimestamp(-2082816000)
datetime.datetime(1904, 1, 1, 0, 0)

And from here I can do whatever I want in terms of formatting.

But on Windows it fails:

>>> datetime.fromtimestamp(-2082816000)
Traceback (most recent call last):    
  File "<stdin>", line 1, in <module>
ValueError: timestamp out of range for platform localtime()/gmtime() function

And trying anything else I can think of fails:

>>> time.gmtime(-2082816000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: (22, 'Invalid argument')

the wonderful python-dateutil package doesn't seem to have this facility. I've looked though time, calendar, and datetime module. Any help?

like image 891
Potrebic Avatar asked Dec 11 '22 07:12

Potrebic


1 Answers

>>> datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=-2082816000)
datetime.datetime(1904, 1, 1, 8, 0)
like image 122
Ignacio Vazquez-Abrams Avatar answered May 20 '23 06:05

Ignacio Vazquez-Abrams