< Summary

Information
Class: Pomodoro.Web.Services.LocalDateTimeService
Assembly: Pomodoro.Web
File(s): /home/runner/work/Pomodoro/Pomodoro/src/Pomodoro.Web/Services/LocalDateTimeService.cs
Line coverage
100%
Covered lines: 57
Uncovered lines: 0
Coverable lines: 57
Total lines: 133
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetLocalDateAsync()100%22100%
GetLocalDateTimeOffsetAsync()100%22100%
ClearCache()100%11100%
GetLocalDateTimeAsync()100%11100%
GetTimezoneOffsetAsync()100%11100%

File(s)

/home/runner/work/Pomodoro/Pomodoro/src/Pomodoro.Web/Services/LocalDateTimeService.cs

#LineLine coverage
 1using Microsoft.JSInterop;
 2
 3namespace Pomodoro.Web.Services;
 4
 5/// <summary>
 6/// Service for getting the client's local date and time
 7/// </summary>
 8public 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>
 2118    public LocalDateTimeService(IJSRuntime jsRuntime)
 2119    {
 2120        _jsRuntime = jsRuntime;
 2121    }
 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()
 1028    {
 29        // Return cached value if available
 1030        if (_cachedLocalDate.HasValue)
 231        {
 232            return _cachedLocalDate.Value;
 33        }
 34
 35        try
 836        {
 37            // Get the client's local date via JavaScript interop
 838            var localDate = await _jsRuntime.InvokeAsync<DateTime>("localDateTime.getLocalDate");
 739            _cachedLocalDate = localDate;
 740            return localDate;
 41        }
 142        catch (Exception)
 143        {
 44            // Fallback to server date if JavaScript interop fails
 145            return DateTime.Now.Date;
 46        }
 1047    }
 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()
 954    {
 55        // Return cached value if available
 956        if (_cachedLocalDateTimeOffset.HasValue)
 257        {
 258            return _cachedLocalDateTimeOffset.Value;
 59        }
 60
 61        try
 762        {
 63            // Get the client's local date and time via JavaScript interop
 764            var localDateTime = await _jsRuntime.InvokeAsync<DateTime>("localDateTime.getLocalDateTime");
 665            var offset = await _jsRuntime.InvokeAsync<int>("localDateTime.getTimezoneOffset");
 66
 67            // Create a DateTimeOffset with the local time and timezone offset
 668            var localDateTimeOffset = new DateTimeOffset(
 669                localDateTime.Year,
 670                localDateTime.Month,
 671                localDateTime.Day,
 672                localDateTime.Hour,
 673                localDateTime.Minute,
 674                localDateTime.Second,
 675                TimeSpan.FromMinutes(-offset)); // JavaScript returns offset in minutes with opposite sign
 76
 677            _cachedLocalDateTimeOffset = localDateTimeOffset;
 678            return localDateTimeOffset;
 79        }
 180        catch (Exception)
 181        {
 82            // Fallback to server date if JavaScript interop fails
 183            return DateTimeOffset.Now;
 84        }
 985    }
 86
 87    /// <summary>
 88    /// Clears the cached local date and time
 89    /// </summary>
 90    public void ClearCache()
 691    {
 692        _cachedLocalDate = null;
 693        _cachedLocalDateTimeOffset = null;
 694    }
 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()
 4101    {
 102        try
 4103        {
 104            // Get the client's local date and time via JavaScript interop
 4105            var localDateTime = await _jsRuntime.InvokeAsync<DateTime>("localDateTime.getLocalDateTime");
 3106            return localDateTime;
 107        }
 1108        catch (Exception)
 1109        {
 110            // Fallback to server date if JavaScript interop fails
 1111            return DateTime.Now;
 112        }
 4113    }
 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()
 4120    {
 121        try
 4122        {
 123            // Get the client's timezone offset via JavaScript interop
 4124            var offset = await _jsRuntime.InvokeAsync<int>("localDateTime.getTimezoneOffset");
 3125            return offset;
 126        }
 1127        catch (Exception)
 1128        {
 129            // Fallback to server timezone offset if JavaScript interop fails
 1130            return (int)TimeZoneInfo.Local.GetUtcOffset(DateTime.Now).TotalMinutes;
 131        }
 4132    }
 133}