简体   繁体   中英

Python method to find time stamp difference to compute even time intervals

What would be a good way to determine the time stamp for a rounded time frame? For example I pull a timestamp from a database of 1388898695 which translates to "2014-01-04 19:11:35".

Now lets say I want to find an earlier record that is even minutes in the past. So start with 2014-01-04 19:10:00. How would I find out the number of seconds from the timestamp (in this case 95) and subtract it from 13388898695 so that I can search the database for the 19:10:00 timestamp?

I'm looking for a general method because I'll want to compute different time frames, like minutes by 5's or 15's.


Ok this isn't very pretty, but let's ignore year/month/day roll overs and just try to find the past even interval of 2 minutes. So 19:53:12 would trigger a search for 19:52:00 (to 19:50:00).

#(2014-01-04 19:53:12)
time1=1388901192

year=int(datetime.datetime.fromtimestamp(time1).strftime('%Y'))
month=int(datetime.datetime.fromtimestamp(time1).strftime('%m'))
day=int(datetime.datetime.fromtimestamp(time1).strftime('%d'))
hours=int(datetime.datetime.fromtimestamp(time1).strftime('%H'))
minutes=int(datetime.datetime.fromtimestamp(time1).strftime('%M'))
seconds=0

if minutes%2 == 0:
    minutes-=2
else:
    minutes-=1

timeString="%s-%s-%s %s:%s:00" % (year,month,day,hours,minutes)
newTime=time.mktime(time.strptime(timeString, '%Y-%m-%d %H:%M:%S'))

print newTime, time1
print datetime.datetime.fromtimestamp(newTime).strftime('%Y-%m-%d %H:%M:%S')

Produces the search time of 1388901120.0 or 2014-01-04 19:52:00

It seems like this is not a very clean way to do this.


Here's my generic solution using the method from Burhan Khalid:

# take timestamp, rewind to timeframe that provides the newest end point where a full interval
# 10:42:34 -> 10:42:00 for interval=2 to fetch records 10:42:00 to 10:40:00
# 10:49:34 -> 10:40:00 for interval=10 to fetch records 10:40:00 to 10:30:00
dt = datetime.datetime.fromtimestamp(ts)
if (dt.minute % interval) == 0:
    prev_ts = dt-datetime.timedelta(minutes=0,seconds=dt.second)
else:
    temp=round(dt.minute/interval)
    temp*=interval
    temp=dt.minute-temp
    prev_ts = dt-datetime.timedelta(minutes=temp,seconds=dt.second)
print "%s: %s -> %s" % (interval, dt, prev_ts)
print time.mktime(prev_ts.timetuple())

Here is an example to get even minutes, it should get you started with your other cases:

>>> import datetime
>>> ts = 1388898695
>>> dt = datetime.datetime.fromtimestamp(ts)
>>> dt.hour,dt.minute,dt.second
(8, 11, 35)
>>> next_ts = dt+datetime.timedelta(minutes=1,seconds=-dt.second)
>>> next_ts.hour,next_ts.minute,next_ts.second
(8, 12, 0)
>>> prev_ts = dt-datetime.timedelta(minutes=1,seconds=dt.second)
>>> prev_ts.hour,prev_ts.minute,prev_ts.second
(8, 10, 0)

Check out http://docs.python.org/3/library/datetime.html#datetime.timedelta

#!/usr/local/cpython-3.3/bin/python

import time
import datetime

def main():
    time0 = time.time()
    time.sleep(3)
    time1 = time.time()

    print(datetime.timedelta(seconds=(time1 - time0)))

main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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