Olson Time Zone Id to Windows Standard Format usin

2019-04-09 21:14发布

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?

2条回答
何必那么认真
2楼-- · 2019-04-09 21:36

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.

查看更多
贼婆χ
3楼-- · 2019-04-09 21:39

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

查看更多
登录 后发表回答