localtime时间获取

来源:互联网 发布:精准数据营销 编辑:程序博客网 时间:2024/05/22 00:30

在linux环境,应用需要记录KPI的数据统计,代码中获取时间函数,localtime(),asctime查看,获取事件为东八区的时间,与当前系统时间一致,而从另一台服务器远程ssh 启动此应用,获取的时间为UTC 的时间,并未换算为东八区时间;

# ssh  10.118.203.140 "/time"
root@10.118.203.140's password:
asctime(now) is  Mon Dec 18 00:43:25 2017
# ./time
asctime(now) is  Mon Dec 18 13:43:34 2017


代码示例:

#include <stdio.h>
#include <string>
#include <time.h>
#include <string.h>
#include <iostream>

using namespace std;
using std::string;

int main()
{
    time_t t=time(NULL);
    struct tm *now;
    now = localtime(&t);
    cout << "asctime(now) is  " << asctime(now) << endl;
    return 0;
}


阅读全文
0 0