Skip to content

Instantly share code, notes, and snippets.

@raidzero
Created March 5, 2014 23:25
Show Gist options
  • Save raidzero/9378923 to your computer and use it in GitHub Desktop.
Save raidzero/9378923 to your computer and use it in GitHub Desktop.
Print local time in any timezone by GMT offset (hour & minute)
#!/usr/bin/python
import time, sys, re
OFFSET = sys.argv[1] # get the time offset in hours
OPERATION = OFFSET[0] # get the first character in OFFSET
if OPERATION != "+" and OPERATION != "-": # we only accept + and - as valid operations
print "usage: %s +3.5 (for UTC +3.5 hours)" % sys.argv[0]
sys.exit(1)
OFFSET = float(re.search("(\d+(\.\d+)?)", OFFSET).group(1)) # pull out offset only, ignoring operator - digit plus an optional decimal then more digits
code = 'print time.strftime("%I:%M %p", time.localtime(time.mktime(time.gmtime()) ' + OPERATION + 'OFFSET * 60 * 60))'; exec code # build the code that prints the time based on operator then execute it
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment