mktime 在某些年份有问题?

mktime having problems with certain years?

我对 mktime() 中的年份有疑问。

每次我将小于 1970 的年份传递到我的 struct tm 然后 运行 mktime() 函数来转换我的结构时,它都会失败(returns -1 ).

有谁知道为什么以及我能否以某种方式完成这项工作?

这永远行不通,因为根据定义,它在 the epoch, which is the start for Unix time. The manual page 状态之外:

The ctime(), gmtime() and localtime() functions all take an argument of data type time_t which represents calendar time. When interpreted as an absolute time value, it represents the number of seconds elapsed since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

如果无法将输入转换为 Unix 时间,则会记录到 return -1,这就是您看到该结果的原因。

这是预期的行为。根据 man page:

   If the specified broken-down time cannot be represented as calendar
   time (seconds since the Epoch), mktime() returns (time_t) -1 and does
   not alter the members of the broken-down time structure.