4

The conversion between Olson and Windows time Id's has come up multiple times on SO; with many people suggesting Jon Skeet's Noda Time for this task.

Whilst the Google Code page states that there is a function to convert between the two, I can't find details anywhere of how to do this.

Can anyone point me in the right direction?

Jonathan
  • 13,947
  • 17
  • 94
  • 123
  • Haven't seen one in the code. You can create `DateTimeZone` objects from either Olson ids or Windows TimeZoneInfo objects but can't convert AFAIK. – Ian Mercer Apr 19 '12 at 02:55
  • 1
    This can be done now with Noda Time 1.1.0. [I have posted conversion functions here.](http://stackoverflow.com/q/17348807/634824) – Matt Johnson-Pint Jun 27 '13 at 17:27

2 Answers2

1

No, unfortunately we don't have a mapping that way round (Windows to Olson) at the moment.

All of the "provider" interface is slightly up in the air right now:

  • Even within the current code, there are API changes we want to make, partly to make code using it more testable
  • The Unicode CLDR mappings have changed between 1.x and 2.x, so now there are multiple mappings each way, potentially (IIRC).

In other words: sorry, we don't support this at the moment, but we'll bear it in mind when trying to finalize the 1.0 API.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

TzdbTimeZoneProvider support conversion from Windows to Olson, but BclTimeZoneProvider (provider for windows time zones) does not have correct implementation of MapTimeZoneId method and interface seems wrong...

Here is implementation at TzdbTimeZoneProvider:

 public string MapTimeZoneId(TimeZoneInfo zone)
 {
   string str;
   this.windowsIdMap.TryGetValue(zone.Id, out str); 
   return str;
 }

Note: the windowsIdMap is a dictionary

Here is implementation at BclTimeZoneProvider:

public string MapTimeZoneId(TimeZoneInfo timeZone)
{
  return timeZone.Id;
}

Note: It just return id of windows time zone.

It seems more correct interface for this method will be:

 string MapTimeZoneId(string providerZoneId);

Then both implementations can be done correctly. I guess you can put this question at Noda Time google groups.

For now you can look into TzdbTimeZoneProvider to find way how to map from Olson to Windows tz (simple iteration through the windowsIdMap values).

Andrew Orsich
  • 52,935
  • 16
  • 139
  • 134
  • The implementation is fine - the purpose of `MapTimeZoneId` is to map *from* Windows *to* the provider-specific ID. That's why it takes a `TimeZoneInfo` instead of a string. – Jon Skeet Jun 20 '12 at 07:55