-2

How do I round it to the next 5th interval?

# date
Sat Oct 13 22:09:25 IST 2012
# date '+%H-%M'
22-09

the output should be
22-10

When the minute is 58 it should round to 00 and not 60 though.

shantanuo
  • 3,459
  • 8
  • 47
  • 64

1 Answers1

1

You can tweak this:

date '+%H-%M' | awk -F- '{ while( $2 % 5 != 0 ){ $2++ }; print $1, $2 % 60 }'

Example:

$ date '+%H-%M' | awk -F- '{ while( $2 % 5 != 0 ){ $2++ }; print $1, $2 % 60 }'
18 55

How it works:

  • Split the time over a hyphen
  • Increment the minute until it divides 5
  • Output the result with the minute modulo 60, so 60 -> 0.
Jay
  • 6,439
  • 24
  • 34
  • Great solution! Just remember to handle rollovers. – Philip Oct 13 '12 at 18:53
  • The question isn't too clear for me, I thought the user wanted 11:56 -> 11:00 though that's probably not very practical. It'll leave adding one to `$1` (`% 24`) as an exercise to the reader :-) – Jay Oct 13 '12 at 21:56
  • Close. But I am not able to round 45-49 to 50 and 05-59 should become 06-00 and not 05 0 – shantanuo Oct 14 '12 at 00:18
  • Yes, that's because I didn't give you a full solution, only a very large pointer as to how I'd do it. – Jay Oct 14 '12 at 09:52