C ++中的日期/时间解析 [英] Date/Time parsing in C++

查看:180
本文介绍了C ++中的日期/时间解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++中进行数据/时间解析(将任何格式的字符串转换为日期)时,我发现以下有用的方法

While doing the data/time parsing in c++ (converting a string in any format to a date), i found the following useful methods

1)strptime - 这里%d,%m等可以有1或2个字符。该功能会处理。这样做的结果是,我们在两个转换说明符之间使用一个分隔符。例如:它不能给出%d%m,它是%d /%m或任何其他分隔符。此外,这不支持时区。

1) strptime() - here the %d, %m etc can have either 1 or 2 characters. The function will take care of that. As a result of this it will enforce that we use a separator between two conversion specifiers. Ex: Its not valid to give %d%m it has be to %d/%m or any other separator. Also this does not support timezones.

2)提升日期IO - 这里%d,%m必须有2个字符。现在,我得到的输入字符串不能保证有这个。因此,它不可能成功地使用这个。但是,这似乎支持时区,但不确定。因为它表示输入它支持时区

2) Boost date IO - here the %d, %m has to have 2 characters. Now, the input string i get is not guaranteed to have this. As a result, its not possible to use this successfully. However, this does seem to support timezone, but not sure. because it says for inputs it does support timezone

所以我打算同时使用两者来确定日期。但我想得到一个,我可以考虑到时区,以及。但没有人似乎支持这一点。

So i am planning to use both in conjunction to determine the date. But i would like to get one where i can take into account the timezone as well. But none seems to be supporting that.

有人有建议吗?
Rgds,
AJ

Does anybody have a suggestion? Rgds, AJ

推荐答案

@AJ:添加了另一个答案, >

@AJ: Added another answer so the code gets formatted

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

int
main(void)
{
        struct tm tm[1] = {{0}};

        strptime("Tue, 19 Feb 2008 20:47:53 +0530", "%a, %d %b %Y %H:%M:%S %z", tm);
        fprintf(stdout, "off %ld\n", tm->tm_gmtoff);
        return 0;
}

运行看起来像(glibc 2.10.1):

And a run looks like (glibc 2.10.1):

freundt@clyde:pts/28:~/temp> ./test
off 19800

这篇关于C ++中的日期/时间解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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