我有一个全球网站,将 IANA 时区 id 传递到服务器,并使用 Noda Time 在 c# 5 web 应用程序中映射到 Windows 时区。
“Etc/UTC” 被传递到服务器,但是 Noda Time 无法将其映射到 Windows 时区。如何映射 IANA 时区 id?
public TimeZoneInfo GetTimeZoneByIanaId(string ianaTimeZoneId)
{
TzdbDateTimeZoneSource timeZoneSource = TzdbDateTimeZoneSource.Default;
IList<MapZone> zoneMaps = timeZoneSource.WindowsMapping.MapZones;
// resolve any link, since the CLDR doesn't necessarily use canonical IDs
IList<string> mapZoneIds = timeZoneSource.CanonicalIdMap.Where(map => map.Value.Equals(ianaTimeZoneId, StringComparison.OrdinalIgnoreCase)).Select(x => x.Key).ToList();
MapZone mapZone = zoneMaps.FirstOrDefault(zoneMap => zoneMap.TzdbIds.Any(mapZoneIds.Contains));
if (mapZone == null)
{
throw new TimeZoneNotFoundException("Unable to determine the clients timezone using the jsTimezoneDetect plugin");
}
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById(mapZone.WindowsId);
if (timeZone == null)
{
throw new TimeZoneNotFoundException("Unable to determine the clients timezone from NodaTime");
}
return timeZone;
}