Skip to content

Instantly share code, notes, and snippets.

@elowy01
Last active September 13, 2021 15:57
Show Gist options
  • Save elowy01/6453fe64ab70c0e5c7f4b1d64cab07ea to your computer and use it in GitHub Desktop.
Save elowy01/6453fe64ab70c0e5c7f4b1d64cab07ea to your computer and use it in GitHub Desktop.
# Convert a time in UTC to BTC
import pytz
from datetime import datetime
tz = pytz.timezone('UTC')
naive_time = datetime.strptime('2016-04-05 15:00', '%Y-%m-%d %H:%M')
tz_time = tz.localize(naive_time)
london_tz = pytz.timezone('Europe/London')
london_time = tz_time.astimezone(london_tz)
print(tz_time)
print(london_time)
# It prints out:
2016-04-05 15:00:00+00:00
2016-04-05 16:00:00+01:00
# Convert from BTC to UTC
import pytz
from datetime import datetime
tz = pytz.timezone('Europe/London')
naive_time = datetime.strptime('2016-04-05 16:00', '%Y-%m-%d %H:%M')
tz_time = tz.localize(naive_time)
utc_tz = pytz.timezone('UTC')
utc_time = tz_time.astimezone(utc_tz)
print(tz_time)
print(utc_time)
# It prints out:
2016-04-05 16:00:00+01:00
2016-04-05 15:00:00+00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment