#include "Time.h"
void Time::timerStart()
{
tBegin = clock();
}
double Time::timerStop()
{
tEnd = clock();
double elapsed_secs = double(tEnd - tBegin) / CLOCKS_PER_SEC;
return elapsed_secs;
}
string Time::getDate(char delimiter)
{
time_t t = time(0); //now
struct tm *now = localtime(&t);
ostringstream ss; //use iss or oss and NOT ss (bulkier)
ss << now->tm_mday << delimiter << (now->tm_mon + 1)
<< delimiter << (now->tm_year + 1900);
return ss.str();
}
string Time::getTime(char delimiter)
{
time_t t = time(0);
struct tm *now = localtime(&t);
ostringstream ss;
ss << now->tm_hour << delimiter << now->tm_min << delimiter << now->tm_sec;
return ss.str();
}
基本上我一直收到错误代码C4966&#39; localtime&#39;:此函数或变量可能不安全。请考虑使用localtime_s。要禁用折旧,请使用_CRT_SECURE_NO_WARNINGS。
我在预处理器定义中使用了_CRT_SECURE_NO_WARNINGS,它仍然给出了同样的错误。我对如何运行这个程序感到很遗憾,我花了几个小时在网上寻找解决方案,并试图自己解决这个问题。
答案 0 :(得分:1)
首先,将_CRT_SECURE_NO_WARNINGS
添加到Preprocessor
对我有用,如果没有,请尝试下面的其他解决方案:
解决方案1:
在文件开头定义_CRT_SECURE_NO_WARNINGS
:
#define _CRT_SECURE_NO_WARNINGS
#include <time.h>
解决方案2:
将4996
添加到:
YourProject->Properties->Configuration Properties->C/C++->Advanced->Disable Specific Warnings