timezones not language depended , too bad
e3ac9cae041c023d1bffa7f2f9479261e6434858
1 files changed
src/GitServer/Services/TimeZoneService.cs
diff --git a/src/GitServer/Services/TimeZoneService.cs b/src/GitServer/Services/TimeZoneService.cs
index 1d8457e..5721867 100644
--- a/src/GitServer/Services/TimeZoneService.cs
+++ b/src/GitServer/Services/TimeZoneService.cs
@@ -1,11 +1,17 @@
+using System.Collections.Concurrent;
+using System.Globalization;
+
namespace GitServer.Services;
-public class TimeZoneService(IHttpContextAccessor httpContextAccessor)
+public class TimeZoneService(IHttpContextAccessor httpContextAccessor, LocalizationService localization)
{
private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor;
+ private readonly LocalizationService _localization = localization;
public const string CookieName = "tz";
+ private static readonly ConcurrentDictionary<string, List<(string Id, string DisplayName)>> _zoneCache = new();
+
public string CurrentTimeZoneId
{
get
@@ -35,9 +41,26 @@ public class TimeZoneService(IHttpContextAccessor httpContextAccessor)
}
public IEnumerable<(string Id, string DisplayName)> AvailableTimeZones() =>
- TimeZoneInfo.GetSystemTimeZones()
- .OrderBy(tz => tz.BaseUtcOffset)
- .Select(tz => (tz.Id, $"{FormatOffset(tz.BaseUtcOffset)} {tz.DisplayName}"));
+ _zoneCache.GetOrAdd(_localization.CurrentLanguage, lang =>
+ {
+ CultureInfo culture;
+ try { culture = CultureInfo.GetCultureInfo(lang); }
+ catch (CultureNotFoundException) { culture = CultureInfo.GetCultureInfo("en"); }
+
+ var original = CultureInfo.CurrentUICulture;
+ try
+ {
+ CultureInfo.CurrentUICulture = culture;
+ return TimeZoneInfo.GetSystemTimeZones()
+ .OrderBy(tz => tz.BaseUtcOffset)
+ .Select(tz => (tz.Id, $"{FormatOffset(tz.BaseUtcOffset)} {tz.DisplayName}"))
+ .ToList();
+ }
+ finally
+ {
+ CultureInfo.CurrentUICulture = original;
+ }
+ });
private static string FormatOffset(TimeSpan offset) =>
$"(UTC{(offset < TimeSpan.Zero ? "-" : "+")}{offset:hh\\:mm})";