/// <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 a time zone converted from the BCL representation of the system local time zone. /// </summary> /// <remarks> /// <para> /// This method is approximately equivalent to calling <see cref="IDateTimeZoneProvider.GetSystemDefault"/> with /// an implementation that wraps <see cref="BclDateTimeZoneSource"/> (e.g. /// <see cref="DateTimeZoneProviders.Bcl"/>), with the exception that it will succeed even if the current local /// time zone was not one of the set of system time zones captured when the source was created (which, while /// highly unlikely, might occur either because the local time zone is not a system time zone, or because the /// system time zones have themselves changed). /// </para> /// <para> /// This method will retain a reference to the returned <c>BclDateTimeZone</c>, and will attempt to return it if /// called repeatedly (assuming that the local time zone has not changed) rather than creating a new instance, /// though this behaviour is not guaranteed. /// </para> /// </remarks> /// <returns>A <see cref="BclDateTimeZone"/> wrapping the "local" (system) time zone as returned by /// <see cref="TimeZoneInfo.Local"/>.</returns> public static BclDateTimeZone ForSystemDefault() { TimeZoneInfo local = TimeZoneInfo.Local; BclDateTimeZone currentSystemDefault = systemDefault; // Cached copy is out of date - wrap a new one if (currentSystemDefault?.OriginalZone != local) { currentSystemDefault = FromTimeZoneInfo(local); systemDefault = currentSystemDefault; } // Always return our local variable; the variable may have changed again. return(currentSystemDefault); }
/// <summary> /// Returns a time zone converted from the BCL representation of the system local time zone. /// </summary> /// <remarks> /// <para> /// This method is approximately equivalent to calling <see cref="IDateTimeZoneProvider.GetSystemDefault"/> with /// an implementation that wraps <see cref="BclDateTimeZoneSource"/> (e.g. /// <see cref="DateTimeZoneProviders.Bcl"/>), with the exception that it will succeed even if the current local /// time zone was not one of the set of system time zones captured when the source was created (which, while /// highly unlikely, might occur either because the local time zone is not a system time zone, or because the /// system time zones have themselves changed). /// </para> /// <para> /// This method will retain a reference to the returned <c>BclDateTimeZone</c>, and will attempt to return it if /// called repeatedly (assuming that the local time zone has not changed) rather than creating a new instance, /// though this behaviour is not guaranteed. /// </para> /// </remarks> /// <exception cref="InvalidOperationException">The system does not provide a time zone.</exception> /// <returns>A <see cref="BclDateTimeZone"/> wrapping the "local" (system) time zone as returned by /// <see cref="TimeZoneInfo.Local"/>.</returns> [NotNull] public static BclDateTimeZone ForSystemDefault() { TimeZoneInfo?local = TimeZoneInfoInterceptor.Local; if (local is null) { throw new InvalidOperationException("No system default time zone is available"); } BclDateTimeZone currentSystemDefault = systemDefault; // Cached copy is out of date - wrap a new one if (currentSystemDefault?.OriginalZone != local) { currentSystemDefault = FromTimeZoneInfo(local); systemDefault = currentSystemDefault; } // Always return our local variable; the variable may have changed again. return(currentSystemDefault); }
/// <summary> /// Returns a time zone converted from the BCL representation of the system local time zone. /// </summary> /// <remarks> /// <para> /// This method is approximately equivalent to calling <see cref="IDateTimeZoneProvider.GetSystemDefault"/> with /// an implementation that wraps <see cref="BclDateTimeZoneSource"/> (e.g. /// <see cref="DateTimeZoneProviders.Bcl"/>), with the exception that it will succeed even if the current local /// time zone was not one of the set of system time zones captured when the source was created (which, while /// highly unlikely, might occur either because the local time zone is not a system time zone, or because the /// system time zones have themselves changed). /// </para> /// <para> /// This method will retain a reference to the returned <c>BclDateTimeZone</c>, and will attempt to return it if /// called repeatedly (assuming that the local time zone has not changed) rather than creating a new instance, /// though this behaviour is not guaranteed. /// </para> /// </remarks> /// <exception cref="InvalidOperationException">The system does not provide a time zone.</exception> /// <returns>A <see cref="BclDateTimeZone"/> wrapping the "local" (system) time zone as returned by /// <see cref="TimeZoneInfo.Local"/>.</returns> public static BclDateTimeZone ForSystemDefault() { TimeZoneInfo?local = TimeZoneInfoInterceptor.Local; if (local is null) { throw new InvalidOperationException("No system default time zone is available"); } BclDateTimeZone currentSystemDefault = systemDefault; // Cached copy is out of date - wrap a new one. // If currentSystemDefault is null, we always enter this block (as local isn't null). if (currentSystemDefault?.OriginalZone != local) { currentSystemDefault = FromTimeZoneInfo(local); systemDefault = currentSystemDefault; } // Always return our local variable; the field may have changed again. // The ! is because the compiler doesn't recognize the logic around // "always fetch if we currentSystemDefault is null". return(currentSystemDefault !); }
/// <summary> /// Returns a time zone converted from the BCL representation of the system local time zone. /// </summary> /// <remarks> /// <para> /// This method is approximately equivalent to calling <see cref="IDateTimeZoneProvider.GetSystemDefault"/> with /// an implementation that wraps <see cref="BclDateTimeZoneSource"/> (e.g. /// <see cref="DateTimeZoneProviders.Bcl"/>), with the exception that it will succeed even if the current local /// time zone was not one of the set of system time zones captured when the source was created (which, while /// highly unlikely, might occur either because the local time zone is not a system time zone, or because the /// system time zones have themselves changed). /// </para> /// <para> /// This method will retain a reference to the returned <c>BclDateTimeZone</c>, and will attempt to return it if /// called repeatedly (assuming that the local time zone has not changed) rather than creating a new instance, /// though this behaviour is not guaranteed. /// </para> /// </remarks> /// <returns>A <see cref="BclDateTimeZone"/> wrapping the "local" (system) time zone as returned by /// <see cref="TimeZoneInfo.Local"/>.</returns> public static BclDateTimeZone ForSystemDefault() { TimeZoneInfo local = TimeZoneInfo.Local; BclDateTimeZone currentSystemDefault = systemDefault; // Cached copy is out of date - wrap a new one if (currentSystemDefault?.OriginalZone != local) { currentSystemDefault = FromTimeZoneInfo(local); systemDefault = currentSystemDefault; } // Always return our local variable; the variable may have changed again. return currentSystemDefault; }