views:

673

answers:

7

This question is the other side of the question asking, "How do I calculate relative time?".

Given some human input for a relative time, how can you parse it? By default you would offset from DateTime.Now(), but could optionally offset from another DateTime.

(Prefer answers in C#)

Example input:

  • "in 20 minutes"
  • "5 hours ago"
  • "3h 2m"
  • "next week"

Edit: Let's suppose we can define some limits on the input. This sort of code would be a useful thing to have out on the web.

A: 

If you're expecting to parse arbitrary input from humans, may I suggest: Make a ton of unit tests, and good luck.

Jason Cohen
Yes, arbitrary input from humans. Quite a task, but should greatly boost usability.
Larsenal
A: 

Parsing specific known formats is easy enough - string manipulation and if-statements.

Parsing human input is really really difficult. It's an AI question.

Blorgbeard
Google's calculator seems to be pretty good at it.
Anders Sandvig
As do the GNU date input formats: http://www.gnu.org/software/tar/manual/html_node/Relative-items-in-date-strings.html
Bobby Jack
It does indeed! But I would say that parsing simple arithmetic is a bit easier than what the OP wants. Also, I didn't say it was impossible, and google has a lot of very smart people.
Blorgbeard
The GNU date thing does look pretty cool. You still have a set of fairly clearly defined formats though. Wouldn't handle "3h 2m" for example? Or "last tuesday morning", or "this afternoon".
Blorgbeard
Outlook does this as well.
Geoffrey Chetwood
+3  A: 

That's building a DSL (Domain specific language) for date handling. I don't know if somebody has done one for .NET but the construction of a DSL is fairly straightforward:

  1. Define the language precisely, which input forms you will accept and what will you do with ambiguities
  2. Construct the grammar for the language
  3. Build the finite state machine that parses your language into an actionable AST

You can do all that by yourself (with the help of the Dragon Book, for instance) or with the help of tools to the effect, as shown in this link.

Just by thinking hard about the possibilities you have a good chance, with the help of good UI examples, of covering more than half of the actual inputs your application will receive. If you aim to accept everything a human could possibly type, you can record the input determined as ambiguous and then add them to the grammar, whenever they can be interpreted, as there are things that will be inherently ambiguous.

Vinko Vrsalovic
A: 

This: http://www.codeproject.com/KB/edit/dateparser.aspx Is fairly close to what you are trying to accomplish. Not the most elegant solution, but certainly might save you some work.

Geoffrey Chetwood
+3  A: 

A google search turns up the parsedatetime library (associated with the Chandler project), which is designed to do exactly this. It's open source (Apache License) and written in Python. It seems to be quite sophisticated -- from the homepage:

parsedatetime is able to parse, for example, the following:

* Aug 25 5pm
* 5pm August 25
* next saturday
...
* tomorrow
* next thursday at 4pm
* at 4pm
* eod
* in 5 minutes
* 5 minutes from now
* 5 hours before now
* 2 days from tomorrow

Since it's implemented in pure Python and doesn't use anything fancy, there's a good chance it's compatible with IronPython, so you could use it with .net. If you want specifically a C# solution, you could write something based on the algorithms they use...

It also comes with a whole bunch of unit tests.

dF
A: 

The ruby folks have attempted to tackle this with a parser called Chronic.

I watched an informative video presentation recently on how the author went about solving this problem.

Jason Navarrete
A link to the video would be great.
Larsenal
A: 

This is likely not all that helpful since you're talking c# but since no one's mentioned it yet you can try to take a look at php's excellent and utterly insane native strtotime function

George Mauer