时间戳记应始终使用UTC吗? [英] Should timestamps always use UTC?

查看:380
本文介绍了时间戳记应始终使用UTC吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时间戳应该始终使用UTC(如2012-06-14T10:32:11+00:00)而不是本地时间(如2012-06-14T06:32:11-04:00的纽约)吗?

Should timestamps always use UTC (as in 2012-06-14T10:32:11+00:00) and not local time (as in 2012-06-14T06:32:11-04:00 for New York)?

  1. 尽管不是WordPress问题,但我相信这将是一个很好的例子-核心开发人员开发的WordPress核心,主题和插件,如果在某处输出时间戳,似乎总是使用类似get_the_date('c');get_the_modified_date('c'); -始终以UTC输出时间戳,如2012-06-14T10:32:11 +00:00 .

  1. Although not a WordPress question, I believe it'll be a strong example -- the WordPress core, themes and plugins developed by the core developers, if outputting timestamp somewhere, always seem to use something like get_the_date('c'); or get_the_modified_date('c'); -- which always output timestamp in UTC, as in 2012-06-14T10:32:11+00:00.

我刚刚遇到了这个答案,它只是指出时间戳使用UTC."

I just came across this answer which simply states that "Time stamps use UTC."

问题

时间戳应该采用UTC还是仅仅是推荐的做法? (两者之间有很多区别.)本质上是这样的-2012-06-14T06:32:11-04:00-对吗? 2012-06-14T10:32:11+00:00更好吗?

The question

Should timestamps be in UTC or is it just a recommended practise? (There's a lot of difference between the two.) Essentially is something like this -- 2012-06-14T06:32:11-04:00 -- wrong? How is 2012-06-14T10:32:11+00:00 better?

推荐答案

时间戳当然应该存储在UTC中.日期的格式取决于该时区的要求.时间戳通常存储在UTC中,因此转换为其他时区更容易且可能.

Timestamps should certainly be stored in UTC. How the date is formatted is dependent on the requirements for that timezone. Timestamps are generally stored in UTC so the conversion for display to other timezones is easier and possible.

通过仅将时间戳记作为UTC存储在数据库中,可以有效地使时区仅成为视图关注点.关于时间比较的所有数学和逻辑都可以假定为在世界范围内同步.剩下的事情仅仅是客户知道它在哪里,它在哪里的规则是什么,然后随便吐出结果.例如,通过Chrome开发者控制台在JavaScript中:

By only storing timestamps as UTC in databases, you effectively make time zones a view concern only. All math and logic in regards to comparisons of times can be assumed to sync up world-wide. The rest is simply a matter of a client knowing where it's at, what the rules are for where it's at, and then just spitting out the result. In JavaScript via the Chrome developer's console for instance:

var dateObj = new Date();

console.log(dateObj);
//spits out local time
//if you and 23 other people in every time zone across the planet created
//a date object simultaneously you'd all see a different number
//adapted to your local time zones

var utcInMillis = dateObj.getTime();
//gives UTC in milliseconds.

console.log(utcInMillis);
//if you and 23 other people in every time zone across the planet created
//that same date object `dateObj` simultaneously you'd see the same number

console.log(new Date().getTime() - utcInMillis);
//how many milliseconds passed since utcinMillis was recorded and now

var utcAsDateObj = new Date(utcInMillis);
console.log(utcAsDateObj);
//and that's how easy it is to restore to a date
//object from a UTC in  millisecond format

通常来说,在具有现代Web技术或类似选项的情况下,要做的最简单的事情就是将所有内容存储为UTC,然后将时区严格视为表示性考虑.除了在本地环境中显示某人的时间外,您不需要任何本地时间.

Generally speaking, where there's modern web technology or similar options, the least painful thing to do is to store everything as UTC and then make time zones strictly a presentational concern. You don't need local time for anything but showing somebody time in the local context.

即使您在后端遇到一些愚蠢的非UTC时间戳,也仍然值得通过在入口点进行转换并在出口处通过任何方式转换回来将UTC用作客户端的规范化选项可能的.整理时区以及在什么地方采用夏令时以及忽略夏令时太麻烦了,以至于无法自己动手做,如果您确实需要触摸一些日期对象,那通常是不可避免的.最终实际操作/比较日期/时间.

Even if you're stuck with some silly non-UTC timestamp on the back-end it's still worth making UTC your canonicalized option on the client-side by converting at point of entry and converting back at point of exit by whatever means possible. Sorting out time zones and in what places Daylight Savings Time was adopted and where its ignored is just way too much of a mess to DIY and it's typically inevitable if you actually have to touch the date object that you'll need to get a bit fancier with actually manipulating/comparing dates/times eventually.

没有比这更简便的方法,当您将所有内容以本地时间在页面上吐出之前,在以毫秒为单位将所有内容标准化为UTC之前,都没有这种方法.

There is no easier way to handle that than when everything is canonicalized as UTC in milliseconds right up the point before you spit it out on the page as a local time.

这篇关于时间戳记应始终使用UTC吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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