How to send reminders at the right time for users in different time zones?

I have never created a reminder app. This is how I see it. Please let me know if I am on the right track.

So, I have users from different time zones.

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 minutes and follow these steps:

  • Get records
  • The second way is to convert DateTimeUTC to Time for the correct time zone.
  • Compare if it matches a. Send a reminder
    var tzi = TimeZoneInfo.FindSystemTimeZoneById(TimeZneID1);
    var local = TimeZoneInfo.ConvertTimeFromUtc(DateTimeUTC, tzi);
    var timeNow = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now, tzi);
    if(local == timeNow)
    SendReminder();

Is this an effective way? Is this the right way?

+4
source share
4 answers

If the date / time values ​​are already set to UTC in the database, you do not need to perform any conversions, of course ... you just need to find out if the current UTC moment matches, and if so, send a reminder.

Assuming you really mean UTC in the database, i.e. You converted it from the user's local time when they entered the reminder (assuming that they did it for a start).

+8
source

In my opinion, you may be complicating this too much. Since you store things in UTC, remind in UTC and agree to UTC. Then simply map the reminders to the users you want to remind.

+1
source

Usually, dealing with such dates, you do all your calculations in UTC format and only switch to local time when this time (pun intended) to display the results. I assume from your question that this is a centralized database that manages all tasks, and you just need them to work at the right 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 consistency between time zones.

Another thing you should be aware of is that it only starts the task scheduler every 15 minutes, so the probability of a match is exactly equal to 15:15:15. What you would usually like to do is check the reminder time that has appeared since the last run:

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

I want to do such things and discussed what would be the appropriate approach. In essence, I am writing an application that will send a message for a specific country at midnight (local time). I have about 100 of these countries, and I also have to remember about summer savings. I can only think of 2 ways to do this, 1. Create an appropriate stream for each country and that wakes up every hour or so and checks if it is midnight (local time) and a message is sent. Therefore, in essence, I will create 100 threads most of the time. 2. With this approach, there will be only one timer that checks every minute or 30 seconds of local time for 100 countries and sends a message. There must be some additional logic, since there will never be an exact match at midnight. Not sure if there is a better way to handle the situation above. It would be great if I could get some ideas / suggestions here. Thanks, SP.

0
source

All Articles