Skip to content

Instantly share code, notes, and snippets.

@bashkirtsevich
Created December 20, 2021 19:20
Show Gist options
  • Save bashkirtsevich/0c78797946158be97027f1c59df46ab1 to your computer and use it in GitHub Desktop.
Save bashkirtsevich/0c78797946158be97027f1c59df46ab1 to your computer and use it in GitHub Desktop.
UTC Timezone Diff
from datetime import datetime
from pytz import UTC
from pytz import timezone
def utc_tz_diff(tz):
diff = (t1 := UTC.localize(now := datetime.utcnow())) - (t2 := timezone(tz).localize(now).astimezone(UTC))
return f"{'+' if t1 > t2 else '-'}{diff.seconds // 3600:02}:{diff.seconds // 60 % 60:02}" if diff.seconds else "Z"
DATETIME_FORMAT = f"%Y-%m-%dT%H:%M:%S{utc_tz_diff(TIME_ZONE)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment