Calculate the 5 minutes ceiling

Calculate the 5 minutes ceiling



Operating System: Red Hat Enterprise Linux Server 7.2 (Maipo)



I want to round the time to the nearest 5 minutes, only up, not down, for example:



08:09:15 should be 08:10:00



08:11:26 should be 08:15:00



08:17:58 should be 08:20:00



I have been trying with:


(date -d @$(( (($(date +%s) + 150) / 300) * 300)) "+%H:%M:%S")



This will round the time but also down (08:11:18 will result in 08:10:00 and not 08:15:00)



Any idea how i can achieve this?





Change 150 to 299. What you add to the time is the threshold for rounding up. If you add 150, it rounds up if you're within 150 seconds of the 5-minute line. If you add 299, it rounds up if you're within 299 seconds, which is always except when you're right on the previous line. Try it yourself on a piece of paper and convince yourself it works.
– rici
Aug 30 at 7:31





Adding 299 will round '08:15:00' to '08:15:00' but IMO it should become '08:20:00'
– anubhava
Aug 30 at 7:34



299


'08:15:00'


'08:15:00'


'08:20:00'





@jww i saw that post also, this will round up but in either way. I need to round up always to the above value, not below.
– Kevin
Aug 30 at 8:55





This questions sounds like a duplicate, but it is not. It is titled with "rounding", but the question asks for "ceiling". I have flagged to reopen.
– ceving
Aug 30 at 9:35






Linked question is not really a dup as OP always wants to round up.
– anubhava
Aug 30 at 9:58




2 Answers
2



You may use this utility function for your rounding up:


roundDt()
local n=300
local str="$1"
date -d @$(( ($(date -d "$str" '+%s') + $n)/$n * $n)) '+%H:%M:%S'



Then invoke this function as:


roundDt '08:09:15'
08:10:00

roundDt '08:11:26'
08:15:00

roundDt '08:17:58'
08:20:00



To trace how this function is computing use -x (trace mode) after exporting:


-x


export -f roundDt

bash -cx "roundDt '08:11:26'"




+ roundDt 08:11:26
+ typeset n=300
+ typeset str=08:11:26
++ date -d 08:11:26 +%s
+ date -d @1535631300 +%H:%M:%S
08:15:00





Hi anubhava, if i want to use this function with the systime and store it in a variable, how can i achieve this?
– Kevin
Aug 30 at 8:31





To call it for systime and save in a variable use: newdt=$(roundDt "$(date '+%H:%M:%S')") and then do echo $newdt"
– anubhava
Aug 30 at 10:01


newdt=$(roundDt "$(date '+%H:%M:%S')")


echo $newdt"





Kevin: Did this work out?
– anubhava
Aug 30 at 11:27



GNU date can calculate already. It is explained in the manual in the chapter "Relative items in date strings". So you need just one date call.


date


d=$(date +%T) # get the current time
IFS=: read h m s <<< "$d" # parse it in hours, minutes and seconds
inc=$(( 300 - (m * 60 + s) % 300 )) # calculate the seconds to increment
date -d "$d $inc sec" +%T # output the new time with the offset



Btw: +%T is the same as +%H:%M:%S.


+%T


+%H:%M:%S



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)