如何在 Python 中生成未来日期时间序列并从集合中确定最近的日期时间

发布于 2024-12-03 01:35:43 字数 259 浏览 3 评论 0原文

我需要在 Python 中生成四个日期时间对象:

"The next instance of 5:30AM EST"
"The next instance of 8:30AM EST"
"The next instance of 1:00PM EST"
"The next instance of 5:30PM EST"

然后我需要找到其中哪个最接近当前日期/时间。

我希望我能说我有一些起始代码,但我不知道从哪里开始。

I need to generate four datetime objects in Python:

"The next instance of 5:30AM EST"
"The next instance of 8:30AM EST"
"The next instance of 1:00PM EST"
"The next instance of 5:30PM EST"

Then I need to find which of those is closest to the current date/time.

I wish I could say I have some starting code, but I have no idea where to start on this one.

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

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

发布评论

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

评论(2

别靠近我心 2024-12-10 01:35:43

这应该可以帮助您开始。我将当前时间作为日期时间传递到函数中,因此如果参数采用 EST,这应该可以工作。

def find_next(cur_dt):
    import datetime as dt
    t = [dt.time(5,30), dt.time(8,30), dt.time(13,0), dt.time(17,30)]

    cur_t = cur_dt.time()
    cur_d = cur_dt.date()

    for i in range(len(t)):
        if t[i] > cur_t:
            rt = [t[(j+i)%len(t)] for j in range(len(t))]
            rd = [cur_d] * (len(t)-i) + [cur_d + dt.timedelta(days=1)]*i
            return [dt.datetime.combine(rd[j],rt[j]) for j in range(len(rt))]

    # everything happens tomorrow        
    return [dt.datetime.combine(cur_d + dt.timedelta(days=1), i) for i in t]

结果将是按顺序排列的对象,从“最快”的对象开始,依此类推。

This should get you started. I have the current time being passed into the function as a datetime, so if the argument is in EST, this should just work.

def find_next(cur_dt):
    import datetime as dt
    t = [dt.time(5,30), dt.time(8,30), dt.time(13,0), dt.time(17,30)]

    cur_t = cur_dt.time()
    cur_d = cur_dt.date()

    for i in range(len(t)):
        if t[i] > cur_t:
            rt = [t[(j+i)%len(t)] for j in range(len(t))]
            rd = [cur_d] * (len(t)-i) + [cur_d + dt.timedelta(days=1)]*i
            return [dt.datetime.combine(rd[j],rt[j]) for j in range(len(rt))]

    # everything happens tomorrow        
    return [dt.datetime.combine(cur_d + dt.timedelta(days=1), i) for i in t]

The result will be the objects, in order, starting with the "soonest" one, and so on.

诗酒趁年少 2024-12-10 01:35:43

这看起来可能是一个家庭作业问题。这些示例代码足以帮助您入门吗?它可能不是最有效的,但它会起作用。

from datetime import datetime, time, timedelta
now = datetime.now()
today = datetime.date(now)
tomorrow = today + timedelta(days=1)

time_a = time  (4, 0)
today_a = datetime.combine(today, time_a)
tomorrow_a = datetime.combine(tomorrow, time_a)
if (today_a - now)>timedelta(0):
    print "%s is in the future" % today_a 

if (tomorrow_a - now)>timedelta(0):
    print "%s is in the future" % tomorrow_a 

对于时间列表“t”,您可以使用:
t = [时间(5,30), 时间(8,30), 时间(13,0), 时间(17,30)]
now = 日期时间.now()

today = [x for x in t if datetime.combine(today, x) > now]
not_today = set(t) - set(today)

This seems like it might be a homework question. Is this enough sample code to get you started? It's probably not the MOST efficient, but it'll work.

from datetime import datetime, time, timedelta
now = datetime.now()
today = datetime.date(now)
tomorrow = today + timedelta(days=1)

time_a = time  (4, 0)
today_a = datetime.combine(today, time_a)
tomorrow_a = datetime.combine(tomorrow, time_a)
if (today_a - now)>timedelta(0):
    print "%s is in the future" % today_a 

if (tomorrow_a - now)>timedelta(0):
    print "%s is in the future" % tomorrow_a 

For a list of times, "t", you can use:
t = [time(5,30), time(8,30), time(13,0), time(17,30)]
now = datetime.now()

today = [x for x in t if datetime.combine(today, x) > now]
not_today = set(t) - set(today)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文