服务器或客户端在发送/接收日期时应处理时区吗? [英] Should the Server or the Client handle timezone when sending/receiving dates?

查看:174
本文介绍了服务器或客户端在发送/接收日期时应处理时区吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解最佳做法是将UTC日期/时间存储在我们的数据库中,并以当地时区显示给用户.我们将存储时区而不是偏移量,以便支持夏时制.

哪种方法最适合处理时区?

选项1:服务器仅讲UTC,客户端将转换为时区

所有以HTML格式发送到浏览器的日期均为UTC.客户端将使用moment.js或类似方法将UTC时间转换为正确的时区.将使用我们数据库中指定的时区,而不是浏览器的本地时间,因为这可能是错误的.

用户提交日期或时间时,必须先将其转换为UTC时间,然后再提交给服务器.

选项2:服务器转换为时区

所有计算仍以UTC进行,并且日期/时间会在最后可能的时刻(例如,以HTML输出日期时)转换为正确的时区.

当客户提交任何日期/时间时,应立即将其转换为UTC.

还有其他方法吗?

解决方案

每种方法均有效,但是每种方法都有其优点和缺点.有几件事情要考虑:

  • JavaScript的本地时区转换仅限于其运行所在计算机的本地时区,并且也是此处列出的时区库之一.我个人建议时刻-时区.

  • 在客户端(使用任何库)中转换值将需要时区数据,如果要支持世界上所有时区,该数据可能会有些大.您可能需要考虑将数据限制为满足您要求的最小子集.

  • 服务器端时区转换也需要有效的时区数据. PHP具有内置功能,您可以随时了解PECL中 timezonedb软件包的更新./p>

  • 您还用ASP.Net标记了您的问题. .Net包括TimeZoneInfo,它仅支持Microsoft时区.如果您想要标准的IANA时区(如PHP中使用的时区),则将需要一个库.我建议诺达时间.您可以在时区标签Wiki 中了解有关不同时区的更多信息.

  • 如何交付最终结果取决于您正在编写哪种类型的应用程序.

    • 如果创建的是传统"网页,其中HTML是在服务器端呈现的,那么您将呈现人类可读的字符串.这意味着不仅要了解时区,还要了解用户的语言环境,以便您可以使用符合文化习惯的格式.

      这根本与时区无关.例如,您可能和美国人使用MM/DD/YYYY格式,但实际上仍然位于欧洲并使用欧洲时区.

    • 如果您的服务器通过API(即JSON,XML等)传递结果,则结果应该是机器可读的.最好使用 ISO 8601 格式.具体来说,时间戳记应按照 RFC 3339 中的说明进行传递.

      这意味着它们应该始终包括Z(用于UTC值)或偏移量(用于本地值).因此,您可以考虑用户的时区,但是传递的结果应包括本地偏移量,这样就不会造成歧义.

      例如,您具有UTC值2014-07-09T12:00:00Z,然后在America/Los_Angeles中它是2014-07-09T05:00:00-07:00,这就是应该通过API传递的内容.

      在客户端,您可以使用 moment.js 之类的库为用户呈现该值.例如:

      var s = moment.parseZone("2014-07-09T05:00:00-07:00").format("LLL");
      

相关:如何正确使用时区?

I understand the best practice is to store UTC date/times in our database and display to the user in their local timezone. We'll store the timezone rather than the offset so we can support daylight savings time.

What way is best for handling timezones?

Option 1: Server speaks UTC only and client converts to timezone

All dates sent to the browser in HTML are in UTC. The client side will convert the UTC time to the correct timezone using moment.js or similar. The timezone specified in our database would be used rather than the browsers local time since that may be incorrect.

When the user submits a date or time, it must first be converted to a UTC time before being submitted to the server.

Option 2: Server converts to timezone

All calculations are still made in UTC and date/times are converted to the correct timezone at the last possible moment, for example when outputting the date in HTML.

When the client submits any date/times, it should be immediately converted to UTC.

Is there another way?

解决方案

Either option is valid, but there are advantages and disadvantages of each approach. There are several things to consider:

  • JavaScript's native time zone conversion is limited to the local time zone of the machine it is running on, and it is also seriously broken. If you plan to convert values in the client, you'll need one of the time zone libraries listed here. I personally recommend moment-timezone.

  • Converting values in the client (with any library) is going to require time zone data, which can be somewhat large if you want to support all of the time zones of the world. You may want to consider limiting the data to the minimal subset that meets your requirements.

  • Server-side time zone conversion also requires valid time zone data. PHP has this built in, and you can stay current with updates to the timezonedb package in PECL.

  • You also tagged your question with ASP.Net. .Net includes TimeZoneInfo, which offers support for Microsoft time zones only. If you want standard IANA time zones (like those used in PHP), then you will need a library. I recommend Noda Time. You can read more about the different kinds of time zones in the timezone tag wiki.

  • How you deliver the end result depends on what kind of application you are writing.

    • If you are creating a "traditional" web page, where the HTML is rendered server side, then you will be rendering a human-readable string. That means understanding not only the time zone, but also the user's locale so you can use a culturally appropriate format.

      This is not at all tied to time zone. For example, you might be and American using the MM/DD/YYYY format, but still be physically located in Europe and using a European time zone.

    • If your server is delivering results via an API (i.e. JSON, XML, etc.), then your results should be machine readable. Preferably, you should use the ISO 8601 format. Specifically, timestamps should be delivered as described in RFC 3339.

      This means that they should always include either a Z (for UTC values), or an offset (for local values). Therefore, you may take the user's time zone into account, but the result you deliver should include the local offset so it is unambiguous.

      For example, you have the UTC value 2014-07-09T12:00:00Z, then in America/Los_Angeles it's 2014-07-09T05:00:00-07:00 and that's what should be delivered over the API.

      On the client-side, you can render that value for the user with a library like moment.js. For example:

      var s = moment.parseZone("2014-07-09T05:00:00-07:00").format("LLL");
      

Related: How to properly work with Timezone?

这篇关于服务器或客户端在发送/接收日期时应处理时区吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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