From 2b48fffa03edb319991c0a80fbe01d917a05c835 Mon Sep 17 00:00:00 2001 From: Winnie Date: Sat, 31 Aug 2024 02:34:04 +0200 Subject: [PATCH] more timezone fixes --- Program.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Program.cs b/Program.cs index 665d361..f0d04c5 100644 --- a/Program.cs +++ b/Program.cs @@ -123,7 +123,9 @@ private static async Task ScheduleNextPost() { var nowUtc = DateTime.UtcNow; var spainTime = TimeZoneInfo.ConvertTimeFromUtc(nowUtc, _spainTimeZone); - var nextPostTimeSpain = DateTime.Today.Add(_postTimeSpain); + + // Specify that nextPostTimeSpain is unspecified in terms of kind because we will convert it to a specific time zone + var nextPostTimeSpain = DateTime.SpecifyKind(DateTime.Today.Add(_postTimeSpain), DateTimeKind.Unspecified); if (nextPostTimeSpain <= spainTime) { @@ -131,14 +133,11 @@ private static async Task ScheduleNextPost() nextPostTimeSpain = nextPostTimeSpain.AddDays(1); } - // Ensure that nextPostTimeSpain is in local time - var nextPostTimeSpainLocal = DateTime.SpecifyKind(nextPostTimeSpain, DateTimeKind.Local); - - // Convert the local time to UTC - var nextPostTimeUtc = TimeZoneInfo.ConvertTimeToUtc(nextPostTimeSpainLocal, _spainTimeZone); + // Convert the unspecified time to Spain time zone and then to UTC + nextPostTimeSpain = TimeZoneInfo.ConvertTimeToUtc(nextPostTimeSpain, _spainTimeZone); // Calculate the delay - var delay = nextPostTimeUtc - nowUtc; + var delay = nextPostTimeSpain - nowUtc; Console.WriteLine($"Scheduling next post in {delay.TotalMinutes} minutes.");