0

I want to create a UNIX timestamp for the date 2014-10-31 for every minute of the day. 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?

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?

7
  • 1
    Sounds like you want a database more than a file, considering there are 1440 minutes in 24 hours, making all the timestamps should be pretty easy using that. Commented Aug 4, 2016 at 21:25
  • @PadraicCunningham Yes, I want to save server logs based on timestamps, IP addresses and server usages. I am not sure of the syntax how to do it for every minute. Could you help me with that? Commented Aug 4, 2016 at 21:27
  • If you are going to be doing this over a long period then I would look into using sqlite or mysql, why do you need the timestamps all created? Commented Aug 4, 2016 at 21:28
  • @PadraicCunningham No it's just a one-time thing, need to generate logs once and find a few instances based on timestamps. Commented Aug 4, 2016 at 21:29
  • if you want to find out where a date/timestamp lands, you can keep an ordered list of datetimes/timestamps and bisect Commented Aug 4, 2016 at 21:38

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

2 Comments

Or for i in range(24*60) ;)
Thanks! Is there a way to create a new log file for every minute? Is that a good practice?
-1

Hope this helps.

Storing timestamps in a database is simply redundant unless the required values cannot be corelated i.e. 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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.