64,654
社区成员
发帖
与我相关
我的任务
分享
我写这个:timeinfo=localtime( &rawtime );总是出现这样的警告,
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.
当我改成 timeinfo=localtime_s( &rawtime ); 时,又出现
error C2660: “localtime_s”: 函数不接受 1 个参数,,
---------------------------
我查了查msdn,
errno_t localtime_s(
struct tm* _tm,
const time_t *time
);
Parameters
_tm
Pointer to the time structure to be filled in.
time
Pointer to the stored time.
---------------
把代码改成
localtime_s( timeinfo,&rawtime );后,又有警告说
timeinfo未初始化。。。
晕了。。这不是一个个别的现象,我在用c里面其它的函数时,也出过这种警告,加了
_s后,也是说参数不匹配,,例如strcpy
求教高手,解释为什么出现这种警告,该怎么消除?
// crt_localtime_s.c
/* This program uses _time64 to get the current time
* and then uses _localtime64_s() to convert this time to a structure
* representing the local time. The program converts the result
* from a 24-hour clock to a 12-hour clock and determines the
* proper extension (AM or PM).
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
int main( void )
{
struct tm newtime;
char am_pm[] = "AM";
__time64_t long_time;
char timebuf[26];
errno_t err;
// Get time as 64-bit integer.
_time64( &long_time );
// Convert to local time.
err = _localtime64_s( &newtime, &long_time );
if (err)
{
printf("Invalid argument to _localtime64_s.");
exit(1);
}
if( newtime.tm_hour > 12 ) // Set up extension.
strcpy_s( am_pm, sizeof(am_pm), "PM" );
if( newtime.tm_hour > 12 ) // Convert from 24-hour
newtime.tm_hour -= 12; // to 12-hour clock.
if( newtime.tm_hour == 0 ) // Set hour to 12 if midnight.
newtime.tm_hour = 12;
// Convert to an ASCII representation.
err = asctime_s(timebuf, 26, &newtime);
if (err)
{
printf("Invalid argument to asctime_s.");
exit(1);
}
printf( "%.19s %s\n", timebuf, am_pm );
}