如何正确使用时区工作? [英] How to properly work with Timezone?

查看:142
本文介绍了如何正确使用时区工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多关于时区偏移 UTC 当地时间 JavaScript函数 DST 熏肉和我试图把所有这一切共同构筑了坚实的/正确的结构,我的应用程序。

I'm reading a lot about timezone, offset, utc, local time, javascript functions, DST, bacon and I 'm trying to put this all together to build a solid/correct structure for my app.

假设我的应用程序是一样的东西计算器。

Suppose my app is something like StackOverflow.

这就是我正在做的...


  • 服务器是在另一个国家,所以我将它设置为UTC 00:00。

  • 我存储日期的DateTimeOffset

  • 我的不是存储 TimeZoneID

  • 日期被发送到客户端格式为: 2012-07-19T14:30:00-03:00

  • 我使用的角度过滤器将其转换为本地时间。

  • The server is in another country, so I set it to UTC 00:00.
  • I'm storing date as DateTimeOffset.
  • I'm not storing TimeZoneID.
  • Date is being sent to the client in this format: 2012-07-19T14:30:00-03:00.
  • I'm using angular filter to convert it to local time.

我有一个关于它的几个问题...

I have a few questions about it ...

关于我的(单个服务器)......它应该被一个中性UTC(:00 +00)上运行?如果什么,在未来,我们移动到一个农场,服务器上的不同位置上运行?

About my server (single server) ... should it be running with a "neutral" UTC (+00:00)? And what if, in the future, we move to a farm where servers run on different locations?

目前,我存储只是日期的DateTimeOffset 。我在读关于保存 TimeZoneID ,但我看到没有用在所有的这一点。我缺少的东西吗?

Currently, I'm storing just date as DateTimeOffset. I'm reading about saving the TimeZoneID but I'm seeing no use at all for this. Am I missing something?

或者我应该保存日期 DateTimeUtc TimeZoneID 和手动转换每次约会与时区类?

Or should I store date as DateTimeUtc with a TimeZoneID and manually convert every date with the TimeZone class?

它是安全的数据转换在客户端上?或日期的转换应该总是在服务器端?

Is it safe to convert data on the client? Or date conversions should be always on the server side?

使用我目前的做法。将DST得到尊重?

Using my current approach. Will DST be respected?

推荐答案

要了解关于日期的一个非常重要的事情/时间是有做没有一个正确的方式一切。常见的回答使用UTC是的不总是的适用。上下文是非常重要的,并且有根据你正在与所代表的工作中的值不同的技术和方法。如果你能在他们的应用程序中使用什么细说,我将相应地更新我的答案。在此期间,我会尽力解决您带来了已经在具体点:

One very important thing to understand about date/time is that there is no one right way for doing everything. The common answer "use UTC" is not always applicable. Context is very important, and there are different techniques and approaches based on what the values you are working with are representing. If you could elaborate on what they are used for in your application, I will update my answer accordingly. In the meantime, I'll try to address the specific points you have brought up already:

在UTC保持你的服务器是一个最佳实践,这是你可以从云供应商,如天青或AWS还期待什么。但它不是东西,你应该依赖。您的服务器能够被设置为的时间又不影响您的应用程序区。只要时钟同步与NTP服务器,时区选择不应该的问题。

Keeping your server at UTC is a best practice, and it is what you can expect from cloud providers like Azure or AWS also. But it isn't something that you should be dependent on. Your server should be able to be set to any time zone without it affecting your application. As long as the clock is in sync with an NTP server, choice of time zone should not matter.

那么,你如何保证?很简单,只要确保你的应用避免了所有的以下内容:

So how do you ensure that? Simple, just make sure your application avoids all of the following:


  • DateTime.Now

  • DateTimeKind.Local

  • 时区(整个班级)

  • TimeZoneInfo.Local

  • DateTime.ToLocalTime()

  • DateTime.ToUniversalTime()(因为它假定输入是本地)

  • 其它。其他方法假定本地输入或输出,如 TimeZoneInfo.ConvertTimeToUtc(DATETIME)(这个特殊的超载并不需要一个时区,所以它假设本地时区)

  • DateTime.Now
  • DateTimeKind.Local
  • TimeZone (the entire class)
  • TimeZoneInfo.Local
  • DateTime.ToLocalTime()
  • DateTime.ToUniversalTime() (because it assumes the input is local)
  • Misc. other methods that assume a local input or output, such as TimeZoneInfo.ConvertTimeToUtc(DateTime) (this particular overload doesn't take a time zone, so it assumes the local time zone)

另请参见我的博客文章:的反对理由DateTime.Now

See also my blog post: The Case Against DateTime.Now.

请注意,我并没有包括 DateTimeOffset.Now 在列表中。虽然这是一个设计的气味一点点,它仍然是安全的用

Note that I didn't include DateTimeOffset.Now in the list. Although it's a little bit of a design smell, it is still "safe" to use.

我建议你阅读我的回答日期时间VS的DateTimeOffset 。应该澄清一些事情。如果不反刍整个事情,主要的一点是,虽然双方代表在时间准确点,一个的DateTimeOffset 提供的视角,而UTC 的DateTime 没有。

I suggest you read my answer to DateTime vs DateTimeOffset. It should clarify some things. Without regurgitating the whole thing, the main point is that while both represent a point in time accurately, a DateTimeOffset provides perspective, while a UTC DateTime does not.

您还问什么时候应该存储 TimeZoneInfo.Id 。有些情况下这是必需的至少两种情况:

You also asked when you should store a TimeZoneInfo.Id. There are at least two scenarios where this is required:


  • 如果您正在录制在过去或现在的事件,并且计划在允许的修改的所记录的时间戳。你需要的时区,以确定哪些新的偏移量应该还是如何在新的输入转换回UTC。

  • If you are recording events in the past or present, and you plan on allowing modifications to the recorded timestamps. You need the time zone to determine what the new offset should be, or how the new input converts back to UTC.

如果你正在计划一次伸到未来,您将需要为复发模式的一部分(即使是单个事件)的时区。请参见 href=\"http://stackoverflow.com/a/19170823/634824\">和一>还,(而其他语言,同样的原则也适用)。

If you are scheduling time out into the future, you will need the time zone as part of the recurrence pattern (even for a single occurrence). See here and here also, (while for other languages, the same principles apply).

再次确切的答案取决于究竟时间戳代表。有没有一个环统治他们。

Again, the exact answer depends on what exactly the timestamps represent. There is no one ring to rule them all.

如果它是一个.NET客户端,相信你可以在那里进行转换。但是,我认为你是问一个JavaScript客户端浏览器。

If it's a .NET client, sure you can convert there. But I think you are asking about a JavaScript client browser.

安全是一个相对的概念。如果你要求准确精通,那么没有。 JavaScript是不是为安全的,由于在ECMAScript规范的错误(ES1通过ES5.1它上正在工作ES6)。你可以阅读更多在我的博客文章:的JavaScript Date类型可怕的是打破

"Safe" is a relative term. If you're asking for exact perfectness, then no. JavaScript isn't safe for that, due to an error in the ECMAScript specification (ES1 through ES5.1. It is being worked on for ES6). You can read more in my blog post: JavaScript Date type is horribly broken.

不过,如果你用相对的当前数据和应用程序的用户不是在世界上的时间的一部分区域是挥发性的,或者你不需要精确的结果的100%的时间,那么你就可以安全地使用JavaScript来转换到用户的本地时区。

However, if you are working with relatively current data, and the users of your application are not in a part of the world where time zones are volatile, or you don't require precise results 100% of the time, then you can "safely" use JavaScript to convert to the user's local time zone.

你可能避免的的实现了IANA TZDB在JavaScript中,如那些这些问题与图书馆的一些的我列出这里。但很多人仍然依赖JS 日期,所以他们仍然有问题。 (附注 - 我工作的一个JS库,将解决这个问题,但还没准备好分享)。在服务器端

You might avoid some of these issues with libraries that implement the IANA TZDB in JavaScript, such as the ones I list here. But many of them are still dependent on JS Date, so they still have issues. (Side note - I'm working on a JS library that will counter this, but it is not ready to share yet).

转化是一个非常更好的选择,只要你可以问他们的时区的用户。大多数的时候,我认为这是可行的。

Conversions on the server side are a much better choice, as long as you can ask the user for their time zone. Most of the time, I think this is doable.

您可能会考虑要求使用基于地图的时区选择器,如的这个这个。而这两者都需要你用IANA时区,这对于.NET使用野田佳彦时间,这是一个伟大的主意是指( 。恕我直言)

You might consider asking using a map-based timezone picker, such as this one or this one. Both of which will require you use IANA time zones, which for .NET means using Noda Time, which is a great idea anyway (IMHO).

通过您目前的做法,DST将内得到尊重的的电流的DST规则为用户已设置为他们的本地浏览器的时区的定义。 (再次,请参阅我的博客文章为什么是这种情况)

With your current approach, DST will be respected within the definition of the current DST rules for the time zone the user has set for their local browser. (Again, refer to my blog post for why this is the case).

从任何值A转换的偏移量(是否 -03: 00 或通过日期传递对象(我相信角度滤波器以Z )会这么做),将正确地转换为特定的UNIX时间戳。

A conversion from any value with an offset (whether -03:00 or Z) that passes through the Date object (which I believe an Angular filter will do), will properly convert to the specific unix timestamp.

该错误,将裁剪与DST转换事先DST规则是因为里面的Unix时间戳会日期对象为本地时区将始终假定的电流的DST规则是适用的,即使时间陷入一个时期,有不同的规则。

The errors that would crop up with DST conversions for prior DST rules are because going from the unix timestamp inside the Date object to the local time zone will always assume that the current DST rule is applicable, even if the time fell into a period that had a different rule.

这篇关于如何正确使用时区工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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