一个unix时间戳转换的小程序

长期查看监控数据, 监控数据的时间戳格式是标准的unix时间戳, 查看费劲, 于是乎自己写了一个小程序

代码如下

#!/bin/env python
#coding:utf8
#########################################################################
#
#File name: check_http_status.py
#Description: unix时间戳转换
#Author:pangli
#mail:artie_lee@163.com
#
#########################################################################

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()

执行,可以直接带上时间戳执行

093059_fwuP_1395186.png

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

093321_186Y_1395186.png

ok,完事,我是将这个程序直接放到了 /usr/bin/ 下, 给了权限, 使用起来更方便

转载于:https://my.oschina.net/leeyd/blog/471796

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值