将time_t转换为int [英] Converting time_t to int

查看:4760
本文介绍了将time_t转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将给定时间转换成epoch(time_t),反之亦然。



更新



  epoch_strt.tm_sec = 0; 
epoch_strt.tm_min = 0;
epoch_strt.tm_hour = 0;
epoch_strt.tm_mday = 1;
epoch_strt.tm_mon = 1;
epoch_strt.tm_year = 70;
epoch_strt.tm_isdst = -1;

double nsecs = difftime(curtime,basetime); //系统当前时间,我将其转换为struct tm

$

解决方案

您应该将其转换为 long int 而不是 int

  long int t = static_cast< long int> time(NULL); 

int 可能不足例如,在我的平台上, time_t typedef __ int64


I want to convert a given time into epoch(time_t) and vice versa. can anyone tell what is the routine or algorithm for this?

Thanks

Update

 epoch_strt.tm_sec = 0;
 epoch_strt.tm_min = 0;
 epoch_strt.tm_hour = 0;
epoch_strt.tm_mday = 1;
epoch_strt.tm_mon = 1;
epoch_strt.tm_year = 70;
 epoch_strt.tm_isdst = -1;

double nsecs = difftime(curtime, basetime);//current time from system, I converrting it to struct tm

but this always return 32399 for some reason.

解决方案

You should cast it to a long int instead of int.

long int t = static_cast<long int> time(NULL);

An int might not be enough to hold the time, for example, on my platform, time_t is a typedef of __int64.

这篇关于将time_t转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆