Click here to Skip to main content
Licence CPOL
First Posted 19 Mar 2008
Views 13,213
Downloads 39
Bookmarked 7 times

Parse date time string with spirit

By | 19 Mar 2008 | Article
A sample show how to parse date with boost spirit framework

Introduction

This article include a sample show how to use boost spirit framework parse a date time string like "1999-01-20T00:01:22".

Background

The XML schema define the date time format like this "2000-01-30T00:10:20+1".

Using the code

First create your parser

struct myparser: public grammar<myparser>
{
  template <typename ScannerT>
  struct definition
  {
    definition(myparser const& self)
    {
      first = int_p[assign(self.date_.year_)] >> "-" >> int_p[assign(self.date_.month_)] 
      >> "-" >> int_p[assign(self.date_.day_)] >> "T" >> int_p[assign(self.date_.hour_)] 
      >> ":" >> int_p[assign(self.date_.minute_)] >> ":" >> int_p[assign(self.date_.second_)] ;
    }
    rule<ScannerT> first;
    rule<ScannerT> const&
    start() const { return first; }
  };
  myparser(date& d):date_(d){};
  date& date_;
};        

Then invoke the parser:

bool parse_date(char const* str, date& d)
{
  myparser p(d);
  return parse(str, p).full;
}      

Points of Interest

This parser can be improved to support more flexible format

History

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

ZhengPing Wang



China China

Member

Programmer live in china.
Interesting with knowledge management.
 
My home page: http://herosys.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionOverkill? PinmemberPatLeCat3:05 20 Mar '08  
AnswerRe: Overkill? PinmemberComaWhite8610:33 16 Nov '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 20 Mar 2008
Article Copyright 2008 by ZhengPing Wang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid