1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| # encoding=utf8 import time import sys
useage = """ useage: time (时间戳|时间字符串)
eg: ➜ time 1553671750 2019-03-27 15:29:10
➜ time "2019-03-27 15:29:10" time_format: 2019-03-27 15:29:10 seconds: 1553671750 millseconds: 1553671750000
➜ time "2019-03-27" time_format: 2019-03-27 seconds: 1553616000 millseconds: 1553616000000 """
if __name__ == "__main__": if len(sys.argv) == 2: try: timeStr = sys.argv[1] if timeStr.isdigit(): if len(timeStr) > 10: timeStruct = time.localtime(int(sys.argv[1]) / 1000) else: timeStruct = time.localtime(int(sys.argv[1])) print "timestamp:", timeStr print "time_format:", time.strftime("%Y-%m-%d %H:%M:%S", timeStruct) elif timeStr.find("-") > 0 and timeStr.find(":") > 0 and len(timeStr) == 19: timeStruct = time.strptime(timeStr, "%Y-%m-%d %H:%M:%S") result = int(time.mktime(timeStruct)) print "time_format:", timeStr print "seconds:", result, "\tmillseconds:", result * 1000 elif timeStr.find("-") > 0 and len(timeStr) == 10: timeStruct = time.strptime(timeStr, "%Y-%m-%d") result = int(time.mktime(timeStruct)) print "time_format:", timeStr print "seconds:", result, "\tmillseconds:", result * 1000 else: print useage except: print "error", useage elif len(sys.argv) == 1: nowTime = int(time.time()) timeStruct = time.localtime(nowTime) print "current time_format:", time.strftime("%Y-%m-%d %H:%M:%S", timeStruct) print "seconds:", nowTime, "\tmillseconds:", nowTime * 1000 else: print useage
|