- 论坛徽章:
- 0
|
string dateTimeStr = "2038-12-31 23:59:59"; string formaterStr = "%Y-%m-%d %H:%M:%S";
int year,month,day,hour,minutes,secondsPar;
sscanf(dateTimeStr.c_str(),formaterStr.c_str(),&year,&month,&day,&hour,&minutes,&secondsPar);
if(year <1900) year = 1970;
if(month <0) month = 1;
if(day <0) day = 1;
if(hour <0) hour = 0;
if(minutes <0) minutes = 0;
if(secondsPar <0) secondsPar = 0;
tm t;
t.tm_year = year-1900;
cout<<"year"<<year-1900<<endl;
t.tm_mon = month-1;
t.tm_mday = day;
t.tm_hour = hour+1;
t.tm_min = minutes;
t.tm_sec = secondsPar;
time_t seconds = mktime(&t);
cout<<"seconds"<<seconds<<endl;
tm date = *localtime(&seconds);
char *datetime;
datetime = asctime(&date);
cout<<datetime<<endl;
==============================
问题: 当dateTimeStr = "2037-12-31 23:59:59";
year137
seconds2145887999
Thu Dec 31 23:59:59 2037
================================================================
当dateTimeStr = "2038-12-31 23:59:59";
year138
seconds-1
Thu Jan 1 07:59:59 1970
是不是超出范围了,有什么解决方法吗? |
|