First draft of a KHumanDateTimeParser class
dsteckel at ulb.ac.be
Tue Apr 16 14:05:51 BST 2013
Two or three days ago, I asked on this mailing list if there was any
implementation of a human-entered date-time parser in the KDE libraries
I received very interesting responses, for instance one that confirmed
isn't any parser like that in kdelibs, and another one that pointed me
Date::Manip Perl module.
During the week-end, I played with Date::Manip and thought about a C++
implementation that could be extensible yet simple to implement and to
After two days of coding, here is my first draft :
It is a simple parser that reads rules from a XML file. The README.md
the repository explains how to write rules, I hope it is not too
that my English doesn't hurt you too much.
In its current state, this parser is able to understand things like
ago", "in 3 months", "next Monday at 3.00 pm" or even "16 May 2009" (a
the English rules, that I just see now, makes parsing "16th of May
impossible, I have forgotten the "of").
One thing it can't properly parse is complex relations like "next
Currently, the parser adds one week to the current date, and then sets
of week to "Tuesday". It works if the current day of week is already
a later day, but if we are a Monday, next Tuesday is tomorrow, not in
The same problem is present for dates like "Last Monday". If the Monday
current week already passed, the parser will erroneously return the
last week, not the Monday of this week.
Fixing this problem may really complicate the parser, as it would
parsing rules to have "if" conditions. Another solution may to
logic in C++.
When I was implementing this parser, I realized that every western
will have nearly the same rules, and that duplicating them for every
will be a waste of time. What about considering this parser an
and hard-coding the most useful rules in KCalendarSystem, using i18n()
to translate everything ? With a bit of code, it could be possible to
implement any rule, even the "last Monday" ones. I thought of
them in KCalendarSystem because the rules seem to be more
calendar-system-specific than language-specific.
More information about the kde-core-devel