Skip to content

Commit

Permalink
more timezone fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
outerwinnie committed Aug 31, 2024
1 parent 80ec8e1 commit 2b48fff
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,21 @@ 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)
{
// If the time has already passed for today, schedule for tomorrow
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.");

Expand Down

0 comments on commit 2b48fff

Please sign in to comment.