Parsing complex input
In the previous recipe, we were writing a simple parser for date. Imagine that some time has passed and the task has changed. Now, we need to write a date-time parser that supports multiple input formats and zone offsets. Our parser must understand the following inputs:
2012-10-20T10:00:00Z // date time with zero zone offset 2012-10-20T10:00:00 // date time without zone offset 2012-10-20T10:00:00+09:15 // date time with zone offset 2012-10-20-09:15 // date time with zone offset 10:00:09+09:15 // time with zone offset
Getting ready
We'll be using the Boost.Spirit
library, which was described in the Parsing simple input recipe. Read it before getting hands on with this recipe.
How to do it...
- Let's start by writing a date-time structure that will hold a parsed result:
#include <stdexcept> #include <cassert> struct datetime { enum zone_offsets_t { OFFSET_NOT_SET, OFFSET_Z, OFFSET_UTC_PLUS, OFFSET_UTC_MINUS...