为什么 strptime() 在 OSX 和 Linux 上表现不同?

Why does strptime() behave differently on OSX and on Linux?

考虑这个程序:

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

int main() {
  struct tm t;
  strptime("2015-08-13 12:00:00", "%F %T", &t);
  printf("t.tm_wday = %d\n", t.tm_wday);
  return 0;
}

在OSX下,我得到的是:

$ gcc test_strptime.c
$ ./a.out
t.tm_wday = 0

但是在 Linux,这是我得到的:

$ gcc test_strptime.c
$ ./a.out
t.tm_wday = 4

为什么行为不同?考虑到数据和一天中的时间,我希望星期几能够明确定义?

Linux (glibc) 和 OS strptime 的 X 实现是不同的。来自 OS X man page:

If the format string does not contain enough conversion specifications to completely specify the resulting struct tm, the unspecified members of tm are left untouched. For example, if format is ``%H:%M:%S'', only tm_hour, tm_sec and tm_min will be modified.

glibc相比:

The glibc implementation does not touch those fields which are not explicitly specified, except that it recomputes the tm_wday and tm_yday field if any of the year, month, or day elements changed.

因此,您可以说它按照记录工作。