【问题标题】:Python: How to get the next day of unix timestamps at 00:00 and 23:59?Python:如何在 00:00 和 23:59 获取第二天的 unix 时间戳?
【发布时间】:2016-07-30 06:06:18
【问题描述】:

我的代码中有一个这样的矩阵,带有 unix 时间戳:

event_sequences = [ 
  [1368136883, 1368137089], #The first event is never empty
  [1368214777, 1368214966],
  [],
    .... There are the perfect number of days (empty arrays) in the gaps between the existing events.
  [],
  [1368747495, 1368747833],
  [1368830501, 1368831869]
]

对于 event_sequences 中的每个事件,我知道它的开始和结束(开始 = event_sequences[n][0] 和结束 = event_sequences[n][1])。如您所见,有些事件是空的,我必须采取的方法是将这些空事件的开始和结束设置为记录最后一个事件后的第二天 00:00 和 23:59。喜欢

[ 
 [start, end],
 [start, end],
 [the day after the last event at 00:00, the day after the last event at 23:59]
]

空事件的开始和结束也需要是unix时间戳。我如何在 python 中做到这一点?

【问题讨论】:

标签: python timestamp unix-timestamp


【解决方案1】:

以下应该可以解决问题。不过有几个问题/需要注意的事项:

  • 如果event_sequences 中的第一个元素是空列表,这将失败
  • event_sequences 中是否应该有天数?例如,您会在下面的输出中看到,您的序列在 2013 年 5 月 12 日和 2013 年 5 月 16 日之间存在间隔,即使在空白条目被填充之后也是如此。
  • 如果您有两个连续的空白条目,我假设您希望第二个空白条目在第一个空白条目之后的第二天

代码:

from datetime import datetime, timedelta
import time

event_sequences = [
  [1368136883, 1368137089],
  [1368214777, 1368214966],
  [],
  [],
  [1368747495, 1368747833],
  [1368830501, 1368831869]
]

#take the last_day recorded date as a datetime object from the event_sequences and return a 2-element list
#with the unix timestamps for 00:00 and 23:59
def getNextDay(last_day):
    next_day = last_day + timedelta(days=1)
    next_day_start = next_day.replace(hour=0,minute=0,second=0)
    next_day_end = next_day.replace(hour=23,minute=59,second=0)
    return [int(next_day_start.strftime("%s")), int(next_day_end.strftime("%s"))]

def fillEmptyDates(event_list):
    new_event_list = []
    #note: this will fail if the first element in the list of event_sequences is blank
    last_day = int(time.time())
    for x in event_sequences:
        if len(x) == 0:
            next_day = getNextDay(last_day)
            last_day = datetime.utcfromtimestamp(next_day[1])
        else:
            next_day = x
        last_day = datetime.utcfromtimestamp(next_day[1])
        new_event_list.append(next_day)
    return new_event_list  

new_event_sequence = fillEmptyDates(event_sequences)

print new_event_sequence

#[[1368136883, 1368137089], [1368214777, 1368214966], [1368230400, 1368316740], [1368316800, 1368403140], [1368747495, 1368747833], [1368830501, 1368831869]]

for event in new_event_sequence:
    print str(datetime.utcfromtimestamp(event[0]))+ ' and '+str(datetime.utcfromtimestamp(event[1]))

#2013-05-09 22:01:23 and 2013-05-09 22:04:49
#2013-05-10 19:39:37 and 2013-05-10 19:42:46
#2013-05-11 00:00:00 and 2013-05-11 23:59:00
#2013-05-12 00:00:00 and 2013-05-12 23:59:00
#2013-05-16 23:38:15 and 2013-05-16 23:43:53
#2013-05-17 22:41:41 and 2013-05-17 23:04:29

【讨论】:

  • 是的,我忘了指定:1 第一个元素永远不会为空,2 event_sequences itens 之间存在间隙,正如我所说的必须填充,3 - 2 个空白条目:第二个空白条目得到最后一个条目,即使它是空白的。
  • 我正在使用你的解决方案@Max,但我发现了一个问题。我会编辑帖子给你看。
  • 您的解决方案是正确的,我只是使用 ctime 来验证序列,但是我的本地时间干扰了该功能,它给了我错误的时间字符串。我在网上测试了它,它工作正常。 (http://goo.gl/hz2KUS)
猜你喜欢
  • 2021-07-05
  • 2021-09-04
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-25
  • 2023-02-20
  • 2015-03-21
相关资源
最近更新 更多