improve TimeZoneService

This commit is contained in:
sbwalker
2025-07-28 17:00:27 -04:00
parent 91c5309855
commit e179976fe8
2 changed files with 29 additions and 18 deletions

View File

@ -117,13 +117,4 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Universal" xml:space="preserve">
<value>(UTC) Coordinated Universal Time</value>
</data>
<data name="US/Eastern" xml:space="preserve">
<value>(UTC-05:00) Eastern Time (US &amp; Canada)</value>
</data>
<data name="US/Pacific" xml:space="preserve">
<value>(UTC-08:00) Pacific Time (US &amp; Canada)</value>
</data>
</root>

View File

@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Localization;
using NodaTime.TimeZones;
using NodaTime;
using Oqtane.Documentation;
using Oqtane.Models;
using Oqtane.Shared;
using NodaTime.Extensions;
namespace Oqtane.Services
{
@ -17,18 +19,36 @@ namespace Oqtane.Services
_TimeZoneLocalizer = TimeZoneLocalizer;
}
public List<TimeZone> GetTimeZones()
public List<Models.TimeZone> GetTimeZones()
{
var _timezones = new List<TimeZone>();
foreach (var timezone in Utilities.GetTimeZones())
var timezones = new List<Models.TimeZone>();
foreach (var tz in DateTimeZoneProviders.Tzdb.GetAllZones()
// only include timezones which have a country code defined or are US timezones
.Where(item => !string.IsNullOrEmpty(TzdbDateTimeZoneSource.Default.ZoneLocations.FirstOrDefault(l => l.ZoneId == item.Id)?.CountryCode) || item.Id.ToLower().Contains("us/"))
// order by UTC offset (ie. -11:00 to +14:00)
.OrderBy(item => item.GetUtcOffset(Instant.FromDateTimeUtc(DateTime.UtcNow)).Ticks))
{
_timezones.Add(new TimeZone
// get localized display name
var displayname = _TimeZoneLocalizer[tz.Id].Value;
if (displayname == tz.Id)
{
Id = timezone.Id,
DisplayName = _TimeZoneLocalizer[timezone.Id]
// use default "friendly" display format
displayname = displayname.Replace("_", " ").Replace("/", " / ");
}
// include offset prefix
var offset = tz.GetUtcOffset(Instant.FromDateTimeUtc(DateTime.UtcNow)).Ticks;
displayname = "(UTC" + (offset >= 0 ? "+" : "-") + new DateTime(Math.Abs(offset)).ToString("HH:mm") + ") " + displayname;
timezones.Add(new Models.TimeZone()
{
Id = tz.Id,
DisplayName = displayname
});
}
return _timezones.OrderBy(item => item.DisplayName).ToList();
return timezones;
}
}
}