Search code examples
c#-4.0timezonereminders

Reminder Service in C#.Net 4.0 using UTC Time


I have a Windows Service that runs on my

Texas origin Server Central Time.

This will check for all active reminders and compares the reminder time that user wants and send out reminder if it matches with user required time.

Scenarios

  1. User is from EST
  2. User set up a reminder for *1:25 PM * using UI via my website
  3. On submit, my C# business logic converts this time to UTC before storing in my database. That will become '18:25:00'
  4. My Business logic will pull all Active reminders from DB
  5. And checks for reminder time if current UTC time and Reminder setup time diffrence is less then 5 mins, then it will send notification to that customer.

this is how my logic written

 DateTime CurrentDate = DateTime.Now.ToUniversalTime();
 TimeSpan currentTime = DateTime.Now.ToUniversalTime().TimeOfDay;

 if (Reminder.DailyReminders.Any(x => currentTime.Subtract(x.ReminderTime).TotalMinutes < 5 
&& currentTime.Subtract(x.ReminderTime).TotalMinutes > 0))
    {
        if (Reminder.ReminderMedhodID.Equals(1))
        _email.ComposeEmail(Reminder);
    }

My Problem is *currentTime* is always 1 hour behind to user requested reminder time SO my reminders are going out 1 hour late.

Note : currentTime is from below

TimeSpan currentTime = DateTime.Now.ToUniversalTime().TimeOfDay;

I am not sure if this is the best way to handle this requirement. considering this is one of the way, can any on help how to fix this issue?

Thanks to peter's answer

Can any one help me how to take user input time with Daylight consideration

This what i have so far

public TimeSpan ConvertToUTCTime(string dateStr)
        {
            DateTime localDateTime = DateTime.Parse(dateStr); // Local .NET timeZone.
            DateTime utcDateTime = localDateTime.ToUniversalTime();

            string clTimeZoneKey = TimeZone.CurrentTimeZone.StandardName;
            TimeZoneInfo clTimeZone = TimeZoneInfo.FindSystemTimeZoneById(clTimeZoneKey);
            DateTime clDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, clTimeZone);
            if (clTimeZone.IsDaylightSavingTime(localDateTime))
            {
                // Get DayLight local time in UTC
                // Yet to be implemented
            }
            return clDateTime.TimeOfDay;
        }

I got this worked using this http://msdn.microsoft.com/en-us/library/system.globalization.daylighttime.aspx


Solution

  • Ummm...we are currently using Daylight Saving Time in most of the U.S. (though there are some portions of Indiana that did/do use EST?) Since EDT is one hour ahead of EST, your logic is correct. It is the input (EST) that is incorrect.