| | | 1 | | using Microsoft.JSInterop; |
| | | 2 | | |
| | | 3 | | namespace Pomodoro.Web.Services; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Service for getting the client's local date and time |
| | | 7 | | /// </summary> |
| | | 8 | | public class LocalDateTimeService : ILocalDateTimeService |
| | | 9 | | { |
| | | 10 | | private readonly IJSRuntime _jsRuntime; |
| | | 11 | | private DateTime? _cachedLocalDate; |
| | | 12 | | private DateTimeOffset? _cachedLocalDateTimeOffset; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Initializes a new instance of the LocalDateTimeService |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="jsRuntime">The JS runtime instance</param> |
| | 21 | 18 | | public LocalDateTimeService(IJSRuntime jsRuntime) |
| | 21 | 19 | | { |
| | 21 | 20 | | _jsRuntime = jsRuntime; |
| | 21 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the client's local date asynchronously |
| | | 25 | | /// </summary> |
| | | 26 | | /// <returns>The client's local date</returns> |
| | | 27 | | public virtual async Task<DateTime> GetLocalDateAsync() |
| | 10 | 28 | | { |
| | | 29 | | // Return cached value if available |
| | 10 | 30 | | if (_cachedLocalDate.HasValue) |
| | 2 | 31 | | { |
| | 2 | 32 | | return _cachedLocalDate.Value; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | try |
| | 8 | 36 | | { |
| | | 37 | | // Get the client's local date via JavaScript interop |
| | 8 | 38 | | var localDate = await _jsRuntime.InvokeAsync<DateTime>("localDateTime.getLocalDate"); |
| | 7 | 39 | | _cachedLocalDate = localDate; |
| | 7 | 40 | | return localDate; |
| | | 41 | | } |
| | 1 | 42 | | catch (Exception) |
| | 1 | 43 | | { |
| | | 44 | | // Fallback to server date if JavaScript interop fails |
| | 1 | 45 | | return DateTime.Now.Date; |
| | | 46 | | } |
| | 10 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets the client's local date and time with timezone offset asynchronously |
| | | 51 | | /// </summary> |
| | | 52 | | /// <returns>The client's local date and time with timezone offset</returns> |
| | | 53 | | public virtual async Task<DateTimeOffset> GetLocalDateTimeOffsetAsync() |
| | 9 | 54 | | { |
| | | 55 | | // Return cached value if available |
| | 9 | 56 | | if (_cachedLocalDateTimeOffset.HasValue) |
| | 2 | 57 | | { |
| | 2 | 58 | | return _cachedLocalDateTimeOffset.Value; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | try |
| | 7 | 62 | | { |
| | | 63 | | // Get the client's local date and time via JavaScript interop |
| | 7 | 64 | | var localDateTime = await _jsRuntime.InvokeAsync<DateTime>("localDateTime.getLocalDateTime"); |
| | 6 | 65 | | var offset = await _jsRuntime.InvokeAsync<int>("localDateTime.getTimezoneOffset"); |
| | | 66 | | |
| | | 67 | | // Create a DateTimeOffset with the local time and timezone offset |
| | 6 | 68 | | var localDateTimeOffset = new DateTimeOffset( |
| | 6 | 69 | | localDateTime.Year, |
| | 6 | 70 | | localDateTime.Month, |
| | 6 | 71 | | localDateTime.Day, |
| | 6 | 72 | | localDateTime.Hour, |
| | 6 | 73 | | localDateTime.Minute, |
| | 6 | 74 | | localDateTime.Second, |
| | 6 | 75 | | TimeSpan.FromMinutes(-offset)); // JavaScript returns offset in minutes with opposite sign |
| | | 76 | | |
| | 6 | 77 | | _cachedLocalDateTimeOffset = localDateTimeOffset; |
| | 6 | 78 | | return localDateTimeOffset; |
| | | 79 | | } |
| | 1 | 80 | | catch (Exception) |
| | 1 | 81 | | { |
| | | 82 | | // Fallback to server date if JavaScript interop fails |
| | 1 | 83 | | return DateTimeOffset.Now; |
| | | 84 | | } |
| | 9 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Clears the cached local date and time |
| | | 89 | | /// </summary> |
| | | 90 | | public void ClearCache() |
| | 6 | 91 | | { |
| | 6 | 92 | | _cachedLocalDate = null; |
| | 6 | 93 | | _cachedLocalDateTimeOffset = null; |
| | 6 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Gets the client's local date and time asynchronously |
| | | 98 | | /// </summary> |
| | | 99 | | /// <returns>The client's local date and time</returns> |
| | | 100 | | public virtual async Task<DateTime> GetLocalDateTimeAsync() |
| | 4 | 101 | | { |
| | | 102 | | try |
| | 4 | 103 | | { |
| | | 104 | | // Get the client's local date and time via JavaScript interop |
| | 4 | 105 | | var localDateTime = await _jsRuntime.InvokeAsync<DateTime>("localDateTime.getLocalDateTime"); |
| | 3 | 106 | | return localDateTime; |
| | | 107 | | } |
| | 1 | 108 | | catch (Exception) |
| | 1 | 109 | | { |
| | | 110 | | // Fallback to server date if JavaScript interop fails |
| | 1 | 111 | | return DateTime.Now; |
| | | 112 | | } |
| | 4 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Gets the client's timezone offset in minutes |
| | | 117 | | /// </summary> |
| | | 118 | | /// <returns>The timezone offset in minutes</returns> |
| | | 119 | | public virtual async Task<int> GetTimezoneOffsetAsync() |
| | 4 | 120 | | { |
| | | 121 | | try |
| | 4 | 122 | | { |
| | | 123 | | // Get the client's timezone offset via JavaScript interop |
| | 4 | 124 | | var offset = await _jsRuntime.InvokeAsync<int>("localDateTime.getTimezoneOffset"); |
| | 3 | 125 | | return offset; |
| | | 126 | | } |
| | 1 | 127 | | catch (Exception) |
| | 1 | 128 | | { |
| | | 129 | | // Fallback to server timezone offset if JavaScript interop fails |
| | 1 | 130 | | return (int)TimeZoneInfo.Local.GetUtcOffset(DateTime.Now).TotalMinutes; |
| | | 131 | | } |
| | 4 | 132 | | } |
| | | 133 | | } |