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

查看:104
本文介绍了日期/时间分析在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,%间等可以有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:增加了另一种答案所以code被格式化

@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天全站免登陆