python datetime.time 操作
t1 = datetime.time(12, 10, 0, tzinfo=GMT1()) # 12:10
t2 = datetime.time(13, 13, 0, tzinfo=GMT1()) #13:13
t3 = datetime.time(23, 55, 0, tzinfo=GMT1()) #23:55
t4 = datetime.time(01, 10, 0, tzinfo=GMT1()) #01:10
我需要两次之间的分钟间隔。例如,一个非工作时间:
def minute_interval(start,end):
return end - start
minute_interval(t1,t2) #should give 63 mins.
此外,如果结束时间小于开始时间,则应该通过了解结束时间是从第二天的时间开始来进行计算。即:
minute_interval(t3,t4) #should give 75 mins.
如何实现这一点?为此,我需要重写 分钟间隔 函数。
t1 = datetime.time(12, 10, 0, tzinfo=GMT1()) # 12:10
t2 = datetime.time(13, 13, 0, tzinfo=GMT1()) #13:13
t3 = datetime.time(23, 55, 0, tzinfo=GMT1()) #23:55
t4 = datetime.time(01, 10, 0, tzinfo=GMT1()) #01:10
I need the minute interval between between two times. For instance a non working one:
def minute_interval(start,end):
return end - start
minute_interval(t1,t2) #should give 63 mins.
Also if the end time is smaller than start, it should do the calculation by understaing the end is from the next days time. ie:
minute_interval(t3,t4) #should give 75 mins.
How can this be achieved ? I need to rewrite the minute_interval function for this aim.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设时间位于同一时区并且没有 DST
输出:
否则您最好使用 datetime.datetime,它支持减法并提供 datetime.timedelta,对于时区,您可以使用 pytz 库。
Assuming time are in same timezone and no DST
output:
otherwise you are better of using datetime.datetime, which supports subtraction and gives datetime.timedelta, for timezone you can use pytz library.
这怎么样?
How's this?