擅长:python、mysql、java
<p><code>abs(current_time)</code>忽略<code>x</code>是在<code>current_time</code>之前还是之后,因此它返回最近的时隙,而不是从<code>current_time</code>之前开始的最近的时隙</p>
<p>如果时间段已排序,只需循环,直到找到一个小于或等于当前时间的时间段</p>
<pre><code>for slot in time:
if slot <= current_time:
print(slot.strftime('%H:%M'))
break
</code></pre>
<p>如果它们尚未排序,请使用<code>for slot in sorted(time):</code></p>