- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 strptime() 解析 ISO 8601 格式的日期(由“%F”或“%Y-%m-%d”指定),但是当我从结果 struct tm 中读取时,我得到了荒谬的值时间、分钟和秒。这是示例代码和结果:
char *s_date = (char *)malloc(sizeof(char) * 255);
scanf("%s", s_date);
struct tm bd_date;
strptime(s_date, "%Y-%m-%d", &bd_date)
printf("%d-%d-%d %d:%d:%d\n", (bd_date.tm_year + 1900), (bd_date.tm_mon + 1), bd_date.tm_mday, bd_date.tm_hour, bd_date.tm_min, bd_date.tm_sec);
输入“1990-05-01”的结果输出是:1990-5-1 2007659173:32766:1076394855,显示 tm_hour、tm_min 和 tm_sec 的非法值。我究竟做错了什么?我有什么遗漏的吗?我虽然 strptime() 会自动将格式中未使用的字段设置为 0,但情况似乎并非如此。
最佳答案
Apple 的 strptime
手册页明确表示:
If the
<i>format</i>
string does not contain enough conversion specifications to completely specify the resulting<i>struct tm</i>
, the unspecified members of<i>timeptr</i>
are left untouched. For example, if format is “%H:%M:%S
”, only<i>tm_hour</i>
,<i>tm_sec</i>
and<i>tm_min</i>
will be modified.
Linux man pages不那么明确,但与此一致:
In principle, this function does not initialize
<i>tm</i>
but stores only the values specified. This means that<i>tm</i>
should be initialized before the call.
Apple 的 macOS 和 Linux 都力求遵守的 POSIX 标准,在我手头上的 2008 年版本中既没有上述内容,也没有类似的文本。对于其他结构成员是否被修改或结构是否被初始化,它只是保持沉默。
因此,至少在某些实现中,strptime
不修改格式字符串中没有相应描述符的成员。例如,如果没有小时描述符,例如 %H
,它不会修改结构体的 hour 成员。如果您想要设置这些成员,您可以在调用 strptime
之前设置它们。 .
关于c - strptime() 在 C 中产生垃圾时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70059813/
当使用 Time.strptime 和 Date.strptime 解析“某些日期”(在 Ruby 中)时有不同的行为。 例如,如果我们尝试转换“2 月 30 日”(一个不存在的日期),我们有: Ti
我有一个带有时间字段的 Django 模型:time_from = models.TimeField()。我目前有一个以小时为单位发送时间的网络表单,例如:12、13、4、5 ...等等... 我目前
我需要解析一些格式为 ddmmyyyyhhmmssXXX 的时间字符串。 XXX 部分是毫秒。在下面的代码中,我忽略了毫秒部分。它有效,但我收到错误: garbage at end of string
我有一个数据集,其中包含要转换为日期时间的字符串。不幸的是,有些数据有微秒,有些则没有。当我尝试使用以下内容转换数据时,出现以下错误。 data['end_date'] = data['end_dat
我有一个芬兰语日期表示形式 (tiistaina, 27. lokakuuta 2015),我需要将其转换为日期时间对象。但是,Python 中的日期时间库无法识别日期和月份名称 我希望像下面这样的东
EDIT2:这个问题假设一个带有 Python 的 POSIX-ish 平台与 Glibc 链接。 在我的系统上,使用 %z 格式化指令进行往返转换使用Python的time库无法解析ISO 8601
我使用了从日志文件中读取的以下格式,使用的格式化程序与日期时间格式匹配。不知道为什么它仍然出错。 - ValueError:时间数据“'2018-01-01 10:00:00'”与格式'%Y-%m-%
我正在使用函数strptime()将字符串转换为struct tm。但是,当我检查字段时,有一些字段未填写。所以我检查了手册页,但似乎我正在做正确的事情,所以现在我对出了什么问题感到茫然。 示例输入字
我是 c 语言新手,正在尝试 strptime 函数,该函数将字符串时间转换为结构 tm。转换后我没有得到正确的时间。一切都很好,但年份显示错误(默认年份 1900)。 #include #incl
我正在解析一个包含格式为 DD/MM/YYYY HH:MM:SS 的字符串的文件。我正在尝试使用以下代码读取它们并将它们转换为 Unix 时间戳: tm tm; cout << datestr <<
我正在拉一个看起来像这样的时间戳 - 2014-02-03T19:24:07Z 我正在尝试计算自 1 月 1 日以来的天数。 我可以使用 将其转换为日期时间 yourdate = dateutil.p
我正在尝试使用以下代码将给定的自定义格式日期字符串解析为纪元时间: time_tuple = time.strptime('Tue Dec 05 13:01:48 PST 2017', '%a %b
我正在尝试像这样解析日期时间, char *line = "Feb 21 20:15:58"; struct tm t; strptime (line, "%B %d %T", &t); printf
我试图找出当前日期和我从数据库 (postgresql) 中提取的日期之间的天数差异 这是我的代码: cur.execute('select id, city_id, event_id, du
我正在编写一些脚本,这些脚本需要处理一年中的哪一周。我的输入格式为 YYYYWW,例如 '201435'。 在我尝试使用 strptime 期间,出于某种原因,它默认为一年中的第一天。例如: impo
我正在尝试使用 strptime(buf, &pattern,&result)转换 char[]包含日期到 tm结构。 我正在使用这样的函数: if(strptime(buf, &pattern,&r
ValueError: time data '03-10-2011 04:35 PM' does not match format '%m-%d-%Y %I:M %p' 这看起来很适合我? date
我收到了大量需要读入数据库的日期时间表示形式。我正在使用 Python(因为它很棒)。这些字符串的格式很糟糕,不精确到秒,没有规定时区,小时也没有前导 0。所以它们看起来更像这样: April 29,
我一直在尝试将给定时间转换为不同的格式,同时考虑外部设备的时区,跳过操作系统的时区转换。 最初,我有一个以 UTC 秒为单位的时间,以及来自设备的时区。例如,我现在所在的时间是秒=1343931446
我有数据以 %Y-%m-%d %H:%M:%S.%f 的格式传入。 因此,我根据该结构处理数据,一旦出现蓝色月亮,就会出现问题。所以我开始在出现问题时将其丢弃,我注意到最后的微秒丢失了。 我假设这意味
我是一名优秀的程序员,十分优秀!