views:

58

answers:

2

i have users from all timezones, and i want to send out alerts at around 8AM in each users respective timezone.

i need a python script that runs every hour [in a cron job] and i need to find out at which timezone it is 8AM right now, and i can use that info to select the users that have to receive the alerts.

how do i go about doing this? there seems to be gmt+14 to gmt-12 that is 27 timezones, and there are only 24 hours in a day!

A: 

Some time zones are in half hour increments, for example Newfoundland Time is UTC-3:30. You should be able to get all the offsets and do the appropriate math to get all the notification times. I would do the scheduling in advance though, otherwise your notifications may be wrong on the daylight savings day turn-overs, etc. So every 24 hours, you would create a list of times to send notifications for the next day and instruct your scheduler appropriately.

Dana the Sane
or even quarter hours (e.g. Nepal)
Mark
A perfect example of why we should rely on libraries to find out about corner cases ;)
Dana the Sane
do you know any library that would help this case?
kevin
i dont mind sending in the half hour incremnts or quarter hour increments with the hourly. for ex UTC-3:30 can go with UTC-3
kevin
+1  A: 

Python defines a tzinfo class that gives you the offset of a time zone, but it doesn't provide any concrete implementation of the class. There are a few implementations available, I've used python-dateutil successfully. Obviously you'll need a time zone for each user; at the hourly (or half-hourly) run, take the current UTC time and set its tzinfo member to the UTZ timezone; then use the astimezone function to convert to each user's time zone in turn, and compare to some range around 8:00.

Mark Ransom
Half hourly is probably sufficient, but if you really want to be accurate about it, you'll have to do quarterly-hourly, at least whenever Nepal is passing by the 8:00 window. They didn't want to show favoritism to india or china, so they went in between.
jcdyer