How do I get the UTC time of “midnight” for a given timezone?

I think you can shave off a few method calls if you do it like this: >>> from datetime import datetime >>> datetime.now(pytz.timezone(“Australia/Melbourne”)) \ .replace(hour=0, minute=0, second=0, microsecond=0) \ .astimezone(pytz.utc) BUT… there is a bigger problem than aesthetics in your code: it will give the wrong result on the day of the switch to or … Read more

How to get system timezone setting and pass it to pytz.timezone?

tzlocal module returns pytz tzinfo’s object corresponding to the local timezone: import time from datetime import datetime import pytz # $ pip install pytz from tzlocal import get_localzone # $ pip install tzlocal # get local timezone local_tz = get_localzone() # test it # utc_now, now = datetime.utcnow(), datetime.now() ts = time.time() utc_now, now = … Read more

How can I remove a pytz timezone from a datetime object?

To remove a timezone (tzinfo) from a datetime object: # dt_tz is a datetime.datetime object dt = dt_tz.replace(tzinfo=None) If you are using a library like arrow, then you can remove timezone by simply converting an arrow object to to a datetime object, then doing the same thing as the example above. # <Arrow [2014-10-09T10:56:09.347444-07:00]> arrowObj … Read more

How to make a timezone aware datetime object

In general, to make a naive datetime timezone-aware, use the localize method: import datetime import pytz unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0) aware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC) now_aware = pytz.utc.localize(unaware) assert aware == now_aware For the UTC timezone, it is not really necessary to use localize since … Read more

Is there a list of Pytz Timezones?

You can list all the available timezones with pytz.all_timezones: In [40]: import pytz In [41]: pytz.all_timezones Out[42]: [‘Africa/Abidjan’, ‘Africa/Accra’, ‘Africa/Addis_Ababa’, …] There is also pytz.common_timezones: In [45]: len(pytz.common_timezones) Out[45]: 403 In [46]: len(pytz.all_timezones) Out[46]: 563

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)