|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | + |
| 3 | +namespace BlazorLocalTime; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Provides an interface for a local time service. |
| 7 | +/// </summary> |
| 8 | +public interface ILocalTimeService |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Browser's time zone information. if you want to override it, set <see cref="OverrideTimeZoneInfo"/>. |
| 12 | + /// </summary> |
| 13 | + TimeZoneInfo? TimeZoneInfo { get; } |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Browser's original time zone information (read-only). |
| 17 | + /// </summary> |
| 18 | + TimeZoneInfo? BrowserTimeZoneInfo { get; } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// User-specified override time zone information. If you want to reset it, set this property to null. |
| 22 | + /// </summary> |
| 23 | + TimeZoneInfo? OverrideTimeZoneInfo { get; set; } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Gets the current browser's local time as a <see cref="DateTimeOffset"/>. |
| 27 | + /// </summary> |
| 28 | + DateTimeOffset Now { get; } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// On local time zone changed event with detailed timezone information. |
| 32 | + /// </summary> |
| 33 | + event EventHandler<TimeZoneChangedEventArgs> LocalTimeZoneChanged; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Is the local time zone set? |
| 37 | + /// </summary> |
| 38 | + [MemberNotNullWhen(true, nameof(TimeZoneInfo))] |
| 39 | + public bool IsTimeZoneInfoAvailable => TimeZoneInfo != null; |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Is the browser's time zone information successfully loaded? |
| 43 | + /// This property is set to null until the browser's time zone information is loaded. |
| 44 | + /// If the browser's time zone information is successfully loaded, it will be set to true. |
| 45 | + /// </summary> |
| 46 | + internal bool? IsSuccessLoadBrowserTimeZone { get; set; } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Sets the browser's time zone information. |
| 50 | + /// this method is only for internal use. |
| 51 | + /// </summary> |
| 52 | + internal void SetBrowserTimeZoneInfo(TimeZoneInfo? timeZoneInfo); |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Converts the specified UTC <see cref="DateTime"/> to local time. |
| 56 | + /// </summary> |
| 57 | + /// <param name="utcDateTime">The UTC date and time to convert.</param> |
| 58 | + /// <returns>The local <see cref="DateTime"/>.</returns> |
| 59 | + public DateTime ToLocalTime(DateTime utcDateTime) |
| 60 | + { |
| 61 | + return TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, GetBrowserTimeZone()); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Converts the <see cref="DateTimeOffset"/> to local time as a <see cref="DateTime"/>. |
| 66 | + /// </summary> |
| 67 | + /// <param name="dateTimeOffset"> The UTC date and time offset to convert.</param> |
| 68 | + /// <returns>The local <see cref="DateTime"/>.</returns> |
| 69 | + public DateTime ToLocalTime(DateTimeOffset dateTimeOffset) |
| 70 | + { |
| 71 | + return ToLocalTime(dateTimeOffset.UtcDateTime); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Converts the specified UTC <see cref="DateTime"/> to a local <see cref="DateTimeOffset"/>. |
| 76 | + /// </summary> |
| 77 | + /// <param name="utcDateTime">The UTC date and time to convert.</param> |
| 78 | + /// <returns>The local <see cref="DateTimeOffset"/>.</returns> |
| 79 | + public DateTimeOffset ToLocalTimeOffset(DateTime utcDateTime) |
| 80 | + { |
| 81 | + return new(ToLocalTime(utcDateTime), GetBrowserTimeZone().GetUtcOffset(utcDateTime)); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Converts the <see cref="DateTimeOffset"/> to a local <see cref="DateTimeOffset"/>. |
| 86 | + /// </summary> |
| 87 | + /// <param name="dateTimeOffset">The UTC date and time to convert.</param> |
| 88 | + /// <returns>The local <see cref="DateTimeOffset"/>.</returns> |
| 89 | + public DateTimeOffset ToLocalTimeOffset(DateTimeOffset dateTimeOffset) |
| 90 | + { |
| 91 | + return new(ToLocalTime(dateTimeOffset), GetBrowserTimeZone().GetUtcOffset(dateTimeOffset)); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Gets the browser's time zone information. |
| 96 | + /// </summary> |
| 97 | + /// <returns>The <see cref="TimeZoneInfo"/> representing the browser's time zone.</returns> |
| 98 | + public TimeZoneInfo GetBrowserTimeZone() |
| 99 | + { |
| 100 | + if (!IsTimeZoneInfoAvailable) |
| 101 | + { |
| 102 | + throw new InvalidOperationException( |
| 103 | + """ |
| 104 | + Failed to obtain the browser's time zone information. |
| 105 | + Possible causes: |
| 106 | + 1) The `<BlazorLocalTimeProvider />` component has not been added. |
| 107 | + In this case, please add `<BlazorLocalTimeProvider />` to a root component such as `Routes.razor`. |
| 108 | + 2) You are trying to use `ILocalTimeService` in `OnInitialized(Async)`. |
| 109 | + In this case, you need to subscribe to the `ILocalTimeService.OnLocalTimeZoneChanged` event |
| 110 | + and perform processing after the time zone information has been set. |
| 111 | + """ |
| 112 | + ); |
| 113 | + } |
| 114 | + return TimeZoneInfo; |
| 115 | + } |
| 116 | +} |
0 commit comments