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?

farthVader
  • 888
  • 11
  • 19
  • 1
    I would assume it is an OS function... – thebjorn Jan 07 '15 at 23:17
  • @thebjorn And what happens if one manually tweaks the system clock? – farthVader Jan 07 '15 at 23:24
  • @farthVader Then the function would return the tweaked time. – Gillespie Jan 07 '15 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). – thebjorn Jan 07 '15 at 23:29
  • This may sound stupid, but is the OS pre configured with these values? I'm assuming there must be some kind of sync that needs to be performed. – farthVader Jan 07 '15 at 23:37
  • 1
    Yes, the OS comes with a time zone database which is updated when time zones (and dst rules) change. – thebjorn Jan 07 '15 at 23:39
  • @farthVader http://www.iana.org/time-zones (the time zone database is now maintained by iana). – thebjorn Jan 07 '15 at 23:40
  • @thebjorn: OS may use "right" zoneinfo and sync. with GPS ntp server i.e., it may even use non-UTC time scale. – jfs Jan 08 '15 at 09:16

1 Answers1

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.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670