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

问题描述

|| 我处于一种情况,即我的程序与3rd Party系统集成了2个集成。集成包括1)TCP / IP上的XML协议和2)WCF中进行的Web服务。 无论哪种情况,集成合作伙伴都需要我传递给他们的TimeStamps / DateTimes来包含有关TimeZone和Daylight Savings的信息,因此他们可以显示正确的时间,而不管其客户的TimeZone / Position。 我不想更改当前协议,无论哪种情况,他们都希望使用DateTime,所以我的问题是:是否可以将TimeZone和Daylight Savings信息作为DateTime的一部分传递? 现在,我的时间戳如下所示:
2011-04-27T15:14:13.963
我希望它看起来像这样(我位于丹麦,所以我们使用CEST),并且能够使用DateTime对象传输信息
 2011-04-27T15:14:13.963 +01:00
但是我不知道该怎么做,也不知道夏时制因素     

解决方法

使用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();
      }
  }
}     ,如果您需要知道日期/时间和时区,则必须提出自己的封装: “ 3”仅包含日期/时间以及有关它是本地时间(在系统时区)还是UTC的信息
DateTimeOffset
包含有关日期/时间及其相对于UTC的偏移量的信息,但与时区不同 您可以在结构中组合
TimeZoneInfo
和ѭ4combine。 另外,如果您乐于使用Beta版质量的API仍可以更改软件,则可以使用Noda Time,这是我开始将Joda Time API移植到.NET的一个项目。 这就是在过程中的表示方式...为了传递信息,您需要找出您的集成合作伙伴使用了什么。例如,他们可能想要UTC时间和Olsen时区名称...,或者他们只想要偏移量。 如果只需要知道在创建时间戳时用户的本地时间是多少,那么从UTC偏移\“ current \”就足够了,而不是包含完整的时区信息。这只是意味着您不知道2秒后的当地时间是什么...     ,代替
DateTime
,使用
DateTimeOffset
,它包含时区偏移信息。 至于夏令时-您将需要使用Olsen数据库。 请参阅此相关问题。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...