在C中与strtime的解析不一致?

在C中与strtime的解析不一致?,c,time,strptime,C,Time,Strptime,我在尝试使用C中的strtime函数时遇到了奇怪的行为 #include <stdio.h> #define __USE_XOPEN #define _GNU_SOURCE #include <time.h> #include <stdlib.h> #include <unistd.h> int parseTime(char *timestamp) { time_t t1; struct tm *timeptr,tm1;

我在尝试使用C中的strtime函数时遇到了奇怪的行为

#include <stdio.h>
#define __USE_XOPEN
#define _GNU_SOURCE
#include <time.h>
#include <stdlib.h>
#include <unistd.h>


int parseTime(char *timestamp)
{

    time_t t1;
    struct tm *timeptr,tm1;
    char* time1 = timestamp;


    //(1) convert `String to tm`:  
    if(strptime(time1, "%Y/%j/%H/%M/%S",&tm1) == 0)
    {
        fprintf(stderr,"\nInvalid timestamp\nTimestamp should be in the format: YYYY/DDD/HH/MM/SS\n");
        exit(EXIT_FAILURE); 
    }         

    //(2)   convert `tm to time_t`:    
    t1 = mktime(&tm1);

    return t1;
}


int main(int argc, char const *argv[])
{
     int now = parseTime(argv[1]);  

     int wait = parseTime(argv[2]) - now;

     printf("%d\n", wait);


    return 0;
}

是否有我遗漏的东西会产生这些不一致的结果?

可能是
tm
在使用
strtime
之前没有初始化

初始化tm:
memset(&tm,0,sizeof(struct-tm))


声明
tm
在被
strtime
调用之前通常不会初始化。这取决于您使用的是哪个实现/UNIX系统。

在声明为返回
int
的函数中返回
time\u t
。。这些应该匹配
$ ./timetest 2400/001/00/00/00 2400/001/00/00/08

3608

$ ./timetest 2400/001/00/00/00 2400/001/00/00/08

3608

$ ./timetest 2400/001/00/00/00 2400/001/00/00/08

8

$ ./timetest 2400/001/00/00/00 2400/001/00/00/08

8

$ ./timetest 2400/001/00/00/00 2400/001/00/00/08

8

$ ./timetest 2400/001/00/00/00 2400/001/00/00/08

8

$ ./timetest 2400/001/00/00/00 2400/001/00/00/08

3608

$ ./timetest 2400/001/00/00/00 2400/001/00/00/08

3608

$ ./timetest 2400/001/00/00/00 2400/001/00/00/08

8

$ ./timetest 2400/001/00/00/00 2400/001/00/00/08

3608