Does Python's Time.Time() Return the Local or Utc Timestamp

Does Python's time.time() return the local or UTC timestamp?

The time.time() function returns the number of seconds since the epoch, as a float. Note that "the epoch" is defined as the start of January 1st, 1970 in UTC. So the epoch is defined in terms of UTC and establishes a global moment in time. No matter where on Earth you are, "seconds past epoch" (time.time()) returns the same value at the same moment.

Here is some sample output I ran on my computer, converting it to a string as well.

>>> import time
>>> ts = time.time()
>>> ts
1355563265.81
>>> import datetime
>>> datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
'2012-12-15 01:21:05'
>>>

The ts variable is the time returned in seconds. I then converted it to a human-readable string using the datetime library.

Is Python's time.time() timezone specific?

Yes, time.time() returns the number of seconds since an unspecified epoch. Note that on most systems, this does not include leap seconds, although it is possible to configure your system clock to include them. On cpython, time.time is implemented as a call to the C function time, which per §27.23.2.4.2 of the C standard does not have to use a specified epoch:

The time function determines the current calendar time. The encoding of the value is
unspecified.

On virtually every OS (including Linux, Mac OSX, Windows, and all other Unixes), the epoch is 1970-1-1, 00:00 UTC, and on these systems time.time is timezone-independent.

Python: UTC vs local timestamp

Following two statements would always return different result.

local_seconds = int(datetime.utcnow().timestamp())
utc_seconds = int(datetime.utcnow().replace(tzinfo=utc).timestamp())

Output:

1585584790
1585604590

You ask why? Because, by the time first statement executes, there is some time spent during execution and now the second statement would fetch you different result because datetime.utcnow() for 2nd statement has changed.

What I assume is, you want to see if both operations would give the same result or not? They definitely would have given the same results :

  1. Had you provided them the same input?
  2. Had you performed the similar operation from a common library.

To solve 1. change your code like this.

same_time_input = datetime.utcnow()

local_seconds = int(same_time_input.timestamp())
utc_seconds = int(same_time_input.replace(tzinfo=utc).timestamp())

Still the output would not be same, because you are using an external library, and the replace function is not working as you expected.

If you printout the tzinfo from same_time_input, you would see that it doesn't have any timezone info reason of which can be read here. --> Why does datetime.datetime.utcnow() not contain timezone information?

print(same_time_input.tzinfo)

Now, you are trying to give it a timezone info using a separate library which has different implementation internally resulting in slightly off results.

Why does python datetime.timestamp(0) return 1AM as opposed to 00:00

Timezones!!!

Try this:

from datetime import *
print(datetime.fromtimestamp(0, timezone.utc))

This gets the epoch time in UTC (+00:00)

Python Django convert timestamp from UTC to local timezone

It would be something like that:

from datetime import datetime
from dateutil import tz

from_zone = tz.tzutc()
to_zone = tz.tzlocal()

utc = datetime.strptime('2022-06-17 05:39:09', '%Y-%m-%d %H:%M:%S')

utc = utc.replace(tzinfo=from_zone)

# Convert to local time zone
to_local_time = utc.astimezone(to_zone)

print(utc,to_local_time)


Related Topics



Leave a reply



Submit