Unix 时间戳是存储时间戳的最佳方式吗? [英] Are unix timestamps the best way to store timestamps?

查看:53
本文介绍了Unix 时间戳是存储时间戳的最佳方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是对所有内容使用 unix 时间戳,但我想知道是否有更好的方法.

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?

推荐答案

无论您选择存储时间戳,重要的是要避免区域解释问题和时间偏移问题.无论区域如何,Unix 时间戳的解释都是相同的,并且无论时区如何,都从同一时间点计算 - 这些都是好事.

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.

注意将时间戳存储为不明确的字符串,例如 01/02/2008,因为这可能会被解释为 2008 年 1 月 2 日或 2008 年 2 月 1 日,具体取决于语言环境.

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.

在存储小时/分钟/秒时,重要的是要知道指定了哪个"小时/分钟/秒.您可以通过包含时区信息(Unix 时间戳不需要,因为它被假定为 UTC)来实现这一点.

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).

但是,请注意 Unix 时间戳不能唯一地表示某些时刻:当 UTC 出现闰秒时,Unix 时间戳不会改变,因此 23:59:60 UTC 和第二天的 00:00:00具有相同的 Unix 表示.因此,如果您确实需要一秒或更高分辨率,请考虑其他格式.

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.

如果您更喜欢比 Unix 时间戳更易于阅读的存储格式,请考虑 ISO 8601.

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

一种有助于保持简单明了的技术是将日期存储为 UTC,并且仅在向用户显示日期时应用时区或 DST 偏移量.

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.

这篇关于Unix 时间戳是存储时间戳的最佳方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆