[Tutor] Comparing times

John Fouhy john at fouhy.net
Tue Jul 18 22:59:17 CEST 2006


On 18/07/06, Steve Nelson <sanelson at gmail.com> wrote:
> What I want to do is establish if the time of the process is *later*
> than the system date.  For example, we might have a process with a
> time of 11:15:00, when the system time is 10:00:00.  In practice this
> means that the process is from 11am yesterday.
>
> Other than splitting the string up more and saying is 11 bigger than
> 10, how can I go about this?

Have a look at time.strptime.  Eg, if s is '11:15:00' then
time.strptime(s, '%H:%M:%S') will be a tuple of 9 integers which you
should be able to compare directly with other time tuples.

# untested
def cmptime(s1, s2):
    """Compare two times as strings in %H:%M:%S format."""
    return cmp(time.strptime(s1, '%H:%M:%S'), time.strptime(s2, '%H:%M:%S'))

-- 
John.


More information about the Tutor mailing list