Python string to Django timezone (aware datetime)

I know this is old but maybe will be helpful since I got into this situation as well: What about using make_aware() ? from datetime import datetime from django.utils.timezone import make_aware date=”22-05-2018″ aware = make_aware(datetime.strptime(date, ‘%d-%m-%Y’)) This will use the currently active timezone (activated by timezone.activate). If no timezone is activated explicitly, it would use … Read more

Python – Setting a datetime in a specific timezone (without UTC conversions)

There are at least two issues: you shouldn’t pass a timezone with non-fixed UTC offset such as “US/Pacific” as tzinfo parameter directly. You should use pytz.timezone(“US/Pacific”).localize() method instead .strftime(‘%s’) is not portable, it ignores tzinfo, and it always uses the local timezone. Use datetime.timestamp() or its analogs on older Python versions instead. To make a … Read more

datetime and timezone conversion with pytz – mind blowing behaviour

The documentation http://pytz.sourceforge.net/ states “Unfortunately using the tzinfo argument of the standard datetime constructors ‘does not work’ with pytz for many timezones.” The code: t = datetime( 2013, 5, 11, hour=11, minute=0, tzinfo=pytz.timezone(‘Europe/Warsaw’) ) doesn’t work according to this, instead you should use the localize method: t = pytz.timezone(‘Europe/Warsaw’).localize( datetime(2013, 5, 11, hour=11, minute=0))

How to make a datetime object aware (not naive)

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

How to get the common name for a pytz timezone eg. EST/EDT for America/New_York

Given a pytz timezone for a particular user(calculated from his offset), i want to display the common name for that timezone. I’m assuming people are more accustomed to seeing EST or PST instead of spelled out like America/NewYork. If you need this derived from a datetime object localized with pytz… >>> import pytz as tz … Read more

pytz utc conversion

Your first method seems to be the approved one, and should be DST-aware. You could shorten it a tiny bit, since pytz.utc = pytz.timezone(‘UTC’), but you knew that already 🙂 tz = timezone(‘US/Pacific’) def toUTC(d): return tz.normalize(tz.localize(d)).astimezone(pytz.utc) print “Test: “, datetime.datetime.utcnow(), ” = “, toUTC(datetime.datetime.now())

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