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(); + } +}