如何从字符串解析日期/时间? [英] How to parse date/time from string?

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

问题描述

输入:带有日期和可选的时间字符串。不同的再presentations将是很好的,但必要的。字符串是用户提供的,可以是畸形的。例如:

Input: strings with date and optional time. Different representations would be nice but necessary. The strings are user-supplied and can be malformed. Examples:


  • 2004年3月21日12时45分33秒(我认为这是默认的布局)

  • 2004年3月21日12时45分33秒(可选配置)

  • 2004年9月23日4点12分21秒(德语格式,可选)

  • 2003-02-11(时间可能会丢失)

  • "2004-03-21 12:45:33" (I consider this the default layout)
  • "2004/03/21 12:45:33" (optional layout)
  • "23.09.2004 04:12:21" (german format, optional)
  • "2003-02-11" (time may be missing)

所需输出:纪元以来的秒数(1970/01/01 00:00:00)或其他一些定点

Needed Output: Seconds since Epoch (1970/01/01 00:00:00) or some other fixed point.

奖金:另外,读取UTC偏移的本地系统时间将是巨大的。

Bonus: Also, reading the UTC-offset of the local system time would be great.

的输入被假定为有问题的机器上的本地时间。
输出必须是UTC。是Linux系统只(Debian Lenny的和Ubuntu需要)。

The input is assumed to be a local time on the machine in question. The output needs to be UTC. System is Linux only (Debian Lenny and Ubuntu needed).

我曾尝试使用升压/ DATE_TIME ,但必须承认,我不能完成我的头文档各地。而不需要转换的以下工作从系统的本地时间UTC:

I have tried to use boost/date_time, but must admit I can't wrap my head around the documentation. The following works without the needed conversion from system local time to UTC:

std::string date = "2000-01-01";
boost::posix_time::ptime ptimedate = boost::posix_time::time_from_string(date);
ptimedate += boost::posix_time::hours(Hardcoded_UTC_Offset);// where to get from?
struct tm = boost::posix_time::to_tm(ptimedate);
int64_t ticks = mktime(&mTmTime);

我觉得的boost :: date_time的可提供所需的UTC偏移,但我不知道怎么办。

I think boost::date_time can provide the needed UTC offset, but I wouldn't know how.

推荐答案

虽然我不知道如何格式化升压个位数的月输入,我可以两位数的编辑后,做到这一点:

Although I don't know how to format a single-digit month input in boost, I can do it after the two-digit edit:

#include <iostream>
#include <boost/date_time.hpp>
namespace bt = boost::posix_time;
const std::locale formats[] = {
std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d %H:%M:%S")),
std::locale(std::locale::classic(),new bt::time_input_facet("%Y/%m/%d %H:%M:%S")),
std::locale(std::locale::classic(),new bt::time_input_facet("%d.%m.%Y %H:%M:%S")),
std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d"))};
const size_t formats_n = sizeof(formats)/sizeof(formats[0]);

std::time_t pt_to_time_t(const bt::ptime& pt)
{
    bt::ptime timet_start(boost::gregorian::date(1970,1,1));
    bt::time_duration diff = pt - timet_start;
    return diff.ticks()/bt::time_duration::rep_type::ticks_per_second;

}
void seconds_from_epoch(const std::string& s)
{
    bt::ptime pt;
    for(size_t i=0; i<formats_n; ++i)
    {
        std::istringstream is(s);
        is.imbue(formats[i]);
        is >> pt;
        if(pt != bt::ptime()) break;
    }
    std::cout << " ptime is " << pt << '\n';
    std::cout << " seconds from epoch are " << pt_to_time_t(pt) << '\n';
}
int main()
{
    seconds_from_epoch("2004-03-21 12:45:33");
    seconds_from_epoch("2004/03/21 12:45:33");
    seconds_from_epoch("23.09.2004 04:12:21");
    seconds_from_epoch("2003-02-11");
}

请注意将要承担的日期秒 - 从 - 划时代产量为UTC:

note that the seconds-from-epoch output will be assuming the date was in UTC:

~ $ ./test | head -2
ptime is 2004-Mar-21 12:45:33
seconds from epoch are 1079873133
~ $ date -d @1079873133
Sun Mar 21 07:45:33 EST 2004

您很可能使用的boost ::了posix_time :: c_time ::本地时间()的#include&LT;提升/ DATE_TIME / c_time.hpp&GT ; 来得到这种转换做假设输入是当前时区,但比较一致:对我来说吧,其结果将是今天和下个月,夏时制结束之间的不同

You could probably use boost::posix_time::c_time::localtime() from #include <boost/date_time/c_time.hpp> to get this conversion done assuming the input is in the current time zone, but it is rather inconsistent: for me, for example, the result will be different between today and next month, when daylight saving ends.

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

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