ASP.NET 3.5:难以获得时区的当前偏移时区、ASP、NET

2023-09-04 00:22:11 作者:让我盲目

不知道我在做什么错在这里。我试图让当前#UTC时间为澳大利亚东部偏移。在code我使用的是回来与GMT + 10小时,而应该是11,因为他们是在DST的时刻。

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

这似乎输出基地偏移,忽视DST。我如何获得的实际电流偏移这需要DST考虑?

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

更新1响应以jlew的建议。我试着让IsDaylightSavingTime但是这似乎给不一致的结果。我知道,东澳大利亚和新西兰这两个都是目前在DST。

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.

但是,下面的返回FALSE(不正确):

However, the following returns FALSE (incorrect):

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

和下返回TRUE(正确):

And the following returns TRUE (correct):

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

更新2 在看到这个帖子 ,我查IsAmbiguousTime,但它是假的两种情况。因此,这不是暧昧,但E澳大利亚区不使用夏令时,是应该的。

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.

更新3 在看到this帖子,我安装了从微软2011年12月累积DST更新,但没有喜悦。大肠杆菌澳大利亚区仍是不正确。

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.

我也尝试了其他2个不同的服务器上运行的code,具有相同的结果。我的是.NET v3.5版本,其他都是V4。

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

更新4 确定,可能已经解决了。翻翻系统时区ID列表再次,我注意到的另一个的区域为东澳元名。这就是所谓的澳大利亚东部标准时间。使用该区域,它产生正确的,DST友好的结果。使用E.澳大利亚标准时间没有。

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.

在搜索周围,似乎做一个变态的那种感觉,因为不同城市,同一国家的同一时区,可能有不同的DST规则。谁要让他们做到这一点?

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??

这不是有一个世界地图的问题,其中区域ID涉及到哪些城市。当然,这不是太难为MS提供。我们可以使用DisplayName属性显示,用户可以从列表中选择,但如果应用程序需要从一个城市到另一个转换的时间,它需要以某种方式知道哪个ID这些城市应该使用。

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.

如果MS包括城市到了zoneid的查找,至少在主要城市,它会采取很多的麻烦出与时区的工作。

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.

推荐答案

我不知道是否有一个自动的方式,但你可以使用的 TimeZone.IsDaylightSavingsTime 和的 DaylightTime.Delta 计算必要的调整。

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