Library to parse descriptions of recurring dates in C++?
For my application I need a parser that can handle direct descriptions of dates, e.g. "12/31/10" for this year's New Year's Eve, but can also handle descriptions of /repeating/ dates, e.g. "First Monday of Every Month." boost::date_time already has a concept of date generators, but I don't think it provides any general way to convert strings to them without knowing the type of generator that will be produced.
Before I go and reinvent the wheel, is there already something out there to do this? I'm flexible with the exact language of the strings, just so long as it's somethi开发者_开发百科ng a nonprogrammer could read and understand.
You can use the wonderful boost::spirit library.
It allows you to easily create parsers for this kind of thing.
Maybe you can draw inspiration from Roaring Penguin's remind
tool which has a reasonably comprehensible (for simple cases) language.
You can write your own grammar in EBNF and then use a lexer/parser generator to create a code-skeleton for you.
Using this library, here is code that prints out the first Monday of every month in 2011:
#include "date.h"
#include <iostream>
int main()
{
using namespace gregorian;
std::cout << date_fmt("%A %B %e, %Y");
for (date d = first*mon/jan/2011; d <= dec/31/2011; d += month(1))
std::cout << d << '\n';
}
Output:
Monday January 3, 2011
Monday February 7, 2011
Monday March 7, 2011
Monday April 4, 2011
Monday May 2, 2011
Monday June 6, 2011
Monday July 4, 2011
Monday August 1, 2011
Monday September 5, 2011
Monday October 3, 2011
Monday November 7, 2011
Monday December 5, 2011
You can also get the second, third, etc., or last weekday of a month. Arithmetic can be day, month or year oriented.
精彩评论