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

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

问题描述

当我运行这个特定值dt的代码时,当我调用ConvertTimeToUtc方法时会抛出异常。
我的本地机器timeZoneId是GMT标准时间

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);

例外情况是:

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 


推荐答案

是的,这是绝对正确的:1995年4月4日中央标准时间55am不存在,挂墙时钟由于夏令时转换而从上午2点凌晨3点跳过了这个例外。(这个例外情况似乎很清楚)(这里使用标准有点棘手,把它叫做中央时间 这将包括中央标准时间和中央夏令时间,但这是一个不同的事情,我希望自己的奥尔森标识符...)

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?

有些不幸的是,异常只是 ArgumentException - 在 Noda时间中,我们将有一个例外,这个确切的情况,所以更容易发现抓住。 (我们也会有类似IsAmbiguous和IsSkipped的东西,所以你可以检查而不捕获异常。)

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.)

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

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

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

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