python datetime.time 操作

发布于 2024-08-23 17:39:08 字数 555 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深居我梦 2024-08-30 17:39:08

假设时间位于同一时区并且没有 DST

import datetime

def minute_interval(start, end):
     reverse = False
     if start > end:
          start, end = end, start
          reverse = True

     delta = (end.hour - start.hour)*60 + end.minute - start.minute + (end.second - start.second)/60.0
     if reverse:
          delta = 24*60 - delta
     return delta

t1 = datetime.time(12, 10, 0) # 12:10
t2 = datetime.time(13, 13, 0) #13:13
t3 = datetime.time(23, 55, 0) #23:55 
t4 = end = datetime.time(01, 10, 0) #01:10

print minute_interval(t1, t2)
print minute_interval(t3, t4)

输出:

63
75

否则您最好使用 datetime.datetime,它支持减法并提供 datetime.timedelta,对于时区,您可以使用 pytz 库。

Assuming time are in same timezone and no DST

import datetime

def minute_interval(start, end):
     reverse = False
     if start > end:
          start, end = end, start
          reverse = True

     delta = (end.hour - start.hour)*60 + end.minute - start.minute + (end.second - start.second)/60.0
     if reverse:
          delta = 24*60 - delta
     return delta

t1 = datetime.time(12, 10, 0) # 12:10
t2 = datetime.time(13, 13, 0) #13:13
t3 = datetime.time(23, 55, 0) #23:55 
t4 = end = datetime.time(01, 10, 0) #01:10

print minute_interval(t1, t2)
print minute_interval(t3, t4)

output:

63
75

otherwise you are better of using datetime.datetime, which supports subtraction and gives datetime.timedelta, for timezone you can use pytz library.

舞袖。长 2024-08-30 17:39:08

这怎么样?

def minute_interval(start,end):
    start_sec= (start.hour*60+start.minute)*60+start.second
    end_sec= (end.hour*60+end.minute)*60+end.second
    return (end_sec-start_sec)/60.0

How's this?

def minute_interval(start,end):
    start_sec= (start.hour*60+start.minute)*60+start.second
    end_sec= (end.hour*60+end.minute)*60+end.second
    return (end_sec-start_sec)/60.0
假情假意假温柔 2024-08-30 17:39:08
>>> import datetime
>>> t1 = datetime.time(12, 10, 0)
>>> t2 = datetime.time(13, 13, 0)
>>> today = datetime.datetime.today()
>>> t1n = datetime.datetime.combine(today, t1)
>>> t2n = datetime.datetime.combine(today + datetime.timedelta(days=int(t2 < t1)), t2)
>>> d = t2n - t1n
>>> (d.days * 86400 + d.seconds) // 60
63
>>> import datetime
>>> t1 = datetime.time(12, 10, 0)
>>> t2 = datetime.time(13, 13, 0)
>>> today = datetime.datetime.today()
>>> t1n = datetime.datetime.combine(today, t1)
>>> t2n = datetime.datetime.combine(today + datetime.timedelta(days=int(t2 < t1)), t2)
>>> d = t2n - t1n
>>> (d.days * 86400 + d.seconds) // 60
63
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文