/// <summary> /// Returns the IDs of all system time zones. /// </summary> /// <returns>The IDs available from this source.</returns> public IEnumerable <string> GetIds() { // Always include the local time zone, since Mono may not include it in the list of system time zones, even // though it allows the Id to be passed to FindSystemTimeZoneById(). // See https://github.com/nodatime/nodatime/issues/235. return(TimeZoneInfoInterceptor.GetSystemTimeZones() .Select(zone => zone.Id) .Union(GetTimeZoneInfoLocalIdOrEmpty())); }
/// <summary> /// Creates a new instance of <see cref="BclDateTimeZone" /> from the <see cref="TimeZoneInfo"/> with the given /// ID. The ID must be a known system time zone ID. /// </summary> /// <param name="id">The ID of the system time zone to convert</param> /// <exception cref="ArgumentException">The given zone doesn't exist.</exception> /// <returns>The Noda Time representation of the given BCL time zone</returns> public BclDateTimeZone ForId(string id) { try { TimeZoneInfo zone = TimeZoneInfoInterceptor.FindSystemTimeZoneById(id); return(BclDateTimeZone.FromTimeZoneInfo(zone)); } catch (TimeZoneNotFoundException) { throw new ArgumentException(id + " is not a system time zone ID", nameof(id)); } }
/// <summary> /// Returns an enumerable containing a singleton element of the Id of the local time zone /// (<c>TimeZoneInfo.Local.Id</c>), unless the local time zone is not available, or not a system time zone, in /// which case returns an empty enumerable. /// </summary> private static IEnumerable <string> GetTimeZoneInfoLocalIdOrEmpty() { // This complexity is entirely to handle Mono, which fails quite badly at this in some cases. try { // May throw TimeZoneNotFoundException, particularly on Mono/Windows. // See https://bugzilla.xamarin.com/show_bug.cgi?id=11817 var local = TimeZoneInfoInterceptor.Local; if (local != null) // https://github.com/nodatime/nodatime/issues/235#issuecomment-80932079 { // Make sure we can look it up again, as there are legitimate cases where the local time zone is not // a system time zone. If not, this also throws TimeZoneNotFoundException. TimeZoneInfoInterceptor.FindSystemTimeZoneById(local.Id); return(new[] { local.Id }); } } catch (TimeZoneNotFoundException) { } return(Enumerable.Empty <string>()); }