How do you convert a datetime/timestamp from one timezone to another timezone?

If you know your origin timezone and the new timezone that you want to convert it to, it turns out to be very straightforward: Make two pytz.timezone objects, one for the current timezone and one for the new timezone e.g. pytz.timezone(“US/Pacific”). You can find a list of all official timezones in pytz library: import pytz; … Read more

Python datetime object show wrong timezone offset

See: http://bytes.com/topic/python/answers/676275-pytz-giving-incorrect-offset-timezone In the comments, someone proposes to use tzinfo.localize() instead of the datetime constructor, which does the trick. >>> tz = timezone(‘Asia/Kolkata’) >>> dt = tz.localize(datetime.datetime(2011, 6, 20, 0, 0, 0, 0)) >>> dt datetime.datetime(2011, 6, 20, 0, 0, tzinfo=<DstTzInfo ‘Asia/Kolkata’ IST+5:30:00 STD>) UPDATE: Actually, the official pytz website states that you should always … Read more

pytz and astimezone() cannot be applied to a naive datetime

For pytz timezones, use their .localize() method to turn a naive datetime object into one with a timezone: start_date = local_tz.localize(start_date) For timezones without a DST transition, the .replace() method to attach a timezone to a naive datetime object should normally also work: start_date = start_date.replace(tzinfo=local_tz) See the localized times and date arithmetic of the … Read more

How to add timezone into a naive datetime instance in python [duplicate]

Use tz.localize(d) to localize the instance. From the documentation: The first is to use the localize() method provided by the pytz library. This is used to localize a naive datetime (datetime with no timezone information): >>> loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0)) >>> print(loc_dt.strftime(fmt)) 2002-10-27 06:00:00 EST-0500 If you don’t use tz.localize(), but … Read more

Python pytz timezone function returns a timezone that is off by 9 minutes

Answer based on the answer by Carl Meyer in Google Groups Answer The reason for this difference, is that this is NOT the right way of converting a timezone agnostic datetime object to a timezone aware object. The explanation being: “A pytz timezone class does not represent a single offset from UTC, it represents a … Read more

Converting python datetime to timestamp and back in UTC still uses local timezone

To get a naive datetime object that represents time in UTC from “seconds since the epoch” timestamp: from datetime import datetime utc_dt = datetime.utcfromtimestamp(ts) If you want to get an aware datetime object for UTC timezone: import pytz aware_utc_dt = utc_dt.replace(tzinfo=pytz.utc) To convert it to some other timezone: tz = pytz.timezone(‘America/Montreal’) dt = aware_utc_dt.astimezone(tz) To … Read more

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