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

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.

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.

Isn't this wrong? why did the converted time increase by an hour on 1st November?

This is my method,

DateTime universalFormatDateTime = localDateTime.Value.GetUniversalFormatDateTime();
utcDateTime = TimeZoneInfo.ConvertTimeToUtc(universalFormatDateTime, _timeZoneInfo);
Jon Skeet
people
quotationmark

Isn't this wrong? why did the converted time increase by an hour on 1st November?

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.

A DateTime can remember which of those you mean, but it depends on how you came up with the value. 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.

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.

people

See more on this question at Stackoverflow