Read time and date from a text file

Peter Otten __peter__ at web.de
Wed Nov 24 08:45:13 EST 2010


huisky wrote:

> I see the problem of year. But the question is the source file does
> NOT provide the year information.
> for instance if i have one record as ['Dec','6','21:01:17'], and the
> other as ['Jan','6','21:01:17']
> these two records may be in different year. Will it be a problem to
> use the 'datetime' do the time difference calculation?

You have a problem that is independent of Python. You have to guess the 
correct year to get the correct time interval. One approach would be to use 
the year from the file's timestamp, then calculate the differences, and 
whenever you get a negative interval add one year to the end date.

This will still result in rare ValueErrors (Feb 29 in non-leapyears) and 
some silent miscalculations; I fear you'll have to live with that.

Peter



More information about the Python-list mailing list