Linux获取系统时间 函数

static gint64 get_nsec(){
    gint64 val = 0;
    struct timespec ts = {0};
    clock_gettime(CLOCK_REALTIME, &ts);
    val = ts.tv_sec * 1000000000 + ts.tv_nsec;
    return val;
}
static gint64 get_usec(){
    gint64 val = 0;
    struct timeval tv;
    gettimeofday(&tv, NULL);
    val = tv.tv_sec * 1000000 + tv.tv_usec;
    return val;
}
static gint64 get_msec(){
    gint64 val = 0;
    struct timeval tv;
    gettimeofday(&tv, NULL);
    val = tv.tv_sec * 1000 + tv.tv_usec/1000;
    return val;
}

获取的常数时间转为localtime

#!/usr/bin/env python
#coding=utf-8

import os
import sys 
import time
import traceback

if __name__ == "__main__":
    lines = []; 
    if len(sys.argv) >= 2:
        if os.path.exists(sys.argv[1]):
            fp = open(sys.argv[1], "r");
            lines = fp.readlines();
            fp.close();
        else:
            longtime = 0;
            try:
                longtime = int(sys.argv[1]);
            except:
                traceback.print_exc();
                exit(0)
            if longtime != 0:
                lines.append(sys.argv[1]);
            else:
                print "File:%s NOT exist!" % (sys.argv[1])
                exit(0);
    else:
        lines = sys.stdin.readlines();

    for line in lines:
        longtime = 0;
        try:
            longtime = int(line);
        except:
            traceback.print_exc();
            exit(0);
    
        tm0 = time.localtime(longtime);
        strtime = time.strftime('%Y%m%d %02H:%02M:%02S', tm0).strip();

       print "##: %d ==> %s" % (longtime, strtime)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值