c++ 获取系统时间减去天数
//计算日期节点 nSaveDays为天数
CTime tmDeleteday=CTime::GetCurrentTime() - CTimeSpan(nSaveDays, 0, 0, 0 );//获取系统当前时间 并 减去相应的天数得到 想要的时间节点
CString strDeleteday;
strDeleteday.Format("%04d-%02d-%02d", tmDeleteday.GetYear(), tmDeleteday.GetMonth(), tmDeleteday.GetDay());//格式化为想要的形式
下面是一个根据配置文件删除数据库中ABC表中超过设定天数的数据栗子:
1 //删除数据库中ABCInfo表中超过设定天数的数据 2 short CPeristentData::DeleteABCInfo() 3 { 4 LOGINFO("CPeristentData::DeleteABCInfobegin"); 5 6 //从配置文件获取天数 7 int nSaveDays = GetPrivateProfileInt("DELETESNRINFO","SaveDays",0,INIFILE);//INIFILE为宏定义配置文件路径 8 9 if (nSaveDays <= 0) 10 { 11 nSaveDays = 90;//默认为90天 12 WritePrivateProfileString("DELETEABCINFO","SaveDays","90",INIFILE);//没有则写入配置文件,以便于维护 13 } 14 15 //计算删除的日期节点 16 CTime tmDeleteday=CTime::GetCurrentTime() - CTimeSpan(nSaveDays, 0, 0, 0 ); 17 CString strDeleteday; 18 strDeleteday.Format("%04d-%02d-%02d", tmDeleteday.GetYear(), tmDeleteday.GetMonth(), tmDeleteday.GetDay()); 19 20 if (!OpenDatabase())//是否打开了数据库 21 { 22 return -2; 23 } 24 25 char szSql[1024]; 26 //delete from ABCInfo where OperationTime < '2018-04-01' 27 sprintf(szSql, "delete from ABCInfo \ 28 where OperationTime<\'%s\'", 29 strDeleteday); 30 31 if(!m_Sqlite.ExcuteNonQuery(szSql))//数据库操作 32 { 33 m_Sqlite.CommitTransaction(); 34 LOGINFO("ExcuteNonQuery Result: Error---[%s]",szSql); 35 return -3; 36 } 37 38 LOGINFO("CPeristentData::DeleteABCInfo end"); 39 40 return 0; 41 }