Skip to content

Instantly share code, notes, and snippets.

@alvarobartt
Created April 21, 2020 11:18
Show Gist options
  • Save alvarobartt/b4b709a195ca919af190cc3b27cd92f6 to your computer and use it in GitHub Desktop.
Save alvarobartt/b4b709a195ca919af190cc3b27cd92f6 to your computer and use it in GitHub Desktop.
Calculate GMT Time Zone using Python
# Copyright 2020 Alvaro Bartolome @ alvarobartt in GitHub
# ____ ___ ___ ______ ______ ____ ___ ___ ___ _____ ___ ____ ___
# / || | || | | || || | | / _]| | / \ | \ / _]
# | __|| _ _ || | | | | | | _ _ | / [_ |__/ || || _ | / [_
# | | || \_/ ||_| |_| |_| |_| | | | \_/ || _]| __|| O || | || _]
# | |_ || | | | | | | | | | | || [_ | / || || | || [_
# | || | | | | | | | | | | || || || || | || |
# |___,_||___|___| |__| |__| |____||___|___||_____||_____| \___/ |__|__||_____|
# __ ____ _ __ __ __ _ ____ ______ ___ ____
# / ] / || | / ]| | || | / || | / \ | \
# / / | o || | / / | | || | | o || || || D )
# / / | || |___ / / | | || |___ | ||_| |_|| O || /
# / \_ | _ || |/ \_ | : || || _ | | | | || \
# \ || | || |\ || || || | | | | | || . \
# \____||__|__||_____| \____| \__,_||_____||__|__| |__| \___/ |__|\_|
# First of all, calculate the difference between your local date and the GMT date (which corresponds to the GMT 00:00 timezone)
diff = datetime.strptime(strftime('%d/%m/%Y %H:%M', localtime()), '%d/%m/%Y %H:%M') - \
datetime.strptime(strftime('%d/%m/%Y %H:%M', gmtime()), '%d/%m/%Y %H:%M')
# Then calculate both the hour and minutes difference between both datetime.datetime object, which now is a datetime.timedelta object
hour_diff = int(diff.total_seconds() / 3600)
min_diff = int(diff.total_seconds() % 3600) * 60
if hour_diff != 0:
time_zone = "GMT " + ('+' if hour_diff > 0 else '') + str(hour_diff) + ":" + ('00' if min_diff < 30 else '30')
else:
time_zone = "GMT 00:00"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment