固定格式字符串中的 strptime 处理 space
strptime handling space in fixed format string
有没有办法让 strptime()
处理固定格式的时间字符串?
我需要解析一个始终采用固定宽度格式的时间字符串:“yymmdd HHMMSS
”,
但复杂的是前导零有时存在有时不存在。
阅读 strptime
的 man(3p) 页面,我注意到对于所有转换说明符 %y, %m, %d, %H, %M, %S
,它被注释为 "leading zeros shall be permitted but shall not be required."。因此,我尝试使用格式说明符 %y%m%d %H%M%S
,天真地希望 strptime
能够识别两个子字符串 %y%m%d
和 %H%M%S
中的空格等同于(缺失)前导零。
这似乎适用于说明符 %m
,但不适用于 %M
(好吧,除非第二部分小于 10),如以下代码片段所示
#include <stdio.h>
#include <time.h>
int main() {
struct tm buff;
const char ts[]="17 310 22 312";
char st[14];
strptime(ts,"%y%m%d %H%M%S", &buff);
strftime(st,14,"%y%m%d %H%M%S",&buff);
printf("%s\n",ts);
printf("%s\n",st);
return 0;
}
编译后 运行 在我的机器上输出
17 310 22 312
170310 223102
任何关于如何克服这个问题的见解都将不胜感激,或者我是否需要在使用 atoi
转换为整数以填充我的 [=24= 时诉诸手动切割字符串 2 个字符]实例与?
最好获取生成格式不正确的数据的代码。
假设今天早上无法完成,那么也许您应该规范化(副本)不稳定的数据,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static inline void canonicalize(char *str, int begin, int end)
{
for (int i = begin; i <= end; i++)
{
if (str[i] == ' ')
str[i] = '0';
}
}
int main(void)
{
struct tm buff;
const char ts[] = "17 310 22 312";
char st[32];
char *raw = strdup(ts);
printf("[%s] => ", raw);
canonicalize(raw, 0, 5);
canonicalize(raw, 7, 12);
printf("[%s] => ", raw);
strptime(raw, "%y%m%d %H%M%S", &buff);
strftime(st, sizeof(st), "%y%m%d %H%M%S", &buff);
printf("[%s] => ", st);
strftime(st, sizeof(st), "%Y-%m-%d %H:%M:%S", &buff);
printf("[%s]\n", st);
free(raw);
return 0;
}
canonicalize()
函数将字符串的给定范围内的空格替换为零。很明显,如果你指定了越界的起点和终点,它就会越界。我在 ts
上保留了 const
并用 strdup()
制作了一份副本;如果您可以将字符串视为可变数据,则无需制作(或释放)副本。
该代码的输出是:
[17 310 22 312] => [170310 220312] => [170310 220312] => [2017-03-10 22:03:12]
有没有办法让 strptime()
处理固定格式的时间字符串?
我需要解析一个始终采用固定宽度格式的时间字符串:“yymmdd HHMMSS
”,
但复杂的是前导零有时存在有时不存在。
阅读 strptime
的 man(3p) 页面,我注意到对于所有转换说明符 %y, %m, %d, %H, %M, %S
,它被注释为 "leading zeros shall be permitted but shall not be required."。因此,我尝试使用格式说明符 %y%m%d %H%M%S
,天真地希望 strptime
能够识别两个子字符串 %y%m%d
和 %H%M%S
中的空格等同于(缺失)前导零。
这似乎适用于说明符 %m
,但不适用于 %M
(好吧,除非第二部分小于 10),如以下代码片段所示
#include <stdio.h>
#include <time.h>
int main() {
struct tm buff;
const char ts[]="17 310 22 312";
char st[14];
strptime(ts,"%y%m%d %H%M%S", &buff);
strftime(st,14,"%y%m%d %H%M%S",&buff);
printf("%s\n",ts);
printf("%s\n",st);
return 0;
}
编译后 运行 在我的机器上输出
17 310 22 312
170310 223102
任何关于如何克服这个问题的见解都将不胜感激,或者我是否需要在使用 atoi
转换为整数以填充我的 [=24= 时诉诸手动切割字符串 2 个字符]实例与?
最好获取生成格式不正确的数据的代码。
假设今天早上无法完成,那么也许您应该规范化(副本)不稳定的数据,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static inline void canonicalize(char *str, int begin, int end)
{
for (int i = begin; i <= end; i++)
{
if (str[i] == ' ')
str[i] = '0';
}
}
int main(void)
{
struct tm buff;
const char ts[] = "17 310 22 312";
char st[32];
char *raw = strdup(ts);
printf("[%s] => ", raw);
canonicalize(raw, 0, 5);
canonicalize(raw, 7, 12);
printf("[%s] => ", raw);
strptime(raw, "%y%m%d %H%M%S", &buff);
strftime(st, sizeof(st), "%y%m%d %H%M%S", &buff);
printf("[%s] => ", st);
strftime(st, sizeof(st), "%Y-%m-%d %H:%M:%S", &buff);
printf("[%s]\n", st);
free(raw);
return 0;
}
canonicalize()
函数将字符串的给定范围内的空格替换为零。很明显,如果你指定了越界的起点和终点,它就会越界。我在 ts
上保留了 const
并用 strdup()
制作了一份副本;如果您可以将字符串视为可变数据,则无需制作(或释放)副本。
该代码的输出是:
[17 310 22 312] => [170310 220312] => [170310 220312] => [2017-03-10 22:03:12]