关于Visual Studio 2022的时间函数报错问题的解决办法

#include <iostream>
#include <time.h>
using namespace std;


int main()
{
time_t t = time(0);//返回从1970,1,1,00:00:00到现在的时间(毫秒)
char tmp[64];
strftime(tmp, sizeof(tmp), "%Y/%m/%d %X %A", localtime(&t));
cout << "现在的时间是:" << tmp;

return 0;
}

当我使用与学习视频相同的代码时,visual studio 2022报错: 错误 C4996 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

 

其中解决的办法是在前面加上#pragma warning( disable : 4996 ),这样可以忽略这个错误,即:

#include <iostream>
#include <time.h>
#pragma warning( disable : 4996 )
using namespace std;


int main()
{
time_t t = time(0);//返回从1970,1,1,00:00:00到现在的时间(毫秒)
char tmp[64];
strftime(tmp, sizeof(tmp), "%Y/%m/%d %X %A", localtime(&t));
cout << "现在的时间是:" << tmp;

return 0;
}

主要参考:【报错】运行C++程序的时候'localtime'报错而我想成为一个有趣的妞的博客-CSDN博客localtime 出错

 

posted on 2022-06-21 09:58  天上的白云  阅读(1089)  评论(0)    收藏  举报