|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Collections.ObjectModel; |
| 6 | +using System.Linq; |
| 7 | +using System.Text.RegularExpressions; |
| 8 | + |
| 9 | +namespace DevToys.Helpers |
| 10 | +{ |
| 11 | + internal static class TimestampToolHelper |
| 12 | + { |
| 13 | + internal static class ZoneInfo |
| 14 | + { |
| 15 | + private static string _utcDisplayName = ""; |
| 16 | + private static string _localDisplayName = ""; |
| 17 | + private static readonly IReadOnlyCollection<TimeZoneInfo> _systemTimeZone = TimeZoneInfo.GetSystemTimeZones(); |
| 18 | + private static readonly IReadOnlyDictionary<string, string> _timeZoneCollection = InitTimeZoneCollection(); |
| 19 | + |
| 20 | + internal static string UtcDisplayName => _utcDisplayName; |
| 21 | + |
| 22 | + internal static string LocalDisplayName => _localDisplayName; |
| 23 | + |
| 24 | + internal static IReadOnlyList<string> DisplayNames => _timeZoneCollection.Keys.ToList(); |
| 25 | + |
| 26 | + internal static IReadOnlyDictionary<string, string> TimeZones => _timeZoneCollection; |
| 27 | + |
| 28 | + private static IReadOnlyDictionary<string, string> InitTimeZoneCollection() |
| 29 | + { |
| 30 | + Dictionary<string, string> timeZoneCollection = new(); |
| 31 | + if (!Regex.IsMatch(_systemTimeZone.ElementAt(0).DisplayName, @"^\(UTC.*\).+$")) |
| 32 | + { |
| 33 | + // version < .Net6 |
| 34 | + // This implementation mitigates the changes in the strings |
| 35 | + // that are obtained when optimized in release builds, |
| 36 | + // as the target of external tools is .net6 or earlier. |
| 37 | + // zone.DisplayName : "(UTC+09:00) 大阪、札幌、東京"( >= .net6) or "東京 (標準時)"( < .net6) |
| 38 | + foreach (TimeZoneInfo zone in _systemTimeZone) |
| 39 | + { |
| 40 | + string displayName = $"(UTC{zone.BaseUtcOffset.Hours:+00;-00;}:{zone.BaseUtcOffset.Minutes:00;00;}) " + zone.DisplayName; |
| 41 | + if (zone.Id == TimeZoneInfo.Utc.Id) |
| 42 | + { |
| 43 | + displayName = "(UTC) " + zone.DisplayName; |
| 44 | + _utcDisplayName = "(UTC) " + zone.DisplayName; |
| 45 | + } |
| 46 | + if (zone.Id == TimeZoneInfo.Local.Id) |
| 47 | + { |
| 48 | + _localDisplayName = displayName; |
| 49 | + } |
| 50 | + timeZoneCollection.Add(displayName, zone.Id); |
| 51 | + } |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + // version >= .Net6 |
| 56 | + foreach (TimeZoneInfo zone in _systemTimeZone) |
| 57 | + { |
| 58 | + timeZoneCollection.Add(zone.DisplayName, zone.Id); |
| 59 | + } |
| 60 | + _utcDisplayName = TimeZoneInfo.Utc.DisplayName; |
| 61 | + _localDisplayName = TimeZoneInfo.Local.DisplayName; |
| 62 | + } |
| 63 | + return timeZoneCollection; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + internal static class TimeZone |
| 68 | + { |
| 69 | + internal static DateTimeOffset SafeMinValue(TimeZoneInfo timezone) |
| 70 | + { |
| 71 | + if (timezone is null) |
| 72 | + { |
| 73 | + timezone = TimeZoneInfo.Utc; |
| 74 | + } |
| 75 | + DateTimeOffset t1 = TimeZoneInfo.ConvertTime( |
| 76 | + new DateTimeOffset(10, 1, 1, 0, 0, 0, TimeZoneInfo.Utc.BaseUtcOffset), |
| 77 | + timezone); |
| 78 | + DateTimeOffset minValue = DateTimeOffset.MinValue; |
| 79 | + if (t1.Year < 10) |
| 80 | + { |
| 81 | + minValue = minValue.Subtract(t1.Offset); |
| 82 | + } |
| 83 | + return TimeZoneInfo.ConvertTime(minValue, timezone); |
| 84 | + } |
| 85 | + |
| 86 | + internal static DateTimeOffset SafeMaxValue(TimeZoneInfo timezone) |
| 87 | + { |
| 88 | + if (timezone is null) |
| 89 | + { |
| 90 | + timezone = TimeZoneInfo.Utc; |
| 91 | + } |
| 92 | + DateTimeOffset t1 = TimeZoneInfo.ConvertTime( |
| 93 | + new DateTimeOffset(9990, 12, 31, 23, 59, 59, TimeZoneInfo.Utc.BaseUtcOffset), |
| 94 | + timezone); |
| 95 | + DateTimeOffset maxValue = DateTimeOffset.MaxValue; |
| 96 | + if (t1.Year > 9990) |
| 97 | + { |
| 98 | + maxValue = maxValue.Subtract(t1.Offset); |
| 99 | + } |
| 100 | + return TimeZoneInfo.ConvertTime(maxValue, timezone); |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | +} |
0 commit comments