DateTime 必须包含时区信息并考虑夏令时

发布于 2024-11-10 05:26:48 字数 463 浏览 6 评论 0原文

我所处的情况是我的程序有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

却一份温柔 2024-11-17 05:26:48

如果您需要知道日期/时间时区,则必须提出自己的封装:

  • DateTime 仅包含日期/时间和信息至于它是“本地”(在系统时区)还是 UTC
  • DateTimeOffset 包含有关日期/时间及其与 UTC 的偏移量的信息,但这与其时区不同

您可以结合不过,TimeZoneInfoDateTimeOffset 在结构中。

或者,如果您愿意使用测试版质量、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 UTC
  • DateTimeOffset contains information about the date/time and its offset to UTC, but that's not the same as its time zone

You could combine TimeZoneInfo and DateTimeOffset 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...

暗恋未遂 2024-11-17 05:26:48

使用 DateTimeOffset< 而不是 DateTime /code>,其中包含时区偏移信息。

至于夏令时 - 您需要使用 Olsen 数据库

请参阅相关问题。

Instead of DateTime use DateTimeOffset, which contains timezone offset information.

As for daylight savings time - you will need to use the Olsen database.

See this related question.

豆芽 2024-11-17 05:26:48

使用 UTC 时间,您可以坚持使用 DateTime。 UTC 就地转换为当地时间(或任何其他时间)。这也解决了夏令时的问题。使用 UTC 是最好的解决方案,我对此有一些经验。

小演示:

namespace TimeZoneTest
{
  using System;
  using System.Globalization;

  class Program
  {
      static void Main(string[] args)
      {
          // get local time
          DateTime localTime = DateTime.Now;
          Console.WriteLine(string.Format(
              CultureInfo.CurrentCulture, 
              "localTime = {0}, localTime.Kind = {1}", 
              localTime, 
              localTime.Kind));

          // get local time zone, or use TimeZoneInfo to get any time zone you want
          TimeZone ltz = TimeZone.CurrentTimeZone;
          Console.WriteLine(string.Format("local time zone = {0}", ltz.StandardName));

          // convert local time to UTC
          DateTime utcTime = ltz.ToUniversalTime(localTime);
          Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
              "utcTime = {0}, utcTime.Kind = {1}",
              utcTime,
              utcTime.Kind));

          // transfer date via service, as ISO time string
          string isoUtc = utcTime.ToString("o");
          Console.WriteLine("...");
          Console.WriteLine(string.Format("transfer: isoUtc = {0}", isoUtc));
          Console.WriteLine("...");

          // now on the other side
          DateTime utcTimeRecieved = DateTime.ParseExact(
              isoUtc, 
              "o", 
              CultureInfo.InvariantCulture, 
              DateTimeStyles.RoundtripKind);
          Console.WriteLine(string.Format(CultureInfo.CurrentCulture, 
              "utcTimeRecieved = {0}, utcTimeRecieved.Kind = {1}", 
              utcTimeRecieved, 
              utcTimeRecieved.Kind));

          // client time zone, or use TimeZoneInfo to get any time zone you want
          TimeZone ctz = TimeZone.CurrentTimeZone;
          Console.WriteLine(string.Format("client time zone = {0}", ctz.StandardName));

          // get local time from utc
          DateTime clientLocal = ctz.ToLocalTime(utcTimeRecieved);
          Console.WriteLine(string.Format(
              CultureInfo.CurrentCulture,
              "clientLocal = {0}, clientLocal.Kind = {1}",
              clientLocal,
              clientLocal.Kind));

          Console.WriteLine("\nPress any key to exit..");
          Console.ReadKey();
      }
  }

}

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:

namespace TimeZoneTest
{
  using System;
  using System.Globalization;

  class Program
  {
      static void Main(string[] args)
      {
          // get local time
          DateTime localTime = DateTime.Now;
          Console.WriteLine(string.Format(
              CultureInfo.CurrentCulture, 
              "localTime = {0}, localTime.Kind = {1}", 
              localTime, 
              localTime.Kind));

          // get local time zone, or use TimeZoneInfo to get any time zone you want
          TimeZone ltz = TimeZone.CurrentTimeZone;
          Console.WriteLine(string.Format("local time zone = {0}", ltz.StandardName));

          // convert local time to UTC
          DateTime utcTime = ltz.ToUniversalTime(localTime);
          Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
              "utcTime = {0}, utcTime.Kind = {1}",
              utcTime,
              utcTime.Kind));

          // transfer date via service, as ISO time string
          string isoUtc = utcTime.ToString("o");
          Console.WriteLine("...");
          Console.WriteLine(string.Format("transfer: isoUtc = {0}", isoUtc));
          Console.WriteLine("...");

          // now on the other side
          DateTime utcTimeRecieved = DateTime.ParseExact(
              isoUtc, 
              "o", 
              CultureInfo.InvariantCulture, 
              DateTimeStyles.RoundtripKind);
          Console.WriteLine(string.Format(CultureInfo.CurrentCulture, 
              "utcTimeRecieved = {0}, utcTimeRecieved.Kind = {1}", 
              utcTimeRecieved, 
              utcTimeRecieved.Kind));

          // client time zone, or use TimeZoneInfo to get any time zone you want
          TimeZone ctz = TimeZone.CurrentTimeZone;
          Console.WriteLine(string.Format("client time zone = {0}", ctz.StandardName));

          // get local time from utc
          DateTime clientLocal = ctz.ToLocalTime(utcTimeRecieved);
          Console.WriteLine(string.Format(
              CultureInfo.CurrentCulture,
              "clientLocal = {0}, clientLocal.Kind = {1}",
              clientLocal,
              clientLocal.Kind));

          Console.WriteLine("\nPress any key to exit..");
          Console.ReadKey();
      }
  }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文