开发者

Does ToLocalTime work between timezones?

The documentation says that...

  • You can use the ToLocalTime method to restore a local date and time value that was co开发者_高级运维nverted to UTC by the ToUniversalTime or FromFileTimeUtc method.

and goes on to say (immediately)

  • However, if the original time represents an invalid time in the local time zone, it will not match the restored value.

Does the latter imply that it will only not work if the time is 'invalid' (whatever that means?)?


I think this demonstrates what it means for times to be invalid:

DateTime now = DateTime.Now;
for (DateTime dt = now; dt < now.AddYears(1); dt += TimeSpan.FromMinutes(30))
{
    DateTime dt2 = dt.ToUniversalTime().ToLocalTime(); // dt2 == dt ?
    if (dt2 != dt)
    {
        Console.WriteLine("Not equal: {0}, {1}", dt, dt2);
    }
}

Result on my computer (you might get different results):

Not equal: 27-03-2011 02:26:28, 27-03-2011 03:26:28
Not equal: 27-03-2011 02:56:28, 27-03-2011 03:56:28

The time "27-03-2011 02:26:28" is invalid because they do not exist due to the clock moving forward one hour, causing that time to be skipped.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜