| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="GlobalizationService.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using MyNet.Globalization.Culture; |
| | | 10 | | using MyNet.Globalization.DateTime; |
| | | 11 | | using MyNet.Primitives; |
| | | 12 | | |
| | | 13 | | namespace MyNet.Globalization; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Provides a service for managing globalization settings, including culture and time zone information. |
| | | 17 | | /// </summary> |
| | | 18 | | public sealed class GlobalizationService(ICultureService cultureService, ITimeZoneService timeZoneService) |
| | | 19 | | : IGlobalizationService |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc /> |
| | 153 | 22 | | public CultureInfo CurrentCulture => cultureService.CurrentCulture; |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | 0 | 25 | | public TimeZoneInfo CurrentTimeZone => timeZoneService.CurrentTimeZone; |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | 0 | 28 | | public System.DateTime Now => timeZoneService.Now; |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public event EventHandler<CultureChangedEventArgs> CultureChanged |
| | | 32 | | { |
| | 15 | 33 | | add => cultureService.CultureChanged += value; |
| | 6 | 34 | | remove => cultureService.CultureChanged -= value; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public event EventHandler<TimeZoneChangedEventArgs> TimeZoneChanged |
| | | 39 | | { |
| | 15 | 40 | | add => timeZoneService.TimeZoneChanged += value; |
| | 6 | 41 | | remove => timeZoneService.TimeZoneChanged -= value; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | 5703 | 45 | | public void SetCulture(CultureInfo culture) => cultureService.SetCulture(culture); |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | 51 | 48 | | public void SetCulture(string cultureCode) => cultureService.SetCulture(cultureCode); |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | 0 | 51 | | public void SetTimeZone(TimeZoneInfo timeZone) => timeZoneService.SetTimeZone(timeZone); |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | 0 | 54 | | public void SetTimeZone(string timeZoneId) => timeZoneService.SetTimeZone(timeZoneId); |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | 0 | 57 | | public DateTimeOffset FromUtc(DateTimeOffset utcDateTime) => timeZoneService.FromUtc(utcDateTime); |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | 0 | 60 | | public DateTimeOffset ToUtc(DateTimeOffset localDateTime) => timeZoneService.ToUtc(localDateTime); |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | 0 | 63 | | public DateTimeOffset Convert(DateTimeOffset dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone |
| | | 64 | | |
| | | 65 | | /// <inheritdoc /> |
| | | 66 | | public IDisposable UseCulture(CultureInfo culture) |
| | | 67 | | { |
| | 0 | 68 | | ArgumentNullException.ThrowIfNull(culture); |
| | | 69 | | |
| | 0 | 70 | | var previousCulture = CurrentCulture; |
| | | 71 | | |
| | 0 | 72 | | cultureService.SetCulture(culture); |
| | | 73 | | |
| | 0 | 74 | | return new DelegateDisposable(() => cultureService.SetCulture(previousCulture)); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | | 78 | | public IDisposable UseTimeZone(TimeZoneInfo timeZone) |
| | | 79 | | { |
| | 0 | 80 | | ArgumentNullException.ThrowIfNull(timeZone); |
| | | 81 | | |
| | 0 | 82 | | var previousTimeZone = CurrentTimeZone; |
| | | 83 | | |
| | 0 | 84 | | timeZoneService.SetTimeZone(timeZone); |
| | | 85 | | |
| | 0 | 86 | | return new DelegateDisposable(() => timeZoneService.SetTimeZone(previousTimeZone)); |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |