午夜的Python时间比较 [英] Python time comparison at midnight

查看:48
本文介绍了午夜的Python时间比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将时间保存为 AM PM 格式.

I have to save the time in AM PM format.

但是我在决定如何输入午夜时间时遇到了麻烦.

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

假设时间是第二天早上9点至凌晨6点.我必须将它分为日常基础.像这样

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()

现在我想知道t2是否应该

Now i want to know whether the t2 should be

12:00 AM 11.59PM

如果我是12:00 AM,那么如果 9pm>我无法比较.上午12点,但11.59看起来很奇怪,或者可能是正确的方式

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

推荐答案

您应始终使用 00:00 (或 12:00 AM )表示午夜.

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

使用 23:59 (或 11:59 PM )存在问题,原因有几个:

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

  • 精度在比较中很重要. 23:59:01 是不是在午夜之前?怎么样 23:59:59.9999 ?

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

您选择的任何精度都会拖延工期计算.考虑到午夜的 10:00 pm 是2小时,而不是1小时59分钟.

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)

现在要穿越午夜边界:

  • 在比较与日期关联的时间时,您可以直接比较:

  • 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
    

    或更简单地说:

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

    这篇关于午夜的Python时间比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆