在 Python 中将 N 秒添加到 datetime.time 的标准方法是什么? [英] What is the standard way to add N seconds to datetime.time in Python?

查看:29
本文介绍了在 Python 中将 N 秒添加到 datetime.time 的标准方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定 Python 中的 datetime.time 值,是否有一种标准方法可以向其添加整数秒数,以便 11:34:59 + 3= 11:35:02,例如?

这些显而易见的想法行不通:

<预><代码>>>>日期时间.时间(11, 34, 59) + 3类型错误:不支持 + 的操作数类型:'datetime.time' 和 'int'>>>datetime.time(11, 34, 59) + datetime.timedelta(0, 3)类型错误:+ 不支持的操作数类型:datetime.time"和datetime.timedelta">>>datetime.time(11, 34, 59) + datetime.time(0, 0, 3)类型错误:不支持 + 的操作数类型:'datetime.time' 和 'datetime.time'

最后我写了这样的函数:

def add_secs_to_time(timeval, secs_to_add):秒 = timeval.hour * 3600 + timeval.minute * 60 + timeval.second秒 += secs_to_addreturn datetime.time(secs//3600, (secs % 3600)//60, secs % 60)

我不禁想到我错过了一种更简单的方法来做到这一点.

相关

解决方案

您可以将完整的 datetime 变量与 timedelta 一起使用,并通过提供一个虚拟日期然后使用 time 获取时间值.

例如:

导入日期时间a = datetime.datetime(100,1,1,11,34,59)b = a + datetime.timedelta(0,3) # 天,秒,然后是其他字段.打印(a.time())打印(b.time())

产生两个值,相隔三秒:

11:34:5911:35:02

你也可以选择更易读的

b = a + datetime.timedelta(seconds=3)

如果你愿意的话.

<小时>

如果您想要一个可以执行此操作的函数,您可以考虑使用下面的 addSecs:

导入日期时间def addSecs(tm, secs):fulldate = datetime.datetime(100, 1, 1, tm.hour, tm.minute, tm.second)fulldate = fulldate + datetime.timedelta(seconds=secs)返回 fulldate.time()a = datetime.datetime.now().time()b = addSecs(a, 300)打印(一)打印(b)

输出:

 09:11:55.77569509:16:55

Given a datetime.time value in Python, is there a standard way to add an integer number of seconds to it, so that 11:34:59 + 3 = 11:35:02, for example?

These obvious ideas don't work:

>>> datetime.time(11, 34, 59) + 3
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'int'
>>> datetime.time(11, 34, 59) + datetime.timedelta(0, 3)
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'
>>> datetime.time(11, 34, 59) + datetime.time(0, 0, 3)
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.time'

In the end I have written functions like this:

def add_secs_to_time(timeval, secs_to_add):
    secs = timeval.hour * 3600 + timeval.minute * 60 + timeval.second
    secs += secs_to_add
    return datetime.time(secs // 3600, (secs % 3600) // 60, secs % 60)

I can't help thinking that I'm missing an easier way to do this though.

Related

解决方案

You can use full datetime variables with timedelta, and by providing a dummy date then using time to just get the time value.

For example:

import datetime
a = datetime.datetime(100,1,1,11,34,59)
b = a + datetime.timedelta(0,3) # days, seconds, then other fields.
print(a.time())
print(b.time())

results in the two values, three seconds apart:

11:34:59
11:35:02

You could also opt for the more readable

b = a + datetime.timedelta(seconds=3)

if you're so inclined.


If you're after a function that can do this, you can look into using addSecs below:

import datetime

def addSecs(tm, secs):
    fulldate = datetime.datetime(100, 1, 1, tm.hour, tm.minute, tm.second)
    fulldate = fulldate + datetime.timedelta(seconds=secs)
    return fulldate.time()

a = datetime.datetime.now().time()
b = addSecs(a, 300)
print(a)
print(b)

This outputs:

 09:11:55.775695
 09:16:55

这篇关于在 Python 中将 N 秒添加到 datetime.time 的标准方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆