开发者

localtime returns null

localtime returns null. Why? 开发者_JS百科(I'm using Visual C++ 2008)

struct tm    *tb;   
time_t       lDate;      

time(&lDate);

tb = localtime(&lDate); // tb is null everytime I try this!      


Is that your exact code? I just compiled this program and it works fine:

#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;
}


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

 int main(void)
  {
     // get the current time
     time_t now = time(0);
     struct tm* theTime = localtime(&now);
     int t=(int)theTime;
     printf("%d",t);
     getch();
     return 0;
  }

it works


Define the preprocessor _USE_32BIT_TIME_T in your project and try again. Good luck:)


The code you posted in your comments works fine, up until you get to the if statement. I'm not sure what you are trying to do here, but you have a ; in if (pArea); that almost definitely should not be there (hard to tell since it's formatted horribly because you put it in a comment). You are also returning 0 all the time, is that what you intended to do?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜