3

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 offset and the country code and need to be able to select the correct time zone value.

John Farrell
  • 24,673
  • 10
  • 77
  • 110
alexm
  • 510
  • 1
  • 8
  • 19
  • As mentioned in my answer, country code + UTC offset isn't enough information to isolate one time zone value. – Matt Mitchell Apr 29 '11 at 03:09
  • Even if it may not identify it 100% at least it will limit to a result of 1-2 or so many. Which in most cases is plenty. In fact, you could prompt the user to select it from the list of values if filtered result is more than 1. but not having any country and locality identification for TimeZoneInfo object is a huge miss! – alexm Apr 29 '11 at 03:44
  • so given that, I'd load one of the DBs I linked in my question so you have a map of country code + UTC -> possible timezones and go from there. It's not the easiest, but shouldn't be too difficult if it's an important requirement. – Matt Mitchell Apr 29 '11 at 03:49

2 Answers2

6

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.

Community
  • 1
  • 1
Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185
  • 2
    Hmm downvoted, not sure why? Please leave a comment if this answer is inaccurate. – Matt Mitchell Apr 29 '11 at 03:07
  • 1
    Undone, sorry. I must've read the other person's answer. You are correct that there may be multiple time zone results for country and a particular offset. In my use-case, this logic is used only to guesstimate anonymous user's time to display my calendar event entries. Once they are registered, they must select their timezone so then I have no problem. – alexm Apr 29 '11 at 03:57
0

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

Jakob Gade
  • 12,319
  • 15
  • 70
  • 118
  • 3
    this does not answer it as well. The original question has the following parameters available: UTC offset in a -xx:xx format and a country code and city. This information comes from geo-ip service. they are not giving .NET 4 formatted names to be able to do such comparisson. – alexm Apr 29 '11 at 02:28
  • @alexm - if this doesn't answer your question, why accept the answer? – Matt Mitchell Apr 29 '11 at 02:45
  • @Graphain sorry, still familiarizing myself with all these buttons -) undone. your answer is the most appropriate, thank you – alexm Apr 29 '11 at 03:55
  • @alexm, it's dissapointing that mine is the "answer" here. If I get bored this weekend I might put something on Codeplex to sort this out. – Matt Mitchell Apr 29 '11 at 04:22