异常调用时TimeZoneInfo.ConvertTimeToUtc某些DateTime值 [英] Exception calling when TimeZoneInfo.ConvertTimeToUtc for certain DateTime values

查看:218
本文介绍了异常调用时TimeZoneInfo.ConvertTimeToUtc某些DateTime值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行DT的这个特定值的代码,一个例外是当我打电话ConvertTimeToUtc方法抛出。
我的本地机器timeZoneId是GMT标准时间

  VAR TZI = TimeZoneInfo.FindSystemTimeZoneById(中原标准时间 ); 
变种dt的=新的日期时间(1995年,4,2,2,55,0);
变种T = TimeZoneInfo.ConvertTimeToUtc(DT,TZI);



唯一的例外是:



  System.ArgumentException了未处理
消息=所提供的DateTime代表无效的时间。例如,当调节时钟向前,即跳过期间的任何时间invalid.\ r\\\
Parameter


解决方案

是的,这是绝对正确的。2 :55am没有在中欧标准时间存在于1995年4月4日,作为挂钟跳过从凌晨2点到凌晨3点,由于夏令时转换中的例外似乎这个比较清楚(使用的标准是有点棘手这里;它会更有意义称之为中部时间,这将包括中部标准时间和中部夏令时间,但是这是一个不同的问题。哎呀,我宁愿奥尔森标识符自己...)



在其他时候,当地时间可能是不明确的 - 如果时钟的的一小时(或更多!),则可能会出现两次当地时间



现在的问题是。 :?你希望你的代码在这种情况下的行为怎么办



这有点不幸的是,例外是刚的ArgumentException - 在野田佳彦时间我们将有这个确切的情况例外,所以它更容易发现和捕捉。 (我们也将有类似IsAmbiguous和IsSkipped,所以你可以不捕获异常检查。)



但基本的信息是,这是不是在一个bug BCL - 这是故意的。


When I run the code for this specific value of dt, an exception is thrown when I call the ConvertTimeToUtc Method. My local Machine timeZoneId is "GMT Standard Time"

var tzi = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var dt = new DateTime(1995, 4, 2, 2, 55, 0);
var t = TimeZoneInfo.ConvertTimeToUtc(dt, tzi);

The exception is:

System.ArgumentException was unhandled
Message="The supplied DateTime represents an invalid time.  For example, when the clock is adjusted forward, any time in the period that is skipped is invalid.\r\nParameter 

解决方案

Yes, that's absolutely right. 2:55am didn't exist in Central Standard Time on April 4th 1995, as the wall clock skipped from 2am to 3am due to daylight saving transitions. The exception seems reasonably clear about this. (The use of "standard" is somewhat tricky here; it would make more sense to call it "Central Time" which would include "Central Standard Time" and "Central Daylight Time" but that's a different matter. Heck, I'd prefer Olson identifiers myself...)

At other times, a local time may be ambiguous - if the clock goes back an hour (or more!) then a local time may occur twice.

The question is: how do you want your code to behave in this situation?

It's somewhat unfortunate that the exception is just ArgumentException - in Noda Time we're going to have an exception for this exact case, so that it's easier to spot and catch. (We'll also have something like IsAmbiguous and IsSkipped so you can check without catching an exception.)

But the basic message is that this isn't a bug in the BCL - it's deliberate.

这篇关于异常调用时TimeZoneInfo.ConvertTimeToUtc某些DateTime值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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