实在没招了,关于localtime

wudicainiao2 2019-08-10 10:01:10
开发环境vc2013,本人有个写数据库函数,需要反复将时间数据写入数据库中,用了别人写的c文件time_util.c,该文件中有函数使用到了localtime,原来是:
time_t timeT = (time_t)second;
struct tm *ptime;
ptime=localtime(&timeT);
运行中发现,随着不断循环写入,程序容易崩溃,原因是ptime有时候经常返回为NULL;
网上查找,可能是localtime函数问题,遂改成
time_t timeT = (time_t)second;
struct tm ptime;
localtime_s(&ptime, &timeT);

编译是出现LNK2028,LNK2019和LNK1120错误
错误 8 error LNK2028: 无法解析的标记(0A00000F) "extern "C" int __cdecl localtime_s(struct tm * const,__int64 const * const)" (?localtime_s@@$$J0YAHQAUtm@@QB_J@Z),该标记在函数 "extern "C" void __cdecl unix_to_date(unsigned __int64,char *)" (?unix_to_date@@$$J0YAX_KPAD@Z) 中被引用 D:\正在试验\舞台测试\MultiProbesMFCApping徕卡(数据库成功后修改0807)\MFCApp\time_util.obj MFCApp
错误 9 error LNK2019: 无法解析的外部符号 "extern "C" int __cdecl localtime_s(struct tm * const,__int64 const * const)" (?localtime_s@@$$J0YAHQAUtm@@QB_J@Z),该符号在函数 "extern "C" void __cdecl unix_to_date(unsigned __int64,char *)" (?unix_to_date@@$$J0YAX_KPAD@Z) 中被引用 D:\正在试验\舞台测试\MultiProbesMFCApping徕卡(数据库成功后修改0807)\MFCApp\time_util.obj MFCApp

实在找不到原因,求各位大神帮忙看看
...全文
231 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jlennonxzy 2019-08-15
  • 打赏
  • 举报
回复
直接数据库层面可以解决的,sqlserver设置字段为datetime,默认为getdate()即可,不用insert这个字段,只要写数据就自动添加时间。其它数据库类似。
zgl7903 2019-08-13
  • 打赏
  • 举报
回复
localtime_s 的代码是开放的, 如果找不到库,就照搬代码写一份
Eleven 2019-08-13
  • 打赏
  • 举报
回复
localtime_s, _localtime32_s, _localtime64_s <time.h> <ctime> or <time.h>
hurryboylqs 2019-08-11
  • 打赏
  • 举报
回复
直接调Windows api GetLocalTime
zgl7903 2019-08-10
  • 打赏
  • 举报
回复

// 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 );
}

smwhotjay 2019-08-10
  • 打赏
  • 举报
回复

c 函数 多线程不安全吧。数据库时间的话 直接用now() 内置函数不行?

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧