Search code examples
pythonstrptime

Inconsistent strptime accross different machines


So basically I have the a formula for getting the UNIX time, but I am getting 10800, and not 0, in my Ubuntu machine (in my mac it works just fine):

datetime.datetime(*time.strptime("01.01.1970","%d.%m.%Y")).timestamp()
>> 10800.0

I have seen a couple different questions, but none brought me any insight.

I think it is not a timezone issue, because:

datetime.datetime(time.strptime("01.01.1970.UTC", "%d.%m.%Y.%Z").timestamp()
>> 10800.0

Solution

  • This is a timezone issue. time.strptime, which relies on posix strptime returns a local time in all cases.

    Please read the documentation of strptime carefully, you will note that while %Z is supported as a format character "fields are parsed, but no field in tm is changed"

    Generally speaking, a posix implementation of strptime that does support %Z will only support the values of UTC, GMT or the systems tzname (available in python from time.tzname). But it will not alter the return value based upon them.