C#C
C#13mo ago
v0fbu1vm

Time conversion issue?

    /// <summary>
    ///     Validates if the start and end times are in the future.
    /// </summary>
    /// <param name="date">The date of the shift (in local time).</param>
    /// <param name="start">The start time of the shift (in local time).</param>
    /// <param name="end">The end time of the shift (in local time).</param>
    /// <returns>True if the shift is in the future; otherwise, false.</returns>
    public static bool IsScheduledInFuture(DateTime date, TimeOnly start, TimeOnly end)
    {
        // Combine the provided date with the start and end times to create local DateTime values
        var localStart = date.Date.Add(start.ToTimeSpan());
        var localEnd = end > start
            ? date.Date.Add(end.ToTimeSpan()) // Same day
            : date.Date.AddDays(1).Add(end.ToTimeSpan()); // Next day for overnight shifts

        // Convert local times to UTC explicitly
        var utcStart = localStart.ToUniversalTime();
        var utcEnd = localEnd.ToUniversalTime();

        // Get current UTC time
        var now = DateTime.UtcNow;

        // Ensure the shift starts and ends in the future
        return utcStart > now && utcEnd > now;
    }
What am I missing here? The date, start and end are in local. For some reason this is returning true even though if the current start and end are in the past? I have written some tests, all of them are passing locally. When testing from the client, the behavior is not the same. The client is calling the server.
Was this page helpful?