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

Get timezone by Country and Region

Finally, after long research, I’ve found the solution. It may not be much accurate, but it does not require the dependency of remote service. I’ve found that maxmind’s database provides mapping file between regions and time zones from Olsen database (aka tz database): http://www.maxmind.com/timezone.txt The second step was to convert Olsen timezones in .NET timezones. … Read more

Location based Timezone Retrieval

This depends on information the “location” contains? You’d somehow need to map the location to a timezone name, preferably the Olson style timezone names, because they are more detailed and easier to map, as they are locations themselves. If it’s an approximate addres (like country and city or so) then several geolocation services do include … Read more

Why doesn’t pytz localize() produce a datetime object with tzinfo matching the tz object that localized it?

They are the same time zone – “Europe/Berlin”. When you are printing them, the output includes the abbreviation and offset that applies at that particular point in time. If you examine the tz data sources, you’ll see: # Zone NAME GMTOFF RULES FORMAT [UNTIL] Zone Europe/Berlin 0:53:28 – LMT 1893 Apr 1:00 C-Eur CE%sT 1945 … Read more

Does ConvertTimeFromUtc() and ToUniversalTime() handle DST?

Yes. ConvertTimeFromUtc will automatically handle daylight saving time adjustments, as long as the time zone that you are targeting uses daylight saving time. From the MSDN documentation: When performing the conversion, the ConvertTimeFromUtc method applies any adjustment rules in effect in the destinationTimeZone time zone. You should not try to add an additional hour in … Read more