【发布时间】:2013-01-28 19:14:34
【问题描述】:
我需要获取输入日期和时间的纪元时间,但是当我输入 2048(年)时,我得到的值非常大,“18446744073709551615”,这应该是不正确的..
当我输入像 2012(年)、2015(年)这样的日期时,它的纪元值是正确的,我需要为 2048(年)做任何更改
time_t get_unix_time(int,int,int,int,int,int,int);
int main()
{
unsigned long long m_process_date;
m_process_date = get_unix_time (12,31,2048,23,59,58,-1);
std::cout<<"\n m_process_date:"<< m_process_date<<std::endl;
return 1;
}
time_t get_unix_time( int month,
int day,
int year,
int hour,
int minute,
int second,
int dst )
{
tm ts;
ts.tm_mon = month - 1;
ts.tm_mday = day;
if( year < 100 )
ts.tm_year = year + 100;
else
ts.tm_year = year - 1900;
ts.tm_hour = hour;
ts.tm_min = minute;
ts.tm_sec = second;
ts.tm_isdst = dst;
return mktime( &ts );
}
【问题讨论】:
-
从 1970 年开始,时间通常仍以带符号的 32 位秒数来衡量。这意味着 2038 年的时间将溢出。参见例如the year 2038 problem.
-
...我正在打字。 :)
-
您的平台是否支持 mktime64(mktime 的 64 位版本)? stackoverflow.com/questions/7914368/…
-
如果您有选择,编译为 64 位。较新的 64 位运行时将 time_t 值存储为 64 位无符号整数。