Python 2.7.8: Different values of fromtimestamp on Windows and Linux

615 Views Asked by At

Python 2.7.8, I call:

import datetime
print datetime.datetime.fromtimestamp(10)

But there are different results depending on operating system:

  • 1970-01-01 01:00:10 - Linux
  • 1970-01-01 00:00:10 - Windows

So there is one hour shift. Is this a known issue? Is there any way to unify returned value, so that result is same on different OSes?

2

There are 2 best solutions below

0
joeButler On

Do you have any timezone differences?

https://docs.python.org/2/library/datetime.html

Have a look at the tzinfo in the datetime library docs, I wonder if that is different

1
Giova On

It's likely the two operating systems had different timezone settings.

The python standard library does not make available an implementation of timezone (as written in the python's documentation for tzinfo). Anyway you should use the third-party (pure-python) pytz module like in the following code.

from datetime import datetime
from pytz import timezone

tz = timezone('America/St_Johns')
datetime.fromtimestamp(10, tz)