5

The time.time() method provides you the timestamp which is basically the time elapsed since epoch(which is in UTC).

The datetime.fromtimestamp() states that if the tz param isn't provided then a local datetime object is returned.

I am aware that the tz info is basically treated as an offset to utc in order to get the local time.

If that's the case, the current time in utc had to be extracted from the platform.

How is the current time of the day, be it local or UTC, extracted from the underlying platform?

8
  • 1
    I would assume it is an OS function... Commented Jan 7, 2015 at 23:17
  • @thebjorn And what happens if one manually tweaks the system clock? Commented Jan 7, 2015 at 23:24
  • @farthVader Then the function would return the tweaked time. Commented Jan 7, 2015 at 23:29
  • 1
    You would get the tweaked system clock... (why would you think you'd get anything else?) The OS controls setting the time and the time zone. Python/C just reads these values. In general there isn't/wasn't any way for a system to know its location. Besides, you cannot calculate the time zone purely based on longitude (time zones just aren't that regular). Commented Jan 7, 2015 at 23:29
  • 1
    Yes, the OS comes with a time zone database which is updated when time zones (and dst rules) change. Commented Jan 7, 2015 at 23:39

1 Answer 1

4

In (the current) CPython time.time() calls
floattime(NULL) which calls
_PyTime_gettimeofday_info() which calls
pygettimeofday()
where pygettimeofday() may use GetSystemTimeAsFileTime(), clock_gettime(CLOCK_REALTIME), gettimeofday() functions depending on the platform.


Unrelated: time.time() returns "seconds since the epoch". It is POSIX time on most systems supported by Python (Linux, OS X, Windows). POSIX timestamp is not the number of elapsed SI seconds since 1970-01-01T00:00:00Z (Epoch). Though it is very close (within 0.000003% counting from Epoch). It counts the number of non-leap SI seconds or the number of UT1 (mean-solar) seconds.

UTC is not a linear time scale relative to TAI, GPS time scales (see the picture). You won't get the exact elapsed SI seconds due to leap seconds (UTC is kept within ±0.9 seconds of UT1 (Earth rotation)). See also, Is a day always 86,400 epoch seconds long?

Some OSes can be configured to use non-POSIX "right" zoneinfo. time.time() is not required to return POSIX timestamp in this case. "right" timezone counts leap seconds and therefore the difference between "right" timestamps and POSIX timestamps is not constant e.g., it will increase in July 2015 due to the introduction of the positive leap second.

In principle, "the epoch" may differ from POSIX Epoch, though Python doesn't support such systems.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.