Created
April 4, 2024 14:21
-
-
Save Darkflib/7d6850751b9e2335d5b3ca8d6b82d224 to your computer and use it in GitHub Desktop.
TZ examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BST | |
2024-04-04 14:20:19.982576+00:00 | |
2024-04-04 14:20:19 | |
UTC | |
2024-04-04 14:20:19.982576+00:00 | |
2024-04-04 14:20:19 UTC | |
2024-04-04 10:20:19.982576-04:00 | |
2024-04-04 10:20:19 EDT | |
2024-04-04 15:20:19.982576+01:00 | |
2024-04-04 15:20:19 BST | |
2024-04-04 19:50:19.982576+05:30 | |
2024-04-04 19:50:19 IST | |
2024-04-04 15:00:00+01:00 | |
2024-04-04 10:00:00-04:00 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from pytz import timezone | |
# get the current timezone | |
print(datetime.datetime.now().astimezone().tzinfo) | |
# get the current time in UTC | |
now = datetime.datetime.now().astimezone(datetime.timezone.utc) | |
# print the current time | |
print(now) | |
# print the current time in a specific format | |
print(now.strftime("%Y-%m-%d %H:%M:%S")) | |
# check if the current timezone is set | |
print(now.tzinfo) | |
# OMG, it's not set! Let's set it to UTC | |
now_utc = now.replace(tzinfo=datetime.timezone.utc) | |
print(now_utc) | |
# print the current time in a specific format with the timezone | |
print(now_utc.strftime("%Y-%m-%d %H:%M:%S %Z")) | |
# set the timezone to a specific timezone | |
now_eastern = now_utc.astimezone(timezone('US/Eastern')) | |
print(now_eastern) | |
# print the current time in a specific format with the timezone | |
print(now_eastern.strftime("%Y-%m-%d %H:%M:%S %Z")) | |
# set the timezone to a specific timezone | |
now_london = now_utc.astimezone(timezone('Europe/London')) | |
print(now_london) | |
# print the current time in a specific format with the timezone | |
print(now_london.strftime("%Y-%m-%d %H:%M:%S %Z")) | |
# set the timezone to a specific timezone | |
now_india = now_utc.astimezone(timezone('Asia/Kolkata')) | |
print(now_india) | |
# print the current time in a specific format with the timezone | |
print(now_india.strftime("%Y-%m-%d %H:%M:%S %Z")) | |
# get the time of 2pm UTC in London | |
london = timezone('Europe/London') | |
london_time = now_utc.replace(hour=14, minute=0, second=0, microsecond=0).astimezone(london) | |
print(london_time) | |
# get the time of 2pm UTC in Miami | |
miami = timezone('US/Eastern') | |
miami_time = now_utc.replace(hour=14, minute=0, second=0, microsecond=0).astimezone(miami) | |
print(miami_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment