为什么strptime()在OSX和Linux上的行为不同? [英] Why does strptime() behave differently on OSX and on Linux?

查看:69
本文介绍了为什么strptime()在OSX和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下,这是我得到的:

Under OSX, this is what I obtain:

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

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

But on Linux, this is what I get:

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

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

Why is the bahaviour different? I would expect the day of the week to be well defined, given the data and the time of the day?

推荐答案

strptime 的Linux(glibc)和OS X实现是不同的.从 OS X手册页:

The Linux (glibc) and OS X implementations of strptime are different. From the OS X man page:

如果格式字符串包含的转换规范不足以完全指定生成的 struct tm ,则tm的未指定成员保持不变.例如,如果格式为``%H:%M:%S'',只会修改 tm_hour tm_sec tm_min .

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 相比:

glibc实现不涉及那些未明确指定的字段,只是它会重新计算 tm_wday tm_yday 字段(如果是年,月或日中的任何一个)元素已更改.

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.

因此,您可以说它按文档所述工作.

So, you could say it's working as documented.

这篇关于为什么strptime()在OSX和Linux上的行为不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆