How to make a timezone aware datetime object in Python?

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

Timezone conversion in php

You can use the datetime object or their function aliases for this: Example (abridged from PHP Manual) date_default_timezone_set(‘Europe/London’); $datetime = new DateTime(‘2008-08-03 12:35:23’); echo $datetime->format(‘Y-m-d H:i:s’) . “\n”; $la_time = new DateTimeZone(‘America/Los_Angeles’); $datetime->setTimezone($la_time); echo $datetime->format(‘Y-m-d H:i:s’); Edit regarding comments but i cannt use this method because i need to show date in different time zones … Read more

Parse DateTime with time zone of form PST/CEST/UTC/etc

AFAIK the time zone abbreviations are not recognized. However if you replace the abbreviation with the time zone offset, it will be OK. E.g.: DateTime dt1 = DateTime.ParseExact(“24-okt-08 21:09:06 CEST”.Replace(“CEST”, “+2”), “dd-MMM-yy HH:mm:ss z”, culture); DateTime dt2 = DateTime.ParseExact(“24-okt-08 21:09:06 CEST”.Replace(“CEST”, “+02”), “dd-MMM-yy HH:mm:ss zz”, culture); DateTime dt3 = DateTime.ParseExact(“24-okt-08 21:09:06 CEST”.Replace(“CEST”, “+02:00”), “dd-MMM-yy HH:mm:ss … Read more

Find if 24 hrs have passed between datetimes

If last_updated is a naive datetime object representing the time in UTC: from datetime import datetime, timedelta if (datetime.utcnow() – last_updated) > timedelta(hours=24): # more than 24 hours passed If last_updated is the local time (naive (timezone-unaware) datetime object): import time DAY = 86400 now = time.time() then = time.mktime(last_updated.timetuple()) if (now – then) > … Read more