So I have a code that receives relevant information for a mobile device, such as phone number, name, and message, through which it can send an SMS message. Also, there is a function which delays the SMS. So a user can choose to send a message at a later time using a drop-down menu. Furthermore the delay function checks whether the time at which the message is to be sent between 12:00am to 8:00am, and if so then the default of 8:00am is returned so the receiver will get the message at 8:00am at the earliest. However the problem I am facing is that the time delay is in terms of Mountain Daylight Time (Canada/US), which means that people living in the PDT timezone will receive the message at 7:00am (hour too early) while people in EDT receive the message at 10:00am (2 hours too late). So I was wondering if there is some python code that would potentially detect which timezone the receiver is located in so that he/she receives the message on time (if I select 8:00am in Nevada then the receiver in California will receive the message at 8:00am PDT), given the area in which the receiver is located (eg. the code is given that the receiver is located in California).
Any idea on how to approach this problem?
Thanks!
Quick and Dirty: You could dodge the problem by getting the user to say after how many hours/minutes the event should occur. Then it doesn’t matter what their local time zone is.
Better: If you have the ability to get the local time on the phone, as well as the UTC time (either from the phone or from your server), that will tell you the UTC offset of the phone. You can get away with that most of the time. But UTC offset is NOT the same thing as a local time zone. Near a daylight-savings or summer-time boundary, ideas like “the same time tomorrow” break when you assume that the UTC offset is the same as the time zone.
Best: For supporting political time zone policies correctly, there is no substitute for the Olson database. Using Olson would imply that your app has a configuration item for the user to set their timezone (examples:
America/New_YorkandEtc/UTC). You might choose defaults for this based on the UTC offset of the phone and the mobile number (using lookup tables as detailed in the other answers) and be correct most of the time.In Python the
pytzmodule gives you a very clean API for using the Olson database to work withdatetimeobjects.EDIT: This function will convert Unix time to a localized
datetimeobject, given a timezone object: