user friendly datetime features

  • Thread starter Daniel Fetchinson
  • Start date
D

Daniel Fetchinson

Many times a more user friendly date format is convenient than the
pure date and time.
For example for a date that is yesterday I would like to see
"yesterday" instead of the date itself. And for a date that was 2 days
ago I would like to see "2 days ago" but for something that was 4 days
ago I would like to see the actual date. This is often seen in web
applications, I'm sure you all know what I'm talking about.

I'm guessing this feature is needed so often in so many projects that
it has been implemented already by several people. Does anyone know of
such a stand alone module?
 
B

Ben Finney

Daniel Fetchinson said:
I'm guessing this feature is needed so often in so many projects that
it has been implemented already by several people. Does anyone know of
such a stand alone module?

The 'python-dateutil' library allows easy *programmatic* manipulation
of relative date expressions, but not in natural language.

The closest I know of is the "Chandler Desktop" release notes
<URL:http://chandlerproject.org/Projects/ReleaseNotes> mentioning
"parsing of natural language date and time". You might want to get the
source code and see if their implementation is suitable for your use.
Do be sure to check the license terms for compatibility with your own
intended use of the work.
 
T

tahiriman

or just calculate the difference between the moment you are and the
date before and display the result in a cases (if diff is one day or 2
days or ...)
 
P

Paul McGuire

The 'python-dateutil' library allows easy *programmatic* manipulation
of relative date expressions, but not in natural language.

I think the OP was looking for presentation of dates in a friendly
format, not parsing, but it so happens that I started such a parser
with pyparsing about a month ago, and was about 1/2 finished with it
when I saw your post. I dredged up the code from where it was, and
have a semi-working version now.

NLTK is not needed, as you can constrain and structure the vocabulary
surprisingly well. Probably trickier is to establish your own
conventions for the meanings of "now", "today", etc. (For instance, I
interpret "today" as "the current date, at time 00:00:00".) "A day
from now" is "the current time, with tomorrow's date". "In a day"
could go either way - interpret like "tomorrow" or like "a day from
now." But once these are settled, a reasonable grammar can be
composed.

My first version is fairly brute force and hackish, but I've gotten
some insights into the date arithmetic, and which words behave much
like operators. Of course, this is all driven around English and
English idioms - converting to another language would probably be
starting from scratch.

Here are the tests for this first version (these tests all pass):
today
tomorrow
yesterday
in a couple of days
a couple of days from now
a couple of days from today
in a day
3 days ago
3 days from now
a day ago
now
10 minutes ago
10 minutes from now
in 10 minutes
in a minute
in a couple of minutes
20 seconds ago
in 30 seconds
20 seconds before noon
20 seconds before noon tomorrow
noon
midnight
noon tomorrow

You can see the results and the parser code at:
http://pyparsing.wikispaces.com/UnderDevelopment.

-- Paul
 
B

Ben Finney

Daniel Fetchinson said:
Thanks for all the responses!
Indeed I was looking for presentation and not parsing, I'll take a
look at roundup.

Yes, that's why I indicated that Chandler Desktop would be a good
place to look. Roundup sounds like an equally good place to look.
Hopefully, with a couple of options like that, you'll find something
decent.

It would be good to see a generic "natural-language datetime
presentation" library in the Cheeseshop, since the functionality seems
quite well-suited to a separate generic library.
 
D

Daniel Fetchinson

The OP was looking for presentation though. I know roundup has
Yes, that's why I indicated that Chandler Desktop would be a good
place to look.

I see, I thought that would only be a good place to look for parsing.
Now I downloaded Chandler and quickly found the parsing module they
use but not the one for display but it has to be there I'm sure. They
must use this all over the place.
Roundup sounds like an equally good place to look.

Yes, roundup has a separate module for this and it does the display,
not parsing. This is exactly what I need.
Hopefully, with a couple of options like that, you'll find something
decent.

Yep, I already did :)
It would be good to see a generic "natural-language datetime
presentation" library in the Cheeseshop, since the functionality seems
quite well-suited to a separate generic library.

Exactly, that's what I was mentioning in my original post too.
Actually it looks like the one included in roundup is a stand alone
library for display (but not parsing). It's released under the same
license as python itself maybe they can be convinced to release it as
a stand alone module to the cheeseshop.

Cheers,
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top