DateTime 必须包含时区信息并考虑夏令时
我所处的情况是我的程序有 2 个与第三方系统的集成。集成包括 1) TCP/IP 上的 XML 协议和 2) WCF 中的 Web 服务。
在任何一种情况下,集成合作伙伴都需要我传递给他们的时间戳/日期时间来包含有关时区和夏令时的信息,以便他们可以显示正确的时间,无论其客户的时区/位置如何。
我不想更改当前协议,在任何情况下他们都期望 DateTime,所以我的问题是:是否可以将时区和夏令时信息作为 DateTime 的一部分传递?
现在我的时间戳看起来像这样:
2011-04-27T15:14:13.963
我希望它看起来像这样(我位于丹麦,所以我们使用 CEST)并且能够使用 DateTime 对象传输信息
2011-04-27T15:14:13.963 +01:00
但是我不知道应该如何完成这也不考虑夏令时因素
I'm placed in a situation where my program has 2 integrations towards 3rd party systems. The integrations consists of 1) XML protocol over TCP/IP and 2) Webservices made in WCF.
In either case the integration partner needs the TimeStamps / DateTimes I pass to them to contain information about the TimeZone and Daylight Savings, so they can display the correct time regardless of their customers TimeZone / Position.
I'd rather not change the current protocol, in either cases they expect DateTime, so my question is: Is it possible to pass the TimeZone and Daylight Savings information as a part of the DateTime?
Right now my timestamps looks like this:
2011-04-27T15:14:13.963
I would love to have it look something like this (I'm located in Denmark so we use CEST) and be able to transfer the information with a DateTime object
2011-04-27T15:14:13.963 +01:00
However I do not know how I should accomplish this nor take into account the Daylight Savings Factor
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要知道日期/时间和时区,则必须提出自己的封装:
DateTime
仅包含日期/时间和信息至于它是“本地”(在系统时区)还是 UTCDateTimeOffset
包含有关日期/时间及其与 UTC 的偏移量的信息,但这与其时区不同您可以结合不过,
TimeZoneInfo
和DateTimeOffset
在结构中。或者,如果您愿意使用测试版质量、API 仍可更改的软件,则可以使用 Noda Time ,我开始的一个项目,基本上是将 Joda Time API 移植到 .NET。
这是就进程内表示而言......至于传递信息,您需要找出集成合作伙伴使用的内容。例如,他们可能想要 UTC 时间和奥尔森时区名称...或者他们可能只想要偏移量。
如果您只需要知道创建时间戳时用户的本地时间,那么包含相对于 UTC 的“当前”偏移量可能就足够了,而不是包含完整的时区信息。这只是意味着你不知道 2 秒后的当地时间......
If you need to know both the date/time and the time zone, you'll have to come up with your own encapsulation:
DateTime
only contains the date/time and information as to whether it's "local" (in the system time zone) or UTCDateTimeOffset
contains information about the date/time and its offset to UTC, but that's not the same as its time zoneYou could combine
TimeZoneInfo
andDateTimeOffset
in a struct though.Alternatively, if you're happy to use beta-quality, API-could-still-change software, you could use Noda Time, a project I started to basically port the Joda Time API to .NET.
That's in terms of in-process representation... as for passing the information around, you need to find out what your integration partners use. For example, they might want the UTC time and an Olsen time zone name... or they may just want the offset.
Including the "current" offset from UTC may be enough, rather than including full time zone information, if you only need to know what the local time was for the user at the time the timestamp was created. It just means you don't know what the local time was 2 seconds later...
使用
DateTimeOffset< 而不是
DateTime
/code>,其中包含时区偏移信息。
至于夏令时 - 您需要使用 Olsen 数据库。
请参阅此相关问题。
Instead of
DateTime
useDateTimeOffset
, which contains timezone offset information.As for daylight savings time - you will need to use the Olsen database.
See this related question.
使用 UTC 时间,您可以坚持使用 DateTime。 UTC 就地转换为当地时间(或任何其他时间)。这也解决了夏令时的问题。使用 UTC 是最好的解决方案,我对此有一些经验。
小演示:
}
Use UTC time and you can stick with DateTime. UTC is converted to local (or any other time) in-place. Also this solves problem with Daylight Saving Time. Using UTC is the best solution, I have had some expirience with it.
Small demo:
}