简体   繁体   English

在使用TimeZoneInfo.ConvertTimeToUtc(DateTime,TimeZoneInfo)方法时,在夏令时结束日期将时间从本地时间转换为UTC的问题

[英]Issue on converting time from local to UTC at DST end date while using TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo) method

I have an application that converts local time to UTC and stores it in the database. 我有一个将本地时间转换为UTC并将其存储在数据库中的应用程序。 I encountered this problem while I was testing the conversion during a particular date - 1st November, 2015(the date on which the Daylight savings time ends(the clock goes back to 1.00AM on reaching 2.00AM)). 我在特定日期(2015年11月1日)(夏令时结束的日期(时钟回到2.00AM时回到1.00AM))测试转换时遇到了此问题。

My local system timezone is (UTC-08:00) Pacific Time (US & Canada) I converted the time 2015-10-31 01:49:00.000 to UTC, the output was 2015-10-31 08:49:00.000. 我的本地系统时区是(UTC-08:00)太平洋时间(美国和加拿大),我将时间2015-10-31 01:49:00.000转换为UTC,输出是2015-10-31 08:49:00.000。

but

when I tried to convert 2015-11-01 01:49:00.000 to UTC, the output was 2015-10-31 09:49:00.000. 当我尝试将2015-11-01 01:49:00.000转换为UTC时,输出为2015-10-31 09:49:00.000。

Isn't this wrong? 这不是错吗? why did the converted time increase by an hour on 1st November? 为什么转换后的时间在11月1日增加了一个小时?

This is my method, 这是我的方法

DateTime universalFormatDateTime = localDateTime.Value.GetUniversalFormatDateTime();
utcDateTime = TimeZoneInfo.ConvertTimeToUtc(universalFormatDateTime, _timeZoneInfo);

Isn't this wrong? 这不是错吗? why did the converted time increase by an hour on 1st November? 为什么转换后的时间在11月1日增加了一个小时?

Because that's when the clocks change, as you say. 正如您所说,那是时钟改变的时候。

The problem is that "2015-11-01 01:49:00.000" is ambiguous in Pacific Time - it occurs twice, once at 2015-11-01T08:49:00Z and once at 2015-11-01T09:49:00Z. 问题是“ 2015-11-01 01:49:00.000”在太平洋时间含糊不清-发生两次,一次发生在2015-11-01T08:49:00Z,一次发生在2015-11-01T09:49:00Z。

A DateTime can remember which of those you mean , but it depends on how you came up with the value. DateTime 可以记住您指的是哪个 ,但这取决于您如何得出该值。 If you've just parsed this from text somewhere, you basically don't have enough information - it doesn't specify a single instant in time. 如果您只是从某处的文本中进行了解析,则您基本上没有足够的信息-它没有指定一个即时的时间。

If you were to use my Noda Time library instead, then when converting from LocalDateTime to ZonedDateTime you'd be able to specify how you wanted ambiguity to be handled - so that may be an option for you... but it depends on where the value came from, and whether you know that it was always the second occurrence or always the first. 如果要改用我的Noda Time库,则在从LocalDateTime转换为ZonedDateTime您可以指定希望如何处理歧义性-因此这可能是您的选择...但这取决于价值来自何处,以及您是否知道这始终是第二次发生或始终是第一次发生。

If you still want to use TimeZoneInfo , you can use TimeZoneInfo.IsAmbiguousTime and TimeZoneInfo.IsInvalidTime to detect local times which occur twice or zero times due to time zone shifts, and then handle those appropriately in your app. 如果仍要使用TimeZoneInfo ,则可以使用TimeZoneInfo.IsAmbiguousTimeTimeZoneInfo.IsInvalidTime来检测由于时区偏移而发生两次或零次的本地时间,然后在您的应用程序中进行适当处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM