From 4bcf15cb1411ceb2b890bc13eeb371acadcce9ba Mon Sep 17 00:00:00 2001 From: Mate Farkas Date: Wed, 17 Apr 2024 17:06:01 +0200 Subject: [PATCH] Calculate sync dates based on the latest current date in the world --- Utility/SyncUtility.cs | 15 ++++++++++++++- Utility/TimezoneUtility.cs | 10 ++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 Utility/TimezoneUtility.cs diff --git a/Utility/SyncUtility.cs b/Utility/SyncUtility.cs index c4b2c60..b54b424 100644 --- a/Utility/SyncUtility.cs +++ b/Utility/SyncUtility.cs @@ -215,7 +215,20 @@ namespace ConnectionsAPI.Utility // we iterate on every day between the start date and UTC tomorrow (this should handle +12 timezones as well) DateTime syncBeginDate = DateTime.ParseExact(startDate, SHORT_DATE, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal).Date; - DateTime syncEndDate = DateTimeOffset.UtcNow.UtcDateTime.Date.AddDays(1); + DateTime syncEndDate; + + // try to find the latest date that is currently going on in the world + TimeZoneInfo? latestTimezone = TimezoneUtility.GetLatestTimezoneOnSystem(); + if (latestTimezone != null) + { + DateTime currentDateInLatestTimezone = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, latestTimezone); + syncEndDate = new DateTime(currentDateInLatestTimezone.Year, currentDateInLatestTimezone.Month, currentDateInLatestTimezone.Day, 0, 0, 0, DateTimeKind.Utc); + } + // default to UTC date + 1 day + else + { + syncEndDate = DateTime.UtcNow.Date.AddDays(1); + } foreach (var date in Enumerable.Repeat(0, Convert.ToInt32((syncEndDate - syncBeginDate).TotalDays)).Select((_, idx) => syncBeginDate.AddDays(idx + 1))) { diff --git a/Utility/TimezoneUtility.cs b/Utility/TimezoneUtility.cs new file mode 100644 index 0000000..62bf9a4 --- /dev/null +++ b/Utility/TimezoneUtility.cs @@ -0,0 +1,10 @@ +namespace ConnectionsAPI.Utility +{ + public static class TimezoneUtility + { + public static TimeZoneInfo? GetLatestTimezoneOnSystem() => + TimeZoneInfo.GetSystemTimeZones() + .OrderByDescending(x => x.GetUtcOffset(DateTime.UtcNow)) + .FirstOrDefault(); + } +}