473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Intelligent Date & Time parsing

I'm new to python and I was wondering if there are any intelligent
date/time parsing modules out there. I've looked at strptime (or
whichever it is) and mxDateTime from the eGenix package. I need
something to parse user input for a django app, and it's awesome to be
able to write "last monday", "a year ago", or "10pm tuesday" like
PHP's strtotime.

So are there any modules that allow for that kind of parsing?
Mar 7 '08 #1
10 4443
On Mar 7, 4:08 pm, shak...@gmail.com wrote:
I'm new to python and I was wondering if there are any intelligent
date/time parsing modules out there. I've looked at strptime (or
whichever it is) and mxDateTime from the eGenix package. I need
something to parse user input for a django app, and it's awesome to be
able to write "last monday", "a year ago", or "10pm tuesday" like
PHP's strtotime.

So are there any modules that allow for that kind of parsing?
There's the dateutil module that's a fancy wrapper for the datetime
module. The author's site has lots of examples as well as the source:

http://labix.org/python-dateutil

I use it quite a bit.

HTH

Mike
Mar 7 '08 #2
On Mar 7, 4:08 pm, shak...@gmail.com wrote:
I'm new to python and I was wondering if there are any intelligent
date/time parsing modules out there. I've looked at strptime (or
whichever it is) and mxDateTime from the eGenix package. I need
something to parse user input for a django app, and it's awesome to be
able to write "last monday", "a year ago", or "10pm tuesday" like
PHP's strtotime.

So are there any modules that allow for that kind of parsing?
I forgot to say, thanks ahead of time, and I'd appreciate any
direction that you can give me! (How rude of me!)

- Jacob Alheid
Mar 7 '08 #3
On Mar 7, 4:10 pm, Mike Driscoll <kyoso...@gmail.comwrote:
On Mar 7, 4:08 pm, shak...@gmail.com wrote:
I'm new to python and I was wondering if there are any intelligent
date/time parsing modules out there. I've looked at strptime (or
whichever it is) and mxDateTime from the eGenix package. I need
something to parse user input for a django app, and it's awesome to be
able to write "last monday", "a year ago", or "10pm tuesday" like
PHP's strtotime.
So are there any modules that allow for that kind of parsing?

There's the dateutil module that's a fancy wrapper for the datetime
module. The author's site has lots of examples as well as the source:

http://labix.org/python-dateutil

I use it quite a bit.

HTH

Mike
Holy Crap that was fast. I'll check it out - thanks for the help!

Jacob
Mar 7 '08 #4
On Mar 7, 4:10 pm, Mike Driscoll <kyoso...@gmail.comwrote:
On Mar 7, 4:08 pm, shak...@gmail.com wrote:
I'm new to python and I was wondering if there are any intelligent
date/time parsing modules out there. I've looked at strptime (or
whichever it is) and mxDateTime from the eGenix package. I need
something to parse user input for a django app, and it's awesome to be
able to write "last monday", "a year ago", or "10pm tuesday" like
PHP's strtotime.
So are there any modules that allow for that kind of parsing?

There's the dateutil module that's a fancy wrapper for the datetime
module. The author's site has lots of examples as well as the source:

http://labix.org/python-dateutil

I use it quite a bit.

HTH

Mike
So close - I tried it in the interpreter it works great for most
things (like "10pm tuesday") but I'm really hoping for something
that'll work with generics like "tomorrow", "yesterday", "last
tuesday", "next month", etc. Although I know that's pretty language
specific. I was just sort of wishing someone had written it before
me... maybe? Possibly?

Although if I end up writing my own I'll sure use
datetime.parser.parse to get everything left over once you remove
strings like "this saturday". So it helps a little!

Jacob

Mar 7 '08 #5
On Mar 7, 4:22 pm, shak...@gmail.com wrote:
[snip]
Although if I end up writing my own I'll sure use
datetime.parser.parse to get everything left over once you remove
I mean dateutil.parser.parse. Tomorrow I'll have to leave off the last
Lunchtime Guiness.
strings like "this saturday". So it helps a little!

Jacob
And on the same thought - does anyone know of a good website, resource
or community that's got intelligible descriptions of all the different
modules that are generally available? I know if I tab complete 'apt-
get install python-*' it tries to list 1000-some possibilities, so I'm
thinking there's quite a few modules out there that I might like to
know about and use...

Thanks!
Jacob
Mar 7 '08 #6
I figured I might as well share the code I ended up using, in case
anyone else wants an easy way to get strings to, for instance, SQL-
storable datetimes.

jake@house:~$ cat test.py
#!/usr/bin/python
from datetime import datetime
import subprocess

def parsedate( date ):
p = subprocess.Popen(['date','+%s.%N',"--date=%s" %
date],stdout=subprocess.PIPE)
out = float(p.stdout.read())
return "%s" % datetime.fromtimestamp(out)

jake@house:~$ python -i test.py
>>parsedate("today")
'2008-03-07 21:20:31.870489'
>>parsedate("tomorrow")
'2008-03-08 21:20:40.516243'
>>parsedate("next monday")
'2008-03-10 00:00:00'
>>parsedate("10pm last week")
'2008-02-29 22:00:00'
>>parsedate("last tuesday")
'2008-03-04 00:00:00'

Thanks to everyone who helped!

Jacob
Mar 8 '08 #7
On Mar 7, 9:23*pm, shak...@gmail.com wrote:
I figured I might as well share the code I ended up using, in case
anyone else wants an easy way to get strings to, for instance, SQL-
storable datetimes.

jake@house:~$ cat test.py
#!/usr/bin/python
from datetime import datetime
import subprocess

def parsedate( date ):
* * p = subprocess.Popen(['date','+%s.%N',"--date=%s" %
date],stdout=subprocess.PIPE)
* * out = float(p.stdout.read())
* * return "%s" % datetime.fromtimestamp(out)

jake@house:~$ python -i test.py>>parsedate("today")

'2008-03-07 21:20:31.870489'>>parsedate("tomorrow")

'2008-03-08 21:20:40.516243'>>parsedate("next monday")

'2008-03-10 00:00:00'>>parsedate("10pm last week")

'2008-02-29 22:00:00'>>parsedate("last tuesday")

'2008-03-04 00:00:00'
I am a GNU newbie. (I know C &o.) Can you point me to a place to
find the source for 'date'?
Mar 8 '08 #8
I am a GNU newbie. (I know C &o.) Can you point me to a
place to find the source for 'date'?
It's part of the GNU Coreutils:

http://ftp.gnu.org/gnu/coreutils/

Within the file, you're likely interested in lib/getdate.*

It helps if you have a working knowledge of Yacc.

-tkc
Mar 8 '08 #9
sh*****@gmail.com writes:
>I'm new to python and I was wondering if there are any intelligent
date/time parsing modules out there. I've looked at strptime (or
whichever it is) and mxDateTime from the eGenix package. I need
something to parse user input for a django app, and it's awesome to be
able to write "last monday", "a year ago", or "10pm tuesday" like
PHP's strtotime.
>So are there any modules that allow for that kind of parsing?
http://code-bear.com/code/parsedatetime/
Mar 10 '08 #10
On Mar 7, 5:26*pm, shak...@gmail.com wrote:
On Mar 7, 4:22 pm, shak...@gmail.com wrote:
[snip]Although if I end up writing my own I'll sure use
datetime.parser.parse to get everything left over once you remove

I mean dateutil.parser.parse. Tomorrow I'll have to leave off the last
Lunchtime Guiness.
strings like "this saturday". So it helps a little!
Jacob

And on the same thought - does anyone know of a good website, resource
or community that's got intelligible descriptions of all the different
modules that are generally available? I know if I tab complete 'apt-
get install python-*' it tries to list 1000-some possibilities, so I'm
thinking there's quite a few modules out there that I might like to
know about and use...

Thanks!
Jacob
Jacom, the http://www.python.org/ site offers most of the modules,
including an index: http://pypi.python.org/pypi

Then there's the ASPN site:

http://aspn.activestate.com/ASPN/Downloads/ActivePython

Mar 10 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
8
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
10
by: Newsscanner | last post by:
Hello, In my MySQL database, one of the fields eople have to fill in is "DOB" (Date of Birth). I have now managed to begin inserting data into my DB via a form, the data type for the DOB field...
6
by: Kalle Anke | last post by:
I want to parse a date string, for example '2005-09-23', and since I haven't done this before I would like to ask what is the best way to do it. I've looked around and the dateutil seems to be...
4
by: Ekong, Samuel Akpan | last post by:
Hi, I am parsing a binary file and intend to read a date(not time) field encoded thus: 0xA1290B(little-endian). Now I know the date to be 12/04/2003 but just cannot get any of the known datetime...
14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
21
by: rdemyan via AccessMonster.com | last post by:
Is there a way to get the internet date/time. I saw an article that uses WinSock, but WinSock doesn't seem to be available in Access. I want to verify that the date/time on the local PC running...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.