问题描述
我需要检查当前时间是否在时间范围内.最简单的情况time_end > time_start:
I need to check if the current time is in timerange. The most simple case time_end > time_start:
但是当用户进入结束时间小于开始时间的时间范围时,麻烦就开始了,例如23:00 - 06:00".像00:00"这样的时间将在此范围内.大约 5 年前,我编写了这个 PHP 函数:
But troubles begin when user enters a time range when the end time is smaller than the start time, e.g. "23:00 - 06:00". A time like '00:00' will be in this range. About 5 years ago I wrote this PHP function:
现在我需要做同样的事情,但我想让它看起来更好看.那么,我应该使用什么算法来确定当前时间 '00:00' 是否在 reversed 范围内,例如['23:00', '01:00']
?
Now I need to do the same thing, but I want to make it good looking. So, what algorithm should I use to determine if the current time '00:00' is in reversed range e.g. ['23:00', '01:00']
?
推荐答案
Python 解决方案将会非常非常短.
The Python solution is going to be much, much shorter.
对 start
、end
和 x
使用 datetime.time
类.
Use the datetime.time
class for start
, end
, and x
.
这篇关于如何检查当前时间是否在python的范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!