linux 时间格式转换

1. time_t:类型变量,相当于long

2.tm:结构体类型
struct tm
{
  int tm_sec;            /* Seconds.    [0-60] (1 leap second) */
  int tm_min;            /* Minutes.    [0-59] */
  int tm_hour;            /* Hours.    [0-23] */
  int tm_mday;            /* Day.        [1-31] */
  int tm_mon;            /* Month.    [0-11] */
  int tm_year;            /* Year    - 1900.  */
  int tm_wday;            /* Day of week.    [0-6] */
  int tm_yday;            /* Days in year.[0-365]    */
  int tm_isdst;            /* DST.        [-1/0/1]*/
}

3.gettimeifday函数:获取本地时间转换为时间戳

timeval cur_time;
char buf[128];
gettimeofday(&cur_time,NULL);
printf("----------cur_time:%d.%d\n",cur_time.tv_sec,cur_time.tv_usec);

函数输出为:

 4.gmtime_r函数:将日历时间timep转换为用UTC时间表示的时间

timeval cur_time;
char buf[128];
gettimeofday(&cur_time,NULL);
printf("----------cur_time:%d.%d\n",cur_time.tv_sec,cur_time.tv_usec);
int time = cur_time.tv_sec;
struct tm t;
time_t epoch_time = (time_t)time;
gmtime_r(&epoch_time, &t);
printf("year:%d-min:%d-day:%d-hour:%d-min:%d-sec:%d\n",t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min,t.tm_sec);

 函数输出为:

 函数输出为从1900开始的年月日,为获得标准输出需要加上1900:

printf("year:%d-min:%d-day:%d-hour:%d-min:%d-sec:%d\n",t.tm_year+1900, t.tm_mon+1, t.tm_mday+1, t.tm_hour, t.tm_min,t.tm_sec);

 输出为:

5.strftime 将时间戳按格式输出

timeval cur_time;
char buf[128];
gettimeofday(&cur_time,NULL);
printf("----------cur_time:%d.%d\n",cur_time.tv_sec,cur_time.tv_usec);
int time = cur_time.tv_sec;
struct tm t;
time_t epoch_time = (time_t)time;
gmtime_r(&epoch_time, &t);
printf("year:%d-mon:%d-day:%d-hour:%d-min:%d-sec:%d\n",t.tm_year+1900, t.tm_mon+1, t.tm_mday+1, t.tm_hour, t.tm_min,t.tm_sec);
strftime(buf, 32,"%Y %m %d %H %M %S",&t);
printf("---------------times:%s\n",buf);
string cmd = "date;timedatectl";
system(cmd.c_str());

函数按格式输出时间,其中timedatectl可以看出系统时区,输出为UTC时间

6. localtime函数:将日历时间timep转换为用户指定的时区的时间

原型:struct tm *localtime(const time_t *calkptr) 

 timeval cur_time;
        char buf[128];
        gettimeofday(&cur_time,NULL);
        printf("----------cur_time:%d.%d\n",cur_time.tv_sec,cur_time.tv_usec);
        int time = cur_time.tv_sec;
        struct tm t;
        time_t epoch_time = (time_t)time;
        gmtime_r(&epoch_time, &t);
        printf("year:%d-mon:%d-day:%d-hour:%d-min:%d-sec:%d\n",t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min,t.tm_sec);
        strftime(buf, 32,"%Y %m %d %H %M %S",&t);
        printf("---------------times:%s\n",buf);
        struct tm * timenow = localtime(&epoch_time);
        strftime(buf, 128,"%Y %m %d %H %M %S", timenow);
        printf("---------------time:%s\n",buf);
        string cmd = "date;timedatectl";
        system(cmd.c_str());

经过localtime转换后输出为本地时区时间

 7.mktime:将tm结构转换为时间戳

timeval cur_time;
        char buf[128];
        gettimeofday(&cur_time,NULL);
        printf("----------cur_time:%d.%d\n",cur_time.tv_sec,cur_time.tv_usec);
        int time = cur_time.tv_sec;
        struct tm t;
        time_t epoch_time = (time_t)time;
        gmtime_r(&epoch_time, &t);
        printf("year:%d-mon:%d-day:%d-hour:%d-min:%d-sec:%d\n",t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min,t.tm_sec);
        strftime(buf, 32,"%Y %m %d %H %M %S",&t);
        printf("---------------times:%s\n",buf);
        struct tm * timenow = localtime(&epoch_time);
        strftime(buf, 128,"%Y %m %d %H %M %S", timenow);
        printf("---------------time:%s\n",buf);
        time_t tmp = mktime(timenow);
        printf("---------------mktime t:%d\n",tmp);
        string cmd = "date;timedatectl";
        system(cmd.c_str());

 8.ctime:将time_t输出为字符串,结合以上代码

std::cout << ctime(&epoch_time) << std::endl;

 9.difftime:计算两个time_t的时间差

time_t t2 = 1656259624;
printf("t1:%d, t2:%d, diff:%f\n",time,t2,difftime(time,t2));
printf("---------diff:%d\n",time-t2);

 

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值