< Summary

Information
Class: MyNet.Globalization.DateTime.TimeZoneService
Assembly: MyNet.Globalization
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Globalization/DateTime/TimeZoneService.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 79
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%
.ctor()100%11100%
.ctor(...)100%11100%
get_Now()100%11100%
SetTimeZone(...)100%44100%
SetTimeZone(...)100%11100%
FromUtc(...)100%11100%
ToUtc(...)100%11100%
Convert(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Globalization/DateTime/TimeZoneService.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TimeZoneService.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Threading;
 9
 10namespace MyNet.Globalization.DateTime;
 11
 12/// <summary>
 13/// Provides a service for managing the current time zone in an application.
 14/// The <see cref="TimeZoneChanged"/> event is raised <b>outside</b> the internal lock to prevent deadlocks.
 15/// </summary>
 16public sealed class TimeZoneService(TimeZoneInfo initialTimeZone) : ITimeZoneService
 17{
 4818    private readonly Lock _lockObject = new();
 19
 20    /// <summary>Initializes a new instance of the <see cref="TimeZoneService"/> class.Initializes with the local time z
 21    public TimeZoneService()
 1522        : this(TimeZoneInfo.Local)
 23    {
 1524    }
 25
 26    /// <summary>Initializes a new instance of the <see cref="TimeZoneService"/> class.Initializes with the default time
 27    public TimeZoneService(GlobalizationOptions options)
 628        : this(options.DefaultTimeZone)
 29    {
 630    }
 31
 32    /// <inheritdoc />
 333    public System.DateTime Now => TimeZoneInfo.ConvertTime(System.DateTime.UtcNow, CurrentTimeZone);
 34
 35    /// <inheritdoc />
 36    public TimeZoneInfo CurrentTimeZone { get; private set; } = initialTimeZone;
 37
 38    /// <inheritdoc />
 39    public event EventHandler<TimeZoneChangedEventArgs>? TimeZoneChanged;
 40
 41    /// <inheritdoc />
 42    /// <remarks>
 43    /// This method is thread-safe. The <see cref="TimeZoneChanged"/> event is raised <b>outside</b> the internal lock.
 44    /// </remarks>
 45    public void SetTimeZone(TimeZoneInfo timeZone)
 46    {
 1547        ArgumentNullException.ThrowIfNull(timeZone);
 48
 49        TimeZoneChangedEventArgs? args;
 50        lock (_lockObject)
 51        {
 1852            if (CurrentTimeZone.Equals(timeZone)) return;
 53
 654            var old = CurrentTimeZone;
 655            CurrentTimeZone = timeZone;
 656            args = new(old, timeZone);
 657        }
 58
 59        // Raise event outside the lock to prevent deadlocks when subscribers access the service.
 660        TimeZoneChanged?.Invoke(this, args);
 961    }
 62
 63    /// <inheritdoc />
 364    public void SetTimeZone(string timeZoneId) => SetTimeZone(TimeZoneInfo.FindSystemTimeZoneById(timeZoneId));
 65
 66    /// <inheritdoc />
 367    public DateTimeOffset FromUtc(DateTimeOffset utcDateTime) => Convert(utcDateTime, TimeZoneInfo.Utc, CurrentTimeZone)
 68
 69    /// <inheritdoc />
 370    public DateTimeOffset ToUtc(DateTimeOffset localDateTime) => Convert(localDateTime, CurrentTimeZone, TimeZoneInfo.Ut
 71
 72    /// <inheritdoc />
 73    public DateTimeOffset Convert(DateTimeOffset dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone
 74    {
 975        var utc = TimeZoneInfo.ConvertTimeToUtc(dateTime.DateTime, sourceTimeZone);
 976        return TimeZoneInfo.ConvertTimeFromUtc(utc, destinationTimeZone);
 77    }
 78}
 79