Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, is epoch time returned by time() always measured from Jan 1, 1970?

Tags:

python

time

epoch

Is the epoch start time in Python independent of the platform (i.e. always 1/1/1970)?

Or is it platform dependent?

I want to serialize datetimes (with second accuracy) on various machines running Python, and be able to read them back on different platforms, possibly also using different programming languages (than Python). Is serializing epoch time a good idea?

like image 586
kaalus Avatar asked Jan 06 '13 20:01

kaalus


People also ask

How does Python calculate epoch time?

Using strftime() to convert Python datetime to epoch strftime() is used to convert string DateTime to DateTime. It is also used to convert DateTime to epoch. We can get epoch from DateTime from strftime().

Why is January 1 1970 the epoch?

January 1st, 1970 at 00:00:00 UTC is referred to as the Unix epoch. Early Unix engineers picked that date arbitrarily because they needed to set a uniform date for the start of time, and New Year's Day, 1970, seemed most convenient.

What time time () returns in Python?

Pythom time method time() returns the time as a floating point number expressed in seconds since the epoch, in UTC. Note − Even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second.

How is epoch time calculated?

What is epoch time? The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).


2 Answers

The documentation says:

To find out what the epoch is, look at gmtime(0).

I would interpret this to mean that no particular epoch is guaranteed.

See also this Python-Dev thread. That seems to confirm the notion that, in practice, the epoch is always assumed to be 1970/01/01, but that this is not explicitly guaranteed by the language.

The upshot of this is that, at least for Python, you're probably okay using epoch time unless you're dealing with strange and obscure platforms. For reading with non-Python tools, you're probably also okay, but to be extra sure you'd need to read the documentation those tools provide.

like image 125
BrenBarn Avatar answered Sep 20 '22 04:09

BrenBarn


Epoch time (unix time) is a standard term:

http://en.wikipedia.org/wiki/Unix_time

Unix time, or POSIX time, is a system for describing instances in time, defined as the number of seconds that have elapsed since midnight Coordinated Universal Time (UTC), 1 January 1970,[note 1] not counting leap seconds.[note 2] It is used widely in Unix-like and many other operating systems and file formats. It is neither a linear representation of time nor a true representation of UTC.[note 3] Unix time may be checked on some Unix systems by typing date +%s on the command line

That means if you use the epoch times through Python, it will be consistent across platforms. Your best bet for consistency is to use UTC in all cases.

like image 20
jdi Avatar answered Sep 23 '22 04:09

jdi