4

I have never created a reminder application. Here is how I see it. Please let me know if I'm on the right way.

So I have users from different timezones.

ID         DateTimeUTC               TimeZoneID            

1          2011-07-12 02:15:15.000   TimeZneID1
2          2011-07-13 16:00:00.000   TimeZneID2
3          2013-11-03 17:00:00.000   TimeZneID3
4          2011-08-22 03:00:00.000   TimeZneID4
5          2011-07-16 22:00:00.000   TimeZneID5

Create a scheduled process to run every 15 mins and do the steps below:

  1. Get records;
  2. The second is to convert DateTimeUTC to Time for the right timezone
  3. Compare if it's match
    a. Send Reminder
    var tzi = TimeZoneInfo.FindSystemTimeZoneById(TimeZneID1);
    var local = TimeZoneInfo.ConvertTimeFromUtc(DateTimeUTC, tzi);
    var timeNow = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now, tzi);
    if(local == timeNow)
    SendReminder();

Is it efficient way? is it the right way?

4 Answers 4

8

If date/time values are already in UTC in the database, you don't need to perform any conversions, surely... you just need to see whether the current UTC instant is a match, and if so, send the reminder.

That's assuming you really mean it's UTC in the database, i.e. you've converted it from the user's local time when they entered the reminder (assuming they did so to start with).

1
  • 1
    To re-emphasize Jon's point -- you must convert the time to UTC before you store it in the database if you expect to be able to do this type of date math when pulling data out of the database. Jul 18, 2011 at 17:55
2

Typically, when dealing with dates like this, you would do all of your calculations in UTC and only switch to local time when it's time (no pun intended) to display the results. I assume from your question that this is a centralized database that's managing all the tasks, and you just need them to run at the correct local time?

if ( dateTimeUtc == DateTime.UtcNow )
{
    // If your reminder needs to display the local time, pass it in:
    var tzi = TimeZoneInfo.FindSystemTimeZoneById(TimeZneID1);
    SendReminder(TimeZoneInfo.ConvertFromUtc(DateTime.UtcNow, tzi));
}

Note that DateTime.Now is in local time; you want DateTime.UtcNow for consistancy across time zones.

Another thing to be aware of is you are only running your task scheduler every 15 minutes, so the odds of times like 02:15:15 matching exactly are slim. What you would typically want to do is check for any reminder times that came up since the last run:

var currentRun = DateTime.UtcNow;
foreach ( dateTimeUtc in GetReminderDateTimes() )
{
    if ( dateTimeUtc > lastRun && dateTimeUtc <= currentRun )
    {
    }
}
lastRun = currentRun;
0
1

In my opinion you might be over-complicating it. Since you are storing things in UTC, have the reminders in UTC, and match on UTC. Then just associate the reminders with the users that you want to remind.

0

I want to do similar stuff and have been debating what would be an appropriate approach. Essentially, I am writing an application which would send a message for a particular country at midnight (localtime). I have about 100 such countries and I have to also be mindful of daylight savings. I can only think of 2 ways to do this, 1. Create a corresponding thread for each country and which wakes up every hour or so and checks if it is midnight(local time) and send out the message. So essentially, I will be creating 100 threads doing nothing most of the time. 2. In this approach, there will be only one timer which checks every minute or 30 secs the local time for 100 countries and send message. There will need to be some extra logic as there will never be an exact midnight match. Not sure, if there is any better way to tackle above situation. It would be great if I can get some ideas/suggestions here. Thanks, SP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.