Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

.net - ASP.NET 3.5: Difficulty getting timezone's current offset

Not sure what I'm doing wrong here. I'm trying to get the current # hours UTC offset for Eastern Australia. The code I'm using is coming back with GMT+10 hours, whereas it should be 11, as they're in DST at the moment.

This is what I'm using to get it:

With TimeZoneInfo.FindSystemTimeZoneById("E. Australia Standard Time")
  Console.Write(.GetUtcOffset(Now.ToUniversalTime).Hours)
End With

It seems to output the "base" offset, disregarding DST. How do I get the actual current offset which takes DST into account?

Update 1 in response to jlew's suggestion. I've tried getting IsDaylightSavingTime but that seems to give an inconsistent result. I know that both East Australia and New Zealand are both currently in DST.

However, the following returns FALSE (incorrect):

TimeZoneInfo.FindSystemTimeZoneById("E. Australia Standard Time").IsDaylightSavingTime(Now.ToUniversalTime)

And the following returns TRUE (correct):

TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time").IsDaylightSavingTime(Now.ToUniversalTime)

Update 2 After seeing this post, I checked IsAmbiguousTime, however it is False in both cases. So it's not "ambiguous", yet "E. Australia" zone is not using DST when it should be.

Update 3 After seeing this post, I installed the Dec 2011 cumulative DST update from MS, but had no joy. The E. Australia zone is still incorrect.

I also tried running the code on 2 other different servers, with the same result. Mine is .net v3.5, the others are v4.

Update 4 Ok, may have solved it. Looking through the list of system timezone IDs again, I noticed another zone name for East Aus. It's called "AUS Eastern Standard Time". Using that zone, it does produce the correct, DST-friendly result. Using "E. Australia Standard Time" does not.

Searching around, it seems to make a perverted kind of sense, as different cities, in the same time zone of the same country, may have different DST rules. Who let them do that??

It's a problem not having a global map of which zone IDs relate to which cities. Surely that's not too difficult for MS to provide. One can use the DisplayName property to show a list that users can pick from, but if the application needs to convert time from one city to another, it needs to somehow know which IDs those cities should use.

If MS included a lookup of City-to-ZoneID, at least for major cities, it would take a lot of hassle out working with time zones.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I don't know if there is an automatic way, but you could use TimeZone.IsDaylightSavingsTime and DaylightTime.Delta to calculate the necessary adjustment.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...