views:

125

answers:

2

I'm working on a journal-type application in Python. The application basically permits the user write entries in the journal and adds a time-stamp for later querying the journal.

As of now, I use the time.ctime() function to generate time-stamps that are visually friendly. The journal entries thus look like:

Thu Jan 21 19:59:47 2010 Did something
Thu Jan 21 20:01:07 2010 Did something else

Now, I would like to be able to use these time-stamps to do some searching/querying. I need to be able to search, for example, for "2010", or "feb 2010", or "23 feb 2010".

My questions are:

1) What time module(s) should I use: time vs datetime?

2) What would be an appropriate way of creating and using the time-stamp objects?

Many thanks!

+6  A: 

Option 1: Don't change anything. Use time.strptime to parse your timestamps.

Option 2: Change to datetime. You can format the timestamps the same way, and use datetime.strptime to parse them.

It doesn't much matter, since the code will be similar. Searching will involve matching months, days or years or some such. Use time tuples for this. Or datetime.datetime objects. Or, searching will involve comparing ranges of times; use time in seconds for this. Or use datetime objects. They will both work and -- for this -- they will be similar in complexity.

Doing date calculations (90 days in the future, 3 months in the past, etc.) is the strong suit of datetime.

In general, you'll often be happier with datetime because it does more and doesn't involve switching between simple double time and time tuple time.

S.Lott
Thanks for your great reply. I'll put time going through the datetime documentation.
Morlock
+8  A: 

You might want to consider changing to ISO 8601. Will help with sorting for example, or transferring data between different systems.

Bandi-T
totally agree. so much more readable and also causes less fuss when using different tools.
pulegium
I will use ISO 8601 in the journal file, and thanks a log for the suggestion! However, the question really is a question about which python module I should use to deal with dates and time. If you have a way I could implement ISO 8601 with a python module from the standard library, I would be glad to hear about it!Cheers
Morlock
Here's a question doing the opposite, parsing ISO 8601 in Python using the datetime module: http://stackoverflow.com/questions/969285/how-do-i-translate-a-iso-8601-datetime-string-into-a-python-datetime-object - note the comment on looking at the methods of datetime.datetime instead of the datetime module itself. If you're lucky, you'll find an strftime() function that accepts the same (or very similar) format syntax. I seem to recall I once did something like this, and the only custom thing I had to do was tack on fractional seconds (as they were needed by my application at the time).
Bandi-T
Don't forget though that Pythons built-in date/time objects are relatively naive, and always assume there's exactly 60 seconds in a minute (3600*24 seconds in a day) - this ignores leap seconds for example.
Bandi-T
Here's a "Date and Time Representation in Python" from Jochen Voss: http://seehuhn.de/pages/pdate
Bandi-T
Thanks for the seehuhn link Bandi-T
Morlock