-
-
Save Chitrank-Dixit/2d013330468df4dcee84452e05352a9f to your computer and use it in GitHub Desktop.
Epoch Converter in Python
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
#!/usr/bin/python | |
import time, argparse | |
def convert(ctx): | |
inputstr = str(ctx.Epoch) | |
if len(inputstr) > 10: | |
secs = inputstr[:10] | |
msecs = inputstr[10:] | |
else: | |
secs = inputstr | |
msecs = "" | |
utc = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(int(secs))) | |
local = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(secs))) | |
if msecs != "": | |
utc = utc + "." + msecs | |
local = local + "." + msecs | |
utc = utc + " UTC" | |
local = local + " " + time.tzname[time.localtime().tm_isdst] | |
if ctx.utc == False and ctx.local == False: | |
ctx.utc = ctx.local = True | |
if ctx.utc == True: | |
print utc | |
if ctx.local == True: | |
print local | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='convert Epoch time to a timestamp') | |
parser.add_argument('Epoch', action="store", type=int, help='Epoch time in seconds or milliseconds') | |
parser.add_argument('-u', '--utc', action="store_true", dest='utc', help='show timestamp in UTC time', default=False) | |
parser.add_argument('-l', '--local', action="store_true", dest='local', help='show timestamp in local time', default=False) | |
parser.add_argument('--version', action="version", version='%(prog)s 20140829.1') | |
ctx = parser.parse_args() | |
convert(ctx) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment