什么时候你喜欢DateTime Over DateTimeOffset [英] When Would You Prefer DateTime Over DateTimeOffset

查看:132
本文介绍了什么时候你喜欢DateTime Over DateTimeOffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前,我被介绍到新的 DateTimeOffset 类型,很高兴 DateTime 关于时区的缺陷终于得到了照顾。

A few months ago I was introduced to the new DateTimeOffset type and was glad DateTime's flaws with regard to time zones were finally taken care of.

然而,我不知道是否有任何使用这种新类型可能发生的开销或问题。

However, I was left wondering if there were any overhead or problems that could occur from using this new type.

我在一个多区域的Web应用程序上工作。有谁知道有什么可以摇摆我不要使用它所有的日期/时间工作?在这里有滥用的窗口吗?

I work on a multi-locale web application. Does anyone know of anything that could sway me from just using it for all my date/time work? Is there a window for abuse here?

参考: DateTimeOffset:Justin Van Patten的.NET 3.5中的新DateTime结构

推荐答案

有时你真的只想表示一个本地(时区不知道)的日期和时间,而不是一个 及时。说实话,代表一个时间更经常有用 - 例如唤醒我上午8点,无论时区 - 但日期和时间也可能有用。

Sometimes you really just want to represent a "local" (timezone unaware) date and time rather than an instant in time. To be honest it's more often useful to represent just a time - e.g. "wake me up at 8am, regardless of timezone" - but date and time could be useful too.

我同意,绝大多数情况下, DateTimeOffset 更适合。它确实令我奇怪,没有一个 DateTimeTimeZone 结构,它既有即时和时区,但是...一个偏移实际上并没有给你所有的信息你需要。 (例如,给定一个 DateTimeOffset ,你不知道24小时后的时间是什么,因为你不知道DST可能会在什么时候进入。)

I agree that for the vast majority of cases, DateTimeOffset is a better fit. It does strike me as odd that there isn't a DateTimeTimeZone struct which has both the instant and its timezone though... an offset doesn't actually give you all the information you need. (For instance, given a DateTimeOffset, you don't know what the time will be 24 hours later, because you don't know when DST might kick in.)

如果你想要那种结构,我有一个另一个答案中非常粗略的实现。我确定它可以很容易地改进:)

If you want that kind of structure, I have a very crude implementation in another answer. I'm sure it could be improved very easily :)

这篇关于什么时候你喜欢DateTime Over DateTimeOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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