日期时间VS的DateTimeOffset [英] DateTime vs DateTimeOffset

查看:218
本文介绍了日期时间VS的DateTimeOffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我们已处理的时区感知方式.NET创建DateTime对象的标准方式:每当我们产生一个DateTime我们这样做是在UTC(例如使用DateTime.UtcNow),而当我们显示一个,我们转换回从UTC到用户的本地时间。

Currently we have a standard way of dealing with .net DateTimes in a TimeZone aware way: Whenever we produce a DateTime we do it in UTC (e.g. using DateTime.UtcNow), and whenever we display one, we convert back from UTC to the user's local time.

这工作得很好,但我一直在阅读有关的DateTimeOffset以及它如何捕捉物体本身的本地和UTC时间。所以,问题是,这将是使用的DateTimeOffset VS我们已经在做的优势是什么?

That works fine, but I've been reading about DateTimeOffset and how it captures the local and UTC time in the object itself. So the question is, what would be the advantages of using DateTimeOffset vs what we have already been doing?

推荐答案

的DateTimeOffset 是一个重新presentation的瞬时间的(也被称为的绝对时间的)。通过这一点,我的意思是一个时刻是普遍为大家(不占闰秒的,或时间扩张)。另一种方式重新present瞬时间是一个的DateTime ,其中 .Kind DateTimeKind.Utc

DateTimeOffset is a representation of instantaneous time (also known as absolute time). By that, I mean a moment in time that is universal for everyone (not accounting for leap seconds, or the relativistic effects of time dilation). Another way to represent instantaneous time is with a DateTime where .Kind is DateTimeKind.Utc.

这是从不同的日历时间的(也被称为的民用时间的),这是对别人的日历中的地位,并有许多不同的日历全球各地。我们称这些挂历的时区的。日历时间是由的DateTime ,其中 .Kind DateTimeKind.Unspecified psented重新$ P $ DateTimeKind.Local 。而。当地是唯一有意义的,你必须在这里被使用结果电脑被定位隐含理解的情景。 (例如,一个用户的工作站)

This is distinct from calendar time (also known as civil time), which is a position on someone's calendar, and there are many different calendars all over the globe. We call these calendars time zones. Calendar time is represented by a DateTime where .Kind is DateTimeKind.Unspecified, or DateTimeKind.Local. And .Local is only meaningful in scenarios where you have an implied understanding of where the computer that is using the result is positioned. (For example, a user's workstation)

那么,为什么的DateTimeOffset ,而不是UTC 的DateTime 这是所有关于透视让我们用一个比喻 - 。我们将pretend是摄影师

So then, why DateTimeOffset instead of a UTC DateTime? It's all about perspective. Let's use an analogy - we'll pretend to be photographers.

想象一下,你正站在一个日历时间轴上,指着相机对准人物的瞬时时间表在你面前奠定了。周期性因夏令时间而改变,或因其他更改时区的法律定义 - 您可以根据时区的规则,排队相机。 (你没有一个稳定的手,让你的相机是不可靠的。)

Imagine you are standing on a calendar timeline, pointing a camera at a person on the instantaneous timeline laid out in front of you. You line up your camera according to the rules of your timezone - which change periodically due to daylight saving time, or due to other changes to the legal definition of your time zone. (You don't have a steady hand, so your camera is shaky.)

的人站在照片中会看到在该相机随附的角度。如果其他人拍照,他们可以从不同的角度。这就是偏移部分的的DateTimeOffset 重presents。

The person standing in the photo would see the angle at which your camera came from. If others were taking pictures, they could be from different angles. This is what the Offset part of the DateTimeOffset represents.

如果你标记您的相机东部时间,有时你是从-5指点,有时你是从-4指向左右。有相机在世界各地,都标有不同的东西,所有的指向从不同的角度相同的瞬时时间表。其中有些是旁边(或之上)对方,所以只要知道偏移量是不够的,以确定哪个时区的时间与。

So if you label your camera "Eastern Time", sometimes you are pointing from -5, and sometimes you are pointing from -4. There are cameras all over the world, all labeled different things, and all pointing at the same instantaneous timeline from different angles. Some of them are right next to (or on top of) each other, so just knowing the offset isn't enough to determine which timezone the time is related to.

又是怎么回事UTC?嗯,这是一个摄像头,在那里,可以保证有一个稳定的手。这是在三脚架上,牢固地锚定到地面。它不会去任何地方。我们把视角的角度零点偏移。

And what about UTC? Well, it's the one camera out there that is guaranteed to have a steady hand. It's on a tripod, firmly anchored into the ground. It's not going anywhere. We call its angle of perspective the zero offset.

所以 - 是什么这个比喻告诉我们什么?它提供了一些直观的指导方针。

So - what does this analogy tell us? It provides some intuitive guidelines.

  • 如果您正在重新presenting时间相对于一些地方,特别是与的DateTime 重present它的日历时间。只要确保你永远不要混淆一个日历与另一个。 未指定应该是你的假设。 本地仅用于从 DateTime.Now 到来。例如,我可能会 DateTime.Now ,并将其保存在数据库中 - 但是当我找回它,我必须假定它是未指定。我不能靠我的本地日历是相同的日历,它最初是取自。

  • If you are representing time relative to some place in particular, represent it in calendar time with a DateTime. Just be sure you don't ever confuse one calendar with another. Unspecified should be your assumption. Local is only useful coming from DateTime.Now. For example, I might get DateTime.Now and save it in a database - but when I retrieve it, I have to assume that it is Unspecified. I can't rely that my local calendar is the same calendar that it was originally taken from.

如果你必须时刻一定的一瞬间,请确保您重新presenting瞬时间。使用的DateTimeOffset 来强制执行,或者使用UTC 的DateTime 按照约定。

If you must always be certain of the moment, make sure you are representing instantaneous time. Use DateTimeOffset to enforce it, or use UTC DateTime by convention.

如果您需要跟踪的瞬时时间的时刻,你想也知道什么时候没的用户认为这是对他们的本地日历? - 那么你的必须的使用的DateTimeOffset 。这是计时系统非常重要,例如 - 无论是技术和法律问题

If you need to track a moment of instantaneous time, but you want to also know "What time did the user think it was on their local calendar?" - then you must use a DateTimeOffset. This is very important for timekeeping systems, for example - both for technical and legal concerns.

如果您需要修改previously录的DateTimeOffset - 你不必在单独偏移足够的信息,以确保新的偏移还是相关的用户。您必须的的存储区标识符(想 - 我需要一个摄像头的名字,所以我可以拍一张新照片,即使位置发生了变化)。

If you ever need to modify a previously recorded DateTimeOffset - you don't have enough information in the offset alone to ensure that the new offset is still relevant for the user. You must also store a timezone identifier (think - I need the name of that camera so I can take a new picture even if the position has changed).

还应当指出的是野田佳彦时间已经重新presentation名为 ZonedDateTime 此,而.NET基类库中没有类似的话。您需要来存储一个的DateTimeOffset TimeZoneInfo.Id 值。

It should also be pointed out that Noda Time has a representation called ZonedDateTime for this, while the .Net base class library does not have anything similar. You would need to store both a DateTimeOffset and a TimeZoneInfo.Id value.

有时候,你会想重新present日历时间是本地的任何人是看着它。例如,定义在什么的今天的意思。今天是总是午夜到午夜,但这些重present瞬时时间轴上重叠的范围的近无限数目。 (在实践中,我们有时区数量有限的,但你可以EX preSS偏移量下降到刻度线),因此,在这些情况下,一定要了解如何可以限制是谁问?问题到一个单一的时间段,或处理它们转换回到瞬时间为合适。

Occasionally, you will want to represent a calendar time that is local to "whomever is looking at it". For example, when defining what today means. Today is always midnight to midnight, but these represent a near-infinite number of overlapping ranges on the instantaneous timeline. (In practice we have a finite number of timezones, but you can express offsets down to the tick) So in these situations, make sure you understand how to either limit the "who's asking?" question down to a single time zone, or deal with translating them back to instantaneous time as appropriate.

下面是关于的DateTimeOffset 其他几个小位的备份这个比喻,并保持笔直的一些提示:

Here are a few other little bits about DateTimeOffset that back up this analogy, and some tips for keeping it straight:

  • 如果你比较两个的DateTimeOffset 的价值观,他们是先归到零比较之前所抵消。换句话说, 2012-01-01T00:00:00 + 00:00 2012-01-01T02:00:00 + 02:00 指的是相同的瞬时时刻,并因此等效

  • If you compare two DateTimeOffset values, they are first normalized to zero offset before comparing. In other words, 2012-01-01T00:00:00+00:00 and 2012-01-01T02:00:00+02:00 refer to the same instantaneous moment, and are therefore equivalent.

如果你正在做的任何单元测试,并需一定的偏移量,试验的两个的的的DateTimeOffset 值, .Offset 属性分开。

If you are doing any unit testing and need to be certain of the offset, test both the DateTimeOffset value, and the .Offset property separately.

有内置的.NET框架,可以让你通过一个单向的隐式转换一个的DateTime 到任何的DateTimeOffset 参数或变量。这样做时, .Kind 问题。如果您传递UTC一种,它将搭载与零点偏移,但如果你通过其中。当地 .Unspecified ,它将承担为本地。该框架基本上是说,好了,你叫我的日历时间转换为瞬时间,但我不知道在哪里这个来的,所以我只是将使用本地日历。这是一个巨大的疑难杂症,如果你的计算机上加载了一个未指定的的DateTime 用不同的时区。 (恕我直言 - 应该抛出一个异常 - 但事实并非如此)

There is a one-way implicit conversion built in to the .Net framework that lets you pass a DateTime into any DateTimeOffset parameter or variable. When doing so, the .Kind matters. If you pass a UTC kind, it will carry in with a zero offset, but if you pass either .Local or .Unspecified, it will assume to be local. The framework is basically saying, "Well, you asked me to convert calendar time to instantaneous time, but I have no idea where this came from, so I'm just going to use the local calendar." This is a huge gotcha if you load up an unspecified DateTime on a computer with a different timezone. (IMHO - that should throw an exception - but it doesn't.)

无耻插头:

很多人都与我分享他们发现的这个比喻极其宝贵的,所以我把它在我的Pluralsight当然的日期和时间基础。你会发现相机类比的一步一步的演练在第二模块中,语境事项,在标题为素材的日历时间与瞬时间。

Many people have shared with me that they find this analogy extremely valuable, so I included it in my Pluralsight course, Date and Time Fundamentals. You'll find a step-by-step walkthrough of the camera analogy in the second module, "Context Matters", in the clip titled "Calendar Time vs. Instantaneous Time".

这篇关于日期时间VS的DateTimeOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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