Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 744
user3214546 Avatar asked Oct 17 '25 03:10

user3214546


1 Answers

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").

like image 180
Matt Johnson-Pint Avatar answered Oct 18 '25 17:10

Matt Johnson-Pint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!