转换日期时间为UTC时,这是什么关于调整规则? [英] What is this about adjustment rules when converting DateTime to UTC?

查看:231
本文介绍了转换日期时间为UTC时,这是什么关于调整规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为 <$ C $的MSDN页面C> TimeZoneInfo.ConvertTimeToUtc() ,有这样的信息框:




如果当前计算机的本地时区包含多个调整规则,这种过载ConvertTimeToUtc方法可以返回从TimeZone.ToUniversalTime和DateTime.ToUniversalTime方法不同的结果。 TimeZone.ToUniversalTime总是应用当前的调整规则的时区转换,日期时间是否位于其日期范围内。和.NET Framework 3.5的执行时,DateTime.ToUniversalTime也适用于目前的调整规则的时区转换,日期时间是否位于其日期范围内。




我不知道我理解这意味着什么。这些是什么调整规则,以及如何将结果之间的差异ConvertTimeToUtc() TimeZone.ToUniversalTime() <? / p>

解决方案

下面是一个例子。我在写这篇文章的时候计算机设置为美国太平洋时区,今天是3月2日,2015年它是目前太平洋标准时间(PST或),这是比UTC 8个小时。

  DateTime的DT =新日期时间(2006年4,1,0,0,0); 
的TimeZoneInfo TZI = TimeZoneInfo.Local;
的DateTime UTC = TimeZoneInfo.ConvertTimeToUtc(DT,TZI);

在上面的代码中,我转换另一个值2006午夜4月1日从我的时区世界标准时间。在该时间特定点,太平洋标准时间(或PST)为有效。上面的代码使用的TimeZoneInfo ,而且是要做到这一点的正确方法。 。输出是2006年4月1日上午8点00分UTC



现在看一下这个代码:

  DateTime的DT =新日期时间(2006年4,1,0,0,0); 
时区TZ = TimeZone.CurrentTimeZone;
的DateTime UTC = tz.ToUniversalTime(DT);



这样看来几乎做了同样的事情。但它返回7:00 AM UTC的一个不正确的值。



这是因为美国的改变它的夏令时规则有效的2007年。本例中​​的日期,DST在由到位当时的规则影响不是,但它会生效如果的电流的规则已经到位即可。



很简单,的TimeZoneInfo 对象是意识到这种变化,但时区对象不是。它错误地假定当前的规则总是有效的。



同样的事情将在 TimeZone的其他方法发生一流的,这就是为什么的MSDN参考说:




重要提示结果
尽可能使用的TimeZoneInfo 类,而不是时区类。




此外,时区类已经的删除的从新 。.NET CoreCLR 项目



关于调整规则 - 在MSDN句话是专门指的 TimeZoneInfo.AdjustmentRule 类,它是用来追踪变化,可以一个时区中的定期或不定期地对发生时区偏移。夏令时就是这样的类型可能发生改变,但也有其他人。



您可能希望了解的夏令时时区的理解这些变化背后的机制。



您也可以尝试我Pluralsight当然的日期和时间基础,这也解释了在更大的详细介绍这些概念



另请参阅:的是什么 DateTime.ToUniversalTime TimeZoneInfo.ConvertTimeToUtc


In the MSDN page for TimeZoneInfo.ConvertTimeToUtc(), there is this info box:

If the current computer's local time zone includes multiple adjustment rules, this overload of the ConvertTimeToUtc method can return results that differ from the TimeZone.ToUniversalTime and DateTime.ToUniversalTime methods. TimeZone.ToUniversalTime always applies the current adjustment rule to time zone conversion, whether or not dateTime lies within its date range. And when executing on .NET Framework 3.5, DateTime.ToUniversalTime also applies the current adjustment rule to time zone conversion, whether or not dateTime lies within its date range.

I'm not sure I understand what this means. What are these adjustment rules, and how will the results differ between ConvertTimeToUtc() and TimeZone.ToUniversalTime()?

解决方案

Here's an example. My computer at the time of writing this post is set to the US Pacific time zone, and today is March 2, 2015. It is currently Pacific Standard Time (or PST), which is 8 hours behind UTC.

DateTime dt = new DateTime(2006, 4, 1, 0, 0, 0);
TimeZoneInfo tzi = TimeZoneInfo.Local;
DateTime utc = TimeZoneInfo.ConvertTimeToUtc(dt, tzi);

In the above code, I am converting another value, midnight April 1st 2006, from my time zone to UTC. At that particular point in time, Pacific Standard Time (or PST) was in effect. The above code uses TimeZoneInfo, and is the correct way to do this. The output is April 1st 2006 at 8:00 AM UTC.

Now look at this code:

DateTime dt = new DateTime(2006, 4, 1, 0, 0, 0);
TimeZone tz = TimeZone.CurrentTimeZone;
DateTime utc = tz.ToUniversalTime(dt);

It would appear to do almost the same thing. But it returns an incorrect value of 7:00 AM UTC.

This occurs because the United States changed it's daylight saving time rules effective 2007. On the date in the example, DST was not in effect by the rule that was in place at that time, but it would be in effect if the current rule was in place then.

Quite simply, the TimeZoneInfo object is aware of this change, but the TimeZone object is not. It mistakenly assumes the current rule was always in effect.

The same thing will occur with other methods on the TimeZone class, which is why the MSDN reference says:

Important
Whenever possible, use the TimeZoneInfo class instead of the TimeZone class.

Also, the TimeZone class has been removed from the new .Net CoreCLR project.

Regarding "adjustment rules" - the MSDN remark is referring specifically to the TimeZoneInfo.AdjustmentRule class, which is used to track changes to time zone offsets that can occur on a regular or irregular basis within a time zone. Daylight Saving Time is one such type of change that can occur, but there are others as well.

You may wish to read the StackOverflow wikis about daylight saving time and time zones to understand the mechanics behind these changes.

You might also try my Pluralsight course, Date and Time Fundamentals, which explains these concepts in even greater detail.

See also: What is the difference between DateTime.ToUniversalTime and TimeZoneInfo.ConvertTimeToUtc?

这篇关于转换日期时间为UTC时,这是什么关于调整规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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