| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TimeZoneService.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Threading; |
| | | 9 | | |
| | | 10 | | namespace 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> |
| | | 16 | | public sealed class TimeZoneService(TimeZoneInfo initialTimeZone) : ITimeZoneService |
| | | 17 | | { |
| | 48 | 18 | | 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() |
| | 15 | 22 | | : this(TimeZoneInfo.Local) |
| | | 23 | | { |
| | 15 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary>Initializes a new instance of the <see cref="TimeZoneService"/> class.Initializes with the default time |
| | | 27 | | public TimeZoneService(GlobalizationOptions options) |
| | 6 | 28 | | : this(options.DefaultTimeZone) |
| | | 29 | | { |
| | 6 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | 3 | 33 | | 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 | | { |
| | 15 | 47 | | ArgumentNullException.ThrowIfNull(timeZone); |
| | | 48 | | |
| | | 49 | | TimeZoneChangedEventArgs? args; |
| | | 50 | | lock (_lockObject) |
| | | 51 | | { |
| | 18 | 52 | | if (CurrentTimeZone.Equals(timeZone)) return; |
| | | 53 | | |
| | 6 | 54 | | var old = CurrentTimeZone; |
| | 6 | 55 | | CurrentTimeZone = timeZone; |
| | 6 | 56 | | args = new(old, timeZone); |
| | 6 | 57 | | } |
| | | 58 | | |
| | | 59 | | // Raise event outside the lock to prevent deadlocks when subscribers access the service. |
| | 6 | 60 | | TimeZoneChanged?.Invoke(this, args); |
| | 9 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | 3 | 64 | | public void SetTimeZone(string timeZoneId) => SetTimeZone(TimeZoneInfo.FindSystemTimeZoneById(timeZoneId)); |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | 3 | 67 | | public DateTimeOffset FromUtc(DateTimeOffset utcDateTime) => Convert(utcDateTime, TimeZoneInfo.Utc, CurrentTimeZone) |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | 3 | 70 | | public DateTimeOffset ToUtc(DateTimeOffset localDateTime) => Convert(localDateTime, CurrentTimeZone, TimeZoneInfo.Ut |
| | | 71 | | |
| | | 72 | | /// <inheritdoc /> |
| | | 73 | | public DateTimeOffset Convert(DateTimeOffset dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone |
| | | 74 | | { |
| | 9 | 75 | | var utc = TimeZoneInfo.ConvertTimeToUtc(dateTime.DateTime, sourceTimeZone); |
| | 9 | 76 | | return TimeZoneInfo.ConvertTimeFromUtc(utc, destinationTimeZone); |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |