Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# library to parse human readable time spans

Tags:

date

c#

parsing

Is there a library that exists that will parse human readable timespans into a .NET TimeSpan?

I need something that will parse strings like:

30 days
1 week
5 hours

Does such a thing exist? Its probably not too hard to write myself, but if something is out there, it would be so much easier!

I currently don't need functionality like "30 days, 3 hours, 5 minutes", just the one value, but it couldn't hurt!

like image 572
Gareth Avatar asked Aug 20 '09 08:08

Gareth


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


2 Answers

I took a crack at this sometime back, I was playing with parsers as I knew nothing about them so don't expect quality. But you can get the code here: http://timestamper.codeplex.com/

I havent looked at it for a while, but im pretty sure it can do all the things that you mentioned above.

Edit:

Just noted that you wanted a timespan object returned and not a date time. The code I provided will return a DateTime reletive to today i.e.

"2 days" will be: DateTime.Now.AddDays(2);

At least I think this is what is did, can quite be sure. If this is the case, you should just be able to get the timespan between the two DateTimes.

like image 56
Owen Avatar answered Sep 21 '22 12:09

Owen


This might be long overdue, but Microsoft has recently released nuget packages and sources for its extractors and parsers that underpin LUIS (the language services in Azure).

You can find them here: GitHub Repo

My guess is that for most projects it would be like swatting a fly with a jackhammer, but luckily separate packages (such as one for DateTime) can be downloaded independently.

The NuGet package that answers the question

like image 26
Ilya-at-PrettyNeat Avatar answered Sep 18 '22 12:09

Ilya-at-PrettyNeat