简体   繁体   English

如何在Python中为每分钟创建UNIX时间戳

[英]How to create a UNIX timestamp for every minute in Python

I want to create a UNIX timestamp for the date 2014-10-31 for every minute of the day. 我想为一天中的每一分钟创建一个日期为2014-10-31的UNIX时间戳。 I have got a timestamp for the date but not for every minute - 我有日期的时间戳,但不是每分钟-

import datetime
date = '2014-10-31'
t_stamp = int(time.mktime(datetime.datetime.strptime(date, "%Y-%m-%d").timetuple()))
print t_stamp

I want to save all these entries in a file which I need to access later to get specific entries only. 我想将所有这些条目保存在一个文件中,以后我需要访问该文件以仅获取特定条目。

If I need to access one of the timestamp records using a date and specific time, how can it be done? 如果我需要使用日期和特定时间访问时间戳记录之一,该怎么办? For example, I need to find the entries for 2014-31-10 for the time between 20:00 and 20:05 , how can it be done? 例如,我需要在20:0020:05之间找到2014-31-10的条目,该怎么办?

Any help would be appreciated! 任何帮助,将不胜感激!

Updated: 更新:

import datetime
import ipaddress
from random import randint

for x in ip_range.hosts():

    for h in range(24): #24 hours
        for i in range(60): # 60 minutes
            f.write(str(t_stamp)+'\t'+str(x)+'\t0\t'+str(randint(0,100))+'%\n')
            f.write(str(t_stamp)+'\t'+str(x)+'\t1\t'+str(randint(0,100))+'%\n')

            t_stamp += 60 # one minute

Now I am looking to create a log file for every minute. 现在,我希望每分钟创建一个日志文件。 How can that be done? 那怎么办?

t_stamp = int(time.mktime(datetime.datetime.strptime(date, "%Y-%m-%d").timetuple()))
for h in range(24): 24 hours
    for i in range(60): # 60 minutes
        print t_stamp
        t_stamp += 60 # one minute

Hope this helps. 希望这可以帮助。

Storing timestamps in a database is simply redundant unless the required values cannot be corelated ie billing transactions' time. 将时间戳存储在数据库中只是多余的,除非不能将所需的值关联起来(即记帐交易时间)。 Here, your required range values have a start and end time, and you know them. 在这里,您所需的范围值具有开始和结束时间,并且您知道它们。 You also know the interval between each value is 1 minute. 您还知道每个值之间的间隔是1分钟。

import datetime

tvalue = datetime.datetime(2014,10,31,0,0).timestamp()  #yy mm dd 0 0

startval = ((REQD_HOUR)*3600 + (REQD_MIN)*60) #required start time

endval = ((REQD_HOUR_E)*3600 + (REQD_MIN_E)*60) # range end time

while (endval > startval):
 print(tvalue+startval)
 startval+=60

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM