using System;
using System.Globalization; // For TimeZoneInfo
public class DateTimeCompareService
{
// This method safely finds the Eastern Time Zone (America/New_York)
// which correctly handles EST/EDT transitions.
private static TimeZoneInfo GetEasternTimeZone()
{
try
{
// Prefer America/New_York for cross-platform compatibility and DST handling
return TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
}
catch (TimeZoneNotFoundException)
{
// Fallback for Windows systems where "Eastern Standard Time" is the ID
return TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
}
catch (Exception ex)
{
// Log or rethrow if the timezone cannot be found for some reason
Console.WriteLine($"Error finding Eastern Time Zone: {ex.Message}");
throw new InvalidOperationException("Could not resolve Eastern Time Zone.", ex);
}
}
public void CompareDates(DateTime dbSubmittedDate)
{
// Assuming dbSubmittedDate from the database has DateTimeKind.Unspecified,
// and its actual time zone is Eastern Time (EST/EDT).
// If it's DateTimeKind.Local but *really* is Eastern Time,
// you might need to ensure it's treated as Unspecified first,
// or fix the data storage/retrieval process.
// For robust conversion, we'll explicitly state its origin time zone.
TimeZoneInfo easternTimeZone = GetEasternTimeZone();
// Convert the DB's EST DateTime to UTC
DateTime submittedDateUtc = TimeZoneInfo.ConvertTimeToUtc(dbSubmittedDate, easternTimeZone);
// Get the current UTC DateTime
DateTime nowUtc = DateTime.UtcNow;
// Extract the DateOnly parts from the UTC DateTimes
DateOnly submittedDateOnlyUtc = DateOnly.FromDateTime(submittedDateUtc);
DateOnly todayUtcDateOnly = DateOnly.FromDateTime(nowUtc);
Console.WriteLine($"DB Submitted Date (EST assumed): {dbSubmittedDate}");
Console.WriteLine($"DB Submitted Date (UTC converted): {submittedDateUtc}");
Console.WriteLine($"Current UTC Time: {nowUtc}");
Console.WriteLine($"DB Submitted DateOnly (UTC): {submittedDateOnlyUtc}");
Console.WriteLine($"Today's DateOnly (UTC): {todayUtcDateOnly}");
if (submittedDateOnlyUtc != todayUtcDateOnly)
{
Console.WriteLine("Condition met: Submitted date is NOT today in UTC.");
// do stuff
}
else
{
Console.WriteLine("Condition not met: Submitted date IS today in UTC.");
}
Console.WriteLine("\n--- Scenario: What if 'today' means today in EST? ---");
// If you wanted "today" to be relative to the *original* EST timezone:
DateTime nowEst = TimeZoneInfo.ConvertTimeFromUtc(nowUtc, easternTimeZone);
DateOnly todayEstDateOnly = DateOnly.FromDateTime(nowEst);
DateOnly submittedDateOnlyEst = DateOnly.FromDateTime(dbSubmittedDate); // Assuming dbSubmittedDate is already the EST representation
Console.WriteLine($"DB Submitted DateOnly (EST): {submittedDateOnlyEst}");
Console.WriteLine($"Today's DateOnly (EST): {todayEstDateOnly}");
if (submittedDateOnlyEst != todayEstDateOnly)
{
Console.WriteLine("Condition met: Submitted date is NOT today in EST.");
// do stuff
}
else
{
Console.WriteLine("Condition not met: Submitted date IS today in EST.");
}
}
public static void Main(string[] args)
{
DateTimeCompareService service = new DateTimeCompareService();
// Example 1: Submitted yesterday EST, compare to now UTC.
// Suppose it's May 8, 2025 10 PM UTC (6 PM EST, if no DST)
// And the DB value is May 8, 2025 5 PM EST
Console.WriteLine("--- Test Case 1: DB value is yesterday EST ---");
DateTime dbDateYesterdayEst = new DateTime(2025, 5, 8, 17, 0, 0, DateTimeKind.Unspecified); // 5 PM EST May 8
service.CompareDates(dbDateYesterdayEst);
Console.WriteLine("\n--- Test Case 2: DB value is today EST, but early morning ---");
// Suppose it's May 9, 2025 02 AM UTC (10 PM EST May 8 if no DST)
// And the DB value is May 9, 2025 01 AM EST
// This will depend on actual current system time when running.
// For a predictable test: let's simulate nowUtc being May 9, 2025 05:00:00 UTC
// (which would be May 9, 2025 01:00:00 AM EST, assuming no DST)
Console.WriteLine("\n--- Test Case 3: DB value is today EST, same day ---");
DateTime dbDateTodayEst = new DateTime(2025, 5, 9, 12, 0, 0, DateTimeKind.Unspecified); // May 9, 12 PM EST
service.CompareDates(dbDateTodayEst);
Console.WriteLine("\n--- Test Case 4: DB value is today EST, but current UTC puts it on different day ---");
// This scenario happens if the "today" in UTC crosses midnight relative to EST.
// E.g., DB: May 9, 2025 1:00 AM EST. Current: May 9, 2025 8:00 AM UTC (4:00 AM EST).
// Both will be May 9 when converted to UTC for comparison.
// Let's use an example where current UTC *is* May 10, but DB is May 9 late EST
// This is a tricky one.
// If db.SubmittedDate is 2025-05-09 23:00:00 EST (May 9, 11 PM EST)
// That converts to 2025-05-10 04:00:00 UTC (May 10, 4 AM UTC)
// If nowUtc is 2025-05-10 05:00:00 UTC
// Then submittedDateOnlyUtc will be 2025-05-10
// And todayUtcDateOnly will be 2025-05-10
// They will be equal in UTC.
// This implies that "today" is defined as "today in UTC".
// If "today" means the calendar day in the *EST* timezone, that's a different comparison (see Scenario B).
// The user's wording "datetime date only is today" and then clarifying "SubmittedDate is EST and DateTime.Now is coming in as GMT"
// strongly suggests they want to align them to UTC for comparison.
// This is the most common and robust way to do date comparisons across timezones.
Console.WriteLine("\n--- Test Case 5: DB value is late EST, crosses midnight to UTC ---");
DateTime dbDateLateEst = new DateTime(2025, 5, 9, 23, 0, 0, DateTimeKind.Unspecified); // May 9, 11 PM EST
service.CompareDates(dbDateLateEst); // This should be 'today' in UTC if nowUtc is also May 10th UTC.
}
}