本地时间返回 null

Nik*_*las 5 c

本地时间返回 null。为什么?(我使用的是 Visual C++ 2008)

struct tm    *tb;   
time_t       lDate;      

time(&lDate);

tb = localtime(&lDate); // tb is null everytime I try this!      
Run Code Online (Sandbox Code Playgroud)

Jon*_*han 1

这是你的确切代码吗?我刚刚编译了这个程序,它运行良好:

#include <stdio.h>
#include <time.h>

int main(int argc, char **argv)
{
    struct tm *tb;
    time_t lDate;

    time(&lDate);
    if (lDate == -1) {
        perror("time");
        return 1;
    }

    tb = localtime(&lDate);
    if (tb == NULL) {
        fprintf(stderr, "localtime failed\n");
        return 1;
    }

    printf("Good\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)