TimeZoneInfo.GetSystemTimeZones() how to Select entries for specific country codes only?

前端 未结 2 521
北荒
北荒 2021-01-06 19:56

TimeZoneInfo.GetSystemTimeZones() gives you an Enumeration of all the time zones. The question is how to Select entries for specific country codes only. I know the UTC offse

相关标签:
2条回答
  • First, you would have discovered you can't simply use the UTC offset due to Daylight Savings variations (among perhaps other regional deviations). Perhaps if your provided UTC offset is the current offset, and you are regularly polling it, you could just use this?

    Regardless, I don't think country code + UTC is going to be easy for these reasons (which you no doubt already know):

    1. Timezones have a many-to-many relationship to countries

      While "Tokyo Standard Time" is most likely just Japan (or at least, "JP" and UTC+9:00 could concievably give you Tokyo Standard Time), what about "Central Asia Standard Time"? Sure, if you had a map of every country to every timezone you might be able to get somewhere here.

    2. The same UTC offset in the same country can yield different timezones

      For instance, in Australia, Queensland (Brisbane) does not observe Daylight Savings Time, whereas the other Eastern states do. This yields "E. Australia Standard Time" and "AUS Eastern Standard Time" as seperate timezones with the same country and UTC offset.

    3. There are often regional differences beyond UTC

      These are probably not a concern for you due to their low relevance (and I'm not sure how precise your usecase is), but there are anomalies such as towns on the borders of multiple timezones which compromise for an "averaged UTC" or similar. An example of this is "Central Western Standard Time" (http://basementgeographer.blogspot.com/2010/07/central-western-standard-time-time-zone.html).

    However, you may find this existing question (Country to Timezone mapping database) of interest, which yields this link (http://www.twinsun.com/tz/tz-link.htm). Assuming you have enough provided information you may be able to resolve a timezone using this data, or at least a best guess (or query user from possible options?) if that is still useful/possible.

    0 讨论(0)
  • 2021-01-06 20:15

    How about a bit of Linq magic?

    var zones = from tz in TimeZoneInfo.GetSystemTimeZones() where tz.Id = "xxx" select tz;
    

    Replace "xxx" with your desired timezone... :)

    0 讨论(0)
提交回复
热议问题