TimeZoneInfo.ConvertTime from PST to UTC to AEST -

2019-03-01 09:45发布

问题:

I convert a string that represents a time in Pacific Time Zone that I am using to create a DateTime object:

var pacificDateTime = new DateTime(2016, 11, 16, 15, 0, 0) // 11/16/2016 3:00:00 PM

Using that, I create a DateTimeOffset because ultimately it becomes a bit easier to work with.

var pacificTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dateTimeNoKind = new DateTime(pacificDateTime.Ticks, DateTimeKind.Unspecified)
var DateTimeOffsetValue = TimeZoneInfo.ConverTimeToUtc(dateTimeNoKind, pacificTimeZoneInfo) // 11/16/2016 11:00:00 PM

So far so good. The difference between UTC and Pacific is that UTC is ahead by 8 hours (the given time is within daylight savings).

Then I want to convert from UTC to AEST—but this is where the problem appears:

var australianEasternTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");
TimeZoneInfo.ConvertTime(DateTimeOffsetValue, australianEasternTimeZoneInfo) // 11/17/2016 10:00:00 AM

AEST is ahead of UTC by 10 hours. I had expected the value to be 11/17/2016 09:00:00 AM but instead I am getting an extra hour added to my result.

As a test, when I convert to PST or GMT or EST from the UTC time, they appear to convert back correctly.

I feel like I am missing something obvious or overlooking something simple?

回答1:

From Wikipedia's Time in Australia article:

Australia has two eastern time zones. In the northeast, UTC+10 applies all year. In the southeast, UTC+10 applies during standard time, and UTC+11 applies during daylight time (aka summer time).

The northeast region (Queensland) uses the IANA time zone identifier "Australia/Brisbane", while the southeast region (New South Wales) uses "Australia/Sydney". These correspond to the Windows time zone identifiers: "E. Australia Standard Time" and "AUS Eastern Standard Time" respectively.

  • If you are converting for Queensland, use "E. Australia Standard Time".

  • If you are converting for New South Wales, use "AUS Eastern Standard Time".

As to the confusing nature of these identifiers, see the section about Windows time zones in the timezone tag wiki.

If you want to use the standard IANA identifiers instead, use Noda Time.