A specific bank has branches in all major cities in the world. They all open at 10:00 AM local time. If within a timezone that uses DST, then of course the local opening time also follows the DST-adjusted time. So how do I go from the local time to the utc time.
What I need is a function to_utc(localdt, tz)
like this:
Arguments:
- localdt: localtime, as naive datetime object, DST-adjusted
- tz: timezone in the TZ-format, e.g. 'Europe/Berlin'
Returns:
- datetime object, in UTC, timezone-aware
EDIT:
The biggest challenge is to detect whether the local time is in a period with DST, which also means that it is DST adjusted.
For 'Europe/Berlin' which has +1 DST in the summer:
- Jan 1st 10:00 => Jan 1st 9:00 UTC
- July 1st 10:00 => July 1st 8:00 UTC
For 'Africa/Lagos' which has no DST:
- Jan 1st 10:00 => Jan 1st 9:00 UTC
- July 1st 10:00 => July 1st 9:00 UTC
Using pytz, and in particular its localize method:
yields
New Version. This does what you want -- takes a naive datetime and a timezone and returns a UTC datetime.