长期查看监控数据, 监控数据的时间戳格式是标准的unix时间戳, 查看费劲, 于是乎自己写了一个小程序
代码如下
#!/bin/env python
#coding:utf8
#unix时间戳转换
import sys, time, os, re
def com_judge():
‘‘‘
@判断执行程序是否后跟参数
‘‘‘
com_count = len(sys.argv)
if com_count == 2:
content = sys.argv[1]
return content
else:
output = "usage->> \n\tpython %s 1418239565\n\tpython %s timestamp_filename"
print output % (sys.argv[0],sys.argv[0])
exit(1)
def dispose(content):
if os.path.isfile(content):
with open(content) as timefile:
line_start=1
for read in timefile:
read = read.strip()
#判断文件中的时间戳格式是否正确
if re.match(r"\d{10,12}", read):
timeStamp =int(read)
#利用python time 模块进行时间戳转换
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print read + " : "+ otherStyleTime
line_start += 1
else:
print "line " + repr(line_start) + " ERROR: " + read
exit(1)
else:
input_time = content
#判断输入的时间戳格式是否正确
if re.match(r"\d{10,12}", input_time):
timeStamp =int(input_time)
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print input_time + " : "+ otherStyleTime
else:
print "UNIX TimeStamp Error : " + input_time
exit(1)
def main():
user_input = com_judge()
dispose(user_input)
if __name__ == ‘__main__‘:
main()
执行,可以直接带上时间戳执行

还可以把一坨坨的时间戳放到一个文件中

ok,完事,我是将这个程序直接放到了 /usr/bin/ 下, 给了权限, 使用起来更方便
原文:http://my.oschina.net/leeyd/blog/471796