Python Localize Time
Last month I wrote a telegram bot: Choke(@getAqiBot. I need to create a datetime
object from a string of hour and minute, then add time zone information to it and finally convert it to UTC time zone.
strftime()
function converts datetime
object to string. On the contrary, striptime()
converts a string to a datetime
object1. Note that we need to add today’s year, month and day, otherwise Python will use January 1, 1900. And that will give us an inaccurate result when we convert time zones later.
from datetime import datetime
hour_minute = "12:00"
time_string = datetime.now().strftime('%Y%m%d') + hour_minute
datetime_obj = datetime.strptime(time_string, '%Y%m%d%H:%M')
>>> datetime.strptime("12:00", "%H:%M")
datetime.datetime(1900, 1, 1, 12, 0)
>>> print(time_string)
2018030212:00
>>> print(datetime_obj)
2018-03-02 12:00:00
We can use pytz’s localize()
function to localize datetime
object.
from pytz import timezone
tzinfo = "Europe/Paris" # UTC+1
localized_datetime = timezone(tzinfo).localize(datetime_obj)
>>> print(localized_datetime)
2018-03-02 12:00:00+01:00
Using astimezone()
function to convert Paris time zone to UTC. Because I hosted the bot on Heroku, and Heroku uses UTC.
import pytz
utc_datetime = localized_datetime.astimezone(pytz.utc)
>>> print(utc_datetime)
2018-03-02 11:00:00+00:00
Finally, using time()
function to get time
object.
notification_time = utc_datetime.time()
>>> print(notification_time)
11:00:00
I didn’t notice that free Heroku dynos sleep after thrity minutes of inactivity2, so most daily notifications will never send. You can upgrade to Hobby or try Kaffeine.