我写了一个 datediff in days 函数,它返回两个日期的天数(输入格式为 20100810)
运行程序后,我的输入字符串被损坏,无法理解我在哪里做错了。
我在 AIX 上运行它,当我在 Redhat 上运行它时,字符串没有损坏。
以下是代码
#include <iostream>
#include <ctime>
#include <sstream>
using namespace std;
int GetYear(std::string dateStr)
{
std::string year = dateStr.substr (0,4);
int sYr = atoi(year.c_str());
//std::cout <<"<<<GetYear dateStr:"<< dateStr << " yearStr:"<< year <<", int sYr:" << sYr << std::endl;
return sYr;
}
int GetMonth(std::string dateStr)
{
//std::cout <<">>>GetMonth dateStr:"<< dateStr << std::endl;
int sMn =0;
std::string mm = dateStr.substr (4,2);
sMn = atoi(mm.c_str());
// std::cout <<"<<<GetMonth dateStr:"<< dateStr <<"MonthStr:"<< mm <<", int MM:" << sMn << std::endl;
return sMn;
}
int GetDay(std::string dateStr)
{
//std::cout << ">>>GetDay dateStr:"<< dateStr << std::endl;
int sDy = 0;
std::string dd = dateStr.substr (6,2);
sDy = atoi(dd.c_str());
//std::cout << "<<<GetDay dateStr:"<< dateStr<<" DayStr:"<< dd <<", int DD:" << sDy << std::endl;
return sDy;
}
int dateDiff_Days (std::string startStr,std::string endStr){
std::cout << ">>>dateDiff_Days" << std::endl;
int sYr;
int eYr;
int sMn;
int eMn;
int sDy;
int eDy;
time_t end;
time_t start;
struct tm *startDate;
struct tm *endDate;
int dif;
int i =0;
//Move over the start date to string and convert string to int
sYr = GetYear(startStr);
sMn = GetMonth(startStr);
sDy = GetDay(startStr);
//Move over the end date to string and convert string to int
eYr = GetYear(endStr);
eMn = GetMonth(endStr);
eDy = GetDay(endStr);
//Move information into startDate structure
start = time(0);
startDate = localtime (&start);
startDate->tm_year = (sYr-1900); //Years since 1900
startDate->tm_mday = sDy; //Day of the month: 1-31
startDate->tm_mon = sMn-1; //Months since Jan: 0-11
//Leave default zero's in for the rest
startDate->tm_sec = '0';
startDate->tm_min = '0';
startDate->tm_hour = '0';
startDate->tm_wday = '0';
startDate->tm_yday = '0';
startDate->tm_isdst = '0';
start = mktime (startDate);
//Move info into endDate structure, same as startDate.
end = time(0);
endDate = localtime (&end);
endDate->tm_year = (eYr-1900);
endDate->tm_mday = eDy;
endDate->tm_mon = eMn-1;
endDate->tm_sec = '0';
endDate->tm_min = '0';
endDate->tm_hour = '0';
endDate->tm_wday = '0';
endDate->tm_yday = '0';
endDate->tm_isdst = '0';
end = mktime (endDate);
dif = difftime (end, start);
dif = dif/86400;
return dif;
}
int main (){
std::string dt1 = "20100810";
std::string dt2 = "20100810";
std::cout << ">>>GetLaterDate before dt1="<< dt1 <<" dt2="<< dt2 << std::endl;
std::string tempDt1 (dt1);
std::string tempDt2 (dt2);
int i = dateDiff_Days(dt1,dt2);
std::cout << ">>>GetLaterDate before dt1="<< dt1 <<" dt2="<< dt2 << std::endl;
std::cout << ">>>GetLaterDate before tempDt1="<< tempDt1 <<" tempDt2="<< tempDt2 << std::endl;
return 0;
}
输出:
>>>GetLaterDate before dt1=20100810 dt2=20100810 >>>dateDiff_Days >>>GetLaterDate before dt1=NULLNULL dt2=NULLNULL >>>getLaterDate before tempDt1=NULLNULL tempDt2=NULLNULL