Local notifications in UTC

The notifications plugin ( https://docs.coronalabs.com/plugin/notifications/scheduleNotification.html ) specifically says to use UTC time when scheduling notifications. So what happens with changing time zones? If I schedule a notification for 30 minutes from now (using UTC), and move into a new time zone which moves my clock up one hour (so the scheduled notification is now 30 minutes in the past), will it notify immediately?

That’s the beauty of UTC. It’s timezone agnostic.  3:30 CT is 4:30 ET but they are all 9:30UTC.

Rob

Thanks Rob. That introduces an issue though. Let’s say I’m in CT at 2 PM. I use scheduleNotification() to schedule something at 4 PM CT which is 10 PM UTC. Now I get in my car and drive into a new time zone, ET. My notification was set for 10 PM UTC which is 5 PM ET, not the 4 PM I’m expecting. So I’m getting the notification an hour later than expected, because I moved the device into a new time zone.

Is there a way around this?

Currently, you have to calculate the difference between two time zones and subtract it.

local diff = os.difftime(os.time(), os.time(os.date("!\*t", os.time())))

Lua is relatively primitive about time zone management. You have to calculate datetime in seconds since Unix epoch time. I wrote some utility classes for datetime algebra, which were not well tested yet.

But if the device changes time zone, that doesn’t reschedule notifications, does it? It would still be scheduled under the old time zone. I’m guessing the only solution is creating a handler for time zone change events…

You are right, my solution didn’t handle time zone change.

It is quite difficult to manage time zone issue in local notifications since the notification event id will disappear when the app closes, no reliable tracker for the event. I think one server is needed for this issue, but it might be complex as the mobile app and the server have to sync notification events. I just ignore the issue in my app because time zone changes are infrequent for most end users. (Not a good model, sorry.)

I do the same thing. As you said, it rarely happens.

4pm CT is 5pm ET. You would get the notification at the right time.

Rob

That’s the beauty of UTC. It’s timezone agnostic.  3:30 CT is 4:30 ET but they are all 9:30UTC.

Rob

Thanks Rob. That introduces an issue though. Let’s say I’m in CT at 2 PM. I use scheduleNotification() to schedule something at 4 PM CT which is 10 PM UTC. Now I get in my car and drive into a new time zone, ET. My notification was set for 10 PM UTC which is 5 PM ET, not the 4 PM I’m expecting. So I’m getting the notification an hour later than expected, because I moved the device into a new time zone.

Is there a way around this?

Currently, you have to calculate the difference between two time zones and subtract it.

local diff = os.difftime(os.time(), os.time(os.date("!\*t", os.time())))

Lua is relatively primitive about time zone management. You have to calculate datetime in seconds since Unix epoch time. I wrote some utility classes for datetime algebra, which were not well tested yet.

But if the device changes time zone, that doesn’t reschedule notifications, does it? It would still be scheduled under the old time zone. I’m guessing the only solution is creating a handler for time zone change events…

You are right, my solution didn’t handle time zone change.

It is quite difficult to manage time zone issue in local notifications since the notification event id will disappear when the app closes, no reliable tracker for the event. I think one server is needed for this issue, but it might be complex as the mobile app and the server have to sync notification events. I just ignore the issue in my app because time zone changes are infrequent for most end users. (Not a good model, sorry.)

I do the same thing. As you said, it rarely happens.

4pm CT is 5pm ET. You would get the notification at the right time.

Rob