C ++中的日期/时间解析(任何格式字符串到Epoch) [英] Date/Time parsing in C++ (Any format string to Epoch)

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

问题描述

我正在编写一个C ++应用程序,它必须解析日期/时间字符串并给出时间。
但是日期/时间字符串的格式可以多于一个(实际上说25个格式),如

  EEE,dd MMM yyyy HH:mm:ss'('ZZZ')',
EEE,dd MMM yyyy HH:mm:ss'\'ZZZ'\',
EEE,dd MMM yyyy hh:mm:ss z,
EEE,dd MMM yyyy HH:mm Z,
EEE,dd MMM yyyy HH:mm:ss,
EEE,MMM dd,yyyy'at'HH:mm:ss z,
EEE M / dd / yyyy hh:mm:ss a,
EEE MMM dd HH:mm :ss z yyyy,
EEE MMM dd hh:mm:ss yyyy,
EEEE,MMMM dd,yyyy hh:mm:ss a,
EEEE,MMMM dd ,yyyy HH:mm a,
EEEE,MMMM dd,yyyy HH:mm,
MMM dd,yyyy hh:mm:ss a,
dd MMM yyyy hh :mm:ss z,
dd-MMM-yyyy HH:mm:ss z,
dd MMM yy HH:mm:ss,
MM / dd / yyyy hh:mm a(EEEE),
MM / dd / yyyy hh:mm a(EEEE),
MM / dd / yyyy hh:mm:ss,
MM / dd / yyyy hh:mm a Z,
MM / dd / yyyy hh:mma Z,
MM / dd / yyyy hh:mma,
MM / dd / yyyy hh:mm a,
MM / dd / yyyy hh:mm Z,
MM / dd / yy hh:mm a Z,
MM / dd / yy hh:mma Z,
MM / dd / yy HH:mm a,
MM / dd / yy HH:mm Z,
MM / dd / yyyy
yyyy-MM-dd HH:mm:ss,
yyyyMMddhhmmss,
yyyyMMddhhmm,
yyyyMMdd

现在,我需要使用字符串,找出它属于哪些格式,然后在时代中获得时间。



你可以建议一种方法来做到这一点。代码示例将非常有帮助。
我在Boost图书馆上进行银行业务。让我知道这可以通过提升日期/时间解析库来实现。



提前感谢
AJ

解决方案

我想你可以尝试使用这些格式转换您的字符串到ptime,并选择不导致 not_a_date_time 。:



提升格式标志与您的稍微不同,我将仅针对此示例的最后五个:

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

time_t ptime_to_time_t(boost :: posix_time :: ptime t)
{
static boost :: posix_time :: ptime epoch(boost :: gregorian :: date(1970,1 ,1));
return(t-epoch).ticks()/ boost :: posix_time :: time_duration :: ticks_per_second();
}
int main()
{
std :: string msg =2010-08-04 08:34:12;

for(size_t i = 0; i< formats; ++ i)
{
std :: istringstream ss(msg);
ss.imbue(inputs [i]);
boost :: posix_time :: ptime this_time;
ss>>这次;

if(this_time!= boost :: posix_time :: not_a_date_time)
std :: cout<<< this_time<< 或< ptime_to_time_t(this_time)<< std :: endl;
}
}


I am writing a C++ app that has to parse a date/time string and give out epoch time. But the format of the date/time string can be more than one (actually say 25 formats) like

    "EEE, dd MMM yyyy HH:mm:ss '('ZZZ')'",
    "EEE, dd MMM yyyy HH:mm:ss '\"'ZZZ'\"'",
    "EEE, dd MMM yyyy hh:mm:ss z",
    "EEE, dd MMM yyyy HH:mm Z",
    "EEE, dd MMM yyyy HH:mm:ss",
    "EEE, MMM dd, yyyy 'at' HH:mm:ss z",
    "EEE M/dd/yyyy hh:mm:ss a",
    "EEE MMM dd HH:mm:ss z yyyy",
    "EEE MMM dd hh:mm:ss yyyy",
    "EEEE, MMMM dd, yyyy hh:mm:ss a",
    "EEEE, MMMM dd, yyyy HH:mm a",
    "EEEE, MMMM dd, yyyy HH:mm",
    "MMM dd, yyyy hh:mm:ss a",
    "dd MMM yyyy hh:mm:ss z",
    "dd-MMM-yyyy HH:mm:ss z",
    "dd MMM yy HH:mm:ss",
    "MM/dd/yyyy  hh:mm a  (EEEE)",
    "MM/dd/yyyy hh:mm a (EEEE)",
    "MM/dd/yyyy hh:mm:ss",
    "MM/dd/yyyy hh:mm a Z",
    "MM/dd/yyyy hh:mma Z",
    "MM/dd/yyyy hh:mma",
    "MM/dd/yyyy hh:mm a",
    "MM/dd/yyyy hh:mm Z",
    "MM/dd/yy hh:mm a Z",
    "MM/dd/yy hh:mma Z",
    "MM/dd/yy HH:mm a",
    "MM/dd/yy HH:mm Z",
    "MM/dd/yyyy",   
    "yyyy-MM-dd HH:mm:ss",
    "yyyyMMddhhmmss",
    "yyyyMMddhhmm",
    "yyyyMMdd"

Now, i need to take the string, figure out it belongs to which of these formats, then get the time in epoch.

Can you suggest a way to do this. Code samples will be really helpful. I am banking on Boost libraries. Let me know if this can be achieved by boost date/time parsing libraries.

Thanks in advance, AJ

解决方案

I suppose you could attempt to convert your string into ptime using each of these formats and pick the ones that do not result in a not_a_date_time.:

The boost format flags are slightly different from yours, I'll do just the last five for this example:

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

time_t ptime_to_time_t(boost::posix_time::ptime t)
{
       static boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
       return (t-epoch).ticks() / boost::posix_time::time_duration::ticks_per_second();
}
int main()
{
       std::string msg = "2010-08-04 08:34:12";

       for(size_t i=0; i<formats; ++i)
       {
           std::istringstream ss(msg);
           ss.imbue(inputs[i]);
           boost::posix_time::ptime this_time;
           ss >> this_time;

           if(this_time != boost::posix_time::not_a_date_time)
               std::cout << this_time << " or " << ptime_to_time_t(this_time) << std::endl;
       }
}

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

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