How to take a time.time() variable and round the minutes down to 0?
#1
Would love any/all help on the following python-specific question (I've googled around a ton and can't seem to find an answer).

I continue to develop the SageTV Addon and am working on a new enhancement to show upcoming shows for specific time slots. e.g. I can browse time slots like 9-10AM, 10-11AM, etc and then when I click that, it shows me the upcoming shows across channels.

Code:
now = time.time()
airTime = strftime('%H:%M', time.localtime(now))
displayText = strftime('%a %b %d', time.localtime(now)) + " @ " + airTime  #Which outputs something like "Wed May 14 @ 8:07"

Ideally what I want to do is if it's 8:07AM, I'd like to change the time object to "round down" to make the minutes always 0... so I'd like the "now" variable to be 8:00AM and not 8:07AM.

Thoughts on how best to do this?
Reply
#2
Why not just hard code the zero's?

airTime = strftime('%H:00', time.localtime(now))

If you want to break it into quarter-hours then

rounded_minutes = (int(time.strftime('%M', time.localtime(now))) / 15) * 15

airTime = time.strftime('%H', time.localtime(now)) + ":%02d" % rounded_minutes
Reply
#3
(2014-05-14, 14:43)Karnagious Wrote: Why not just hard code the zero's?

airTime = strftime('%H:00', time.localtime(now))

If you want to break it into quarter-hours then

rounded_minutes = (int(time.strftime('%M', time.localtime(now))) / 15) * 15

airTime = time.strftime('%H', time.localtime(now)) + ":%02d" % rounded_minutes

I can't hard code it from a formatting perspective because I need to do some calculations from it. But I think I figured it out. You can't modify the time_struct object that is returned by time.localtime, but here's a workaround that I found (thanks for the thoughts though):
Code:
tempStartTime = time.time()
#Take the start time and round the minutes down ... e.g. if it's 9:07AM, round it down so that the start range is 9:00AM
tempStartTimeLocalTime = time.localtime(tempStartTime)
l = list(tempStartTimeLocalTime)
l[4] = 0
tempStartTimeLocalTime = time.struct_time(l)
Reply

Logout Mark Read Team Forum Stats Members Help
How to take a time.time() variable and round the minutes down to 0?0