Are unix timestamps the best way to store timestamps?

后端 未结 9 844
梦毁少年i
梦毁少年i 2020-12-07 16:26

I always use unix timestamps for everything, but am wondering if there is a better way.

What do you use to store timestamps and why?

相关标签:
9条回答
  • 2020-12-07 16:51

    It depends on what you need the timestamps for.

    A unix timestamp cannot represent the time 1 second after 2008-12-31T23:59:59Z. If you do '2009-01-01T09:00:00' - '2008-12-31T09:00:00' with unix timestamps the result is NOT correct: there will be a leap second between those two dates and they're separated by 86401 seconds (not 86400 as unix timestamps will tell you).

    Other than that and what the other responders said, yes -- unix timestamps are the way to go :)

    0 讨论(0)
  • 2020-12-07 16:54

    If you are storing a log file, please for the love of pete make it something human readable and lexically-sortable.

    2008-10-07 09:47:02 for example.

    0 讨论(0)
  • 2020-12-07 16:55

    However you choose to store a timestamp, it is important to avoid regional interpretation problems and time offset problems. A Unix timestamp is interpreted the same regardless of region, and is calculated from the same point in time regardless of time zone - these are good things.

    Beware storing timestamps as ambiguous strings such as 01/02/2008, since that can be interpreted as January 02, 2008 or February 01, 2008, depending on locale.

    When storing hours/minutes/seconds, it is important to know "which" hour/minute/second is being specified. You can do this by including timezone information (not needed for a Unix timestamp, since it is assumed to be UTC).

    However, note that Unix timestamps cannot uniquely represent some instants in time: when there is a leap second in UTC, the Unix timestamp does not change, so both 23:59:60 UTC and 00:00:00 the next day have the same Unix representation. So if you really need one second or better resolution, consider another format.

    If you prefer a more human readable format for storage than a Unix timestamp, consider ISO 8601.

    One technique that helps keep things straight-forward is to store dates as UTC and only apply timezone or DST offsets when displaying a date to a user.

    0 讨论(0)
  • 2020-12-07 16:55

    What era do you need to store and to what resolution? If you need microseconds, or dates in the stone age time_t might not be the best. For general business purposes it's quite good (assuming 64bit)

    0 讨论(0)
  • 2020-12-07 16:56

    timeval-style (time_t + microseconds) if I need sub-second accuracy, else just time_t. You can use a 64-bit integer value to store time_t * 1000000 + usec and you are overflow-proof for over +/- 292,000 years.

    0 讨论(0)
  • 2020-12-07 16:57

    A timestamp is not a good idea on databases, because they do not take daylight savings or the current local time into account. On MySQL it is better to store it as a time, and then use the MySQL date and time functions to retreive the parts you want, or compare to other dates.

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