user3214546
user3214546

Reputation: 6831

Python time comparison at midnight

I have to save the time in AM PM format.

But i am having trouble in deciding how to enter midnight time.

Suppose the time is 9PM to 6AM next morning. I have to divide it into day to day basis . Like this

t1 = datetime.datetime.strptime('09:00PM', '%I:%M%p').time()

t2 = datetime.datetime.strptime('12:00AM', '%I:%M%p').time()

t3 = datetime.datetime.strptime('06:00AM', '%I:%M%p').time()

Now i want to know whether the t2 should be

12:00 AM or 11.59PM

If i do 12:00AM then i can't compare if 9pm > 12am but 11.59 looks odd or may be it is right way

Upvotes: 4

Views: 7469

Answers (2)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241485

You should always use 00:00 (or 12:00 AM) to represent midnight.

Using 23:59 (or 11:59 PM) is problematic for a couple of reasons:

  • Precision matters in the comparison. Is 23:59:01 not before midnight? What about 23:59:59.9999?

  • Duration calculation will be thrown off by whatever precision you chose. Consider that 10:00 pm to midnight is 2 hours, not 1 hour and 59 minutes.

To avoid these problems, you should always treat time intervals as half-open intervals. That is, the range has an inclusive start, and an exclusive end. In interval notation: [start, end)

Now with regard to crossing the midnight boundary:

  • When you are comparing times that are associated with a date, you can just compare directly:

    [2015-01-01T21:00,  2015-01-02T06:00) = 9 hours
     2015-01-01T21:00 < 2015-01-02T06:00 
    
  • When you do not have a date, you can determine duration, but you cannot determine order!

    [21:00, 06:00) = 9 hours
     21:00 < 06:00  OR  21:00 > 06:00
    

    The best you can do is determine whether a time is between the points covered by the range.

    Both 23:00 and 01:00 are in the range [21:00, 06:00)
    21:00 is also in that range, but 06:00 is NOT.
    

    Think about a clock. It's modeled as a circle, not as a straight line.

  • To calculate duration of a time-only interval that can cross midnight, use the following pseudocode:

    if (start <= end)
        duration = end - start
    else
        duration = end - start + 24_hours
    

    Or more simply:

    duration = (end - start + 24_hours) % 24_hours
    
  • To determine whether a time-only value falls within a time-only interval that can cross midnight, use this pseudocode:

    if (start <= end)
        is_between = start <= value AND end > value
    else
        is_between = start <= value OR  end > value
    

Note that in the above pseudocode, I am referring to the magnitude of the values, as compared numerically - not the logical time values which, as said earlier, cannot be compared independently without a reference date.

Also, much of this is covered in my Date and Time Fundamentals course on Pluralsight (towards the very end, in "Working With Ranges").

Upvotes: 10

Michael Lang
Michael Lang

Reputation: 3992

How about making t1 = 09:00PM, t2 = 11.59PM, t3 = 12:00AM and t4 = 06:00AM. Then you have definite time ranges per day. Of course, adding the date would make time differences evident as well.

Upvotes: 0

Related Questions