Should time range checks be inclusive or exclusive?

时间:2019-01-18 18:50:37

标签: python

The general convention for a generic range (x,y) is that x is inclusive and y is exclusive.

For Python datetime.time type, datetime.time.max is time(23, 59, 59, 999999), so it doesn't seem to allow to use the conventional range check on the upper end. For example, if I want to check a time range between 10 am and midnight, I might want a range like this: (time(10), time(24)). But time(24) is not valid, even as a sentinel value.

On the other hand, we can't make x exclusive and y inclusive, because then we lose time(0) as a value.

Should range checks on time be inclusive? Something about it doesn't seem right to me, but I can't articulate it.

1 个答案:

答案 0 :(得分:0)

午夜使用0

代替:

(time(10), time(24))

使用:

# From midnight (0:00) to 10am (10:00)
(time(0), time(10))