超过24小时的Python时间对象

发布于 2024-09-02 21:01:54 字数 223 浏览 3 评论 0原文

我的 Linux 超时时间为 hh:mm:sec,但 hh 可能大于 24 小时。因此,如果时间为 1 天 12 小时,则为 36:00:00。有没有办法采用这种格式并轻松制作时间对象?

我真正想做的是获取所需的时间,即 36:00:00,以及它运行的时间 4:46:23,然后减去两者以获得剩余时间。我认为时间增量可能是在 Python 中执行此操作的最方便的方法,但我也愿意接受其他建议。

谢谢。

I have a time out of Linux that is in hh:mm:sec, but the hh can be greater than 24 hours. So if the time is 1 day 12 hours, it would be 36:00:00. Is there a way to take this format and easily make a time object?

What I would really like to do is take the the required time i.e. 36:00:00, and the time that it has been running 4:46:23, and subtract the two to get the time remaining. I figured time delta might be the most convenient way to do this in Python, but I'd also be open to other suggestions.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

草莓酥 2024-09-09 21:01:54

timedelta 确实是你想要的。这是一个更完整的示例,可以满足您的要求。

>>> import datetime
>>> a = datetime.timedelta(hours=36)
>>> b = datetime.timedelta(hours=4, minutes=46, seconds=23)
>>> c = a - b
>>> print c
1 day, 7:13:37

timedelta is indeed what you want. Here is a more complete example that does what you asked.

>>> import datetime
>>> a = datetime.timedelta(hours=36)
>>> b = datetime.timedelta(hours=4, minutes=46, seconds=23)
>>> c = a - b
>>> print c
1 day, 7:13:37
所谓喜欢 2024-09-09 21:01:54

您需要的是 timedelta 对象: http://docs.python.org/library /datetime.html#timedelta-objects

36 小时:

d = timedelta(hours=36)

What you need is the timedelta object: http://docs.python.org/library/datetime.html#timedelta-objects

36 hours:

d = timedelta(hours=36)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文