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

后端 未结 5 1953
我寻月下人不归
我寻月下人不归 2020-12-20 14:12

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

相关标签:
5条回答
  • 2020-12-20 14:27

    Micropython's epoch is 1/1/2000, see time() and utime.

    0 讨论(0)
  • 2020-12-20 14:33

    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.

    0 讨论(0)
  • 2020-12-20 14:35

    time() would always return the time from epoch, look at the documentation.

    Please note that Epoch is always the number of seconds since 1970, but since the clock on different machines are not the same - you'll might experience some problems.

    Citation:

    The epoch is the point where the time starts. On January 1st of that year, at 0 hours, the “time since the epoch” is zero. For Unix, the epoch is 1970. To find out what the epoch is, look at gmtime(0).

    And:

    time.time()¶

    Return the time in seconds since the epoch as a floating point number. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

    (Both from Python documentation).

    0 讨论(0)
  • 2020-12-20 14:44

    In Python pandas.to_datetime(), while the default is unix epoch origin, you can change it by providing your custom reference timestamp.

    For example,

    pd.to_datetime(18081) #default unix epoch
    Out: Timestamp('1970-01-01 00:00:00.000018081') #default is in nanosecond
    
    pd.to_datetime(18081, unit='D')
    Out: Timestamp('2019-07-04 00:00:00') #change unit of measure
    

    You can change it to any reference date. Make sure the unit is reasonable. In example below, we set the origin to January 1, 1960. Note that this is default SAS date start date.

    pd.to_datetime(18081, unit='D',  origin='1960-1-1') #change to your reference start date
    Out: Timestamp('2009-07-03 00:00:00')
    
    0 讨论(0)
  • 2020-12-20 14:46

    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.

    0 讨论(0)
提交回复
热议问题