与 C 中的 strptime 解析不一致?

Inconsistent parsing with strptime in C?

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

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

我运行这个程序作为./timetest 2400/001/00/00/00 2400/001/00/00/08

这是一些终端输出:

$ ./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

我是否遗漏了什么会产生这些不一致的结果?

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

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

Documentation 声明 tm 通常不会在被 strptime 调用之前进行初始化。这取决于您使用的 implementation/UNIX 系统。