7

We have this string to retrieve date and time in clean format.

 TIMESTAMP=$(date "+%Y%m%d%H%M")

Is there some inline code that we can use to round the minute to 5 down. So if it is 12:03 it will make it 12:00 and if it is 12:49 it will be 12:45

Thank you!

Jasen
  • 11,837
  • 2
  • 30
  • 48
user3408380
  • 375
  • 3
  • 5
  • 22

8 Answers8

9

To do this you can subtract the minutes modulo 5 from the total minutes. To print it out:

echo "$(date "+%Y%m%d%H%M") - ($(date +%M)%5)" | bc

To save it to a variable:

my_var=$(echo "$(date "+%Y%m%d%H%M") - ($(date +%M)%5)" | bc)

This relies on your date format string remaining as it is now - a string of numbers.

Example output:

$ date "+%Y%m%d%H%M"
201404010701
$ echo "$(date "+%Y%m%d%H%M") - ($(date +%M)%5)" | bc
201404010700
Josh Jolly
  • 11,258
  • 2
  • 39
  • 55
5

You can use the integer division and multiply back to get the round value.

$ m=23
$ (( m /= 5, m *= 5 )) && echo $m
20

Integer division just return the integer part of the result, so in the case above is returning 4. Then multiplying by 5 gives 20.

In your case:

$ TIMESTAMP=$(date "+%Y%m%d%H%M")
$ echo $TIMESTAMP
201404011231
$ (( TIMESTAMP /= 5, TIMESTAMP *= 5 ))
$ echo $TIMESTAMP
201404011230

$ TIMESTAMP=$(date "+%Y%m%d%H%M")
$ echo $TIMESTAMP
201404011257
$ (( TIMESTAMP /= 5, TIMESTAMP *= 5 ))
$ echo $TIMESTAMP
201404011255
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 1
    +1, I prefer Glenn's solution but this is also brief, easy to read and *fairly* portable. Note you can have multiple expressions in a single `(( ... ))` expression, e.g. `(( TIMESTAMP /= 5, TIMESTAMP *= 5 ))`. – Adrian Frühwirth Apr 01 '14 at 10:49
  • 1
    Thank you very much for the hint, @AdrianFrühwirth , didn't know it :) – fedorqui Apr 01 '14 at 10:58
5

A little string manipulation:

case $TIMESTAMP in 
    *[1234]) TIMESTAMP=${TIMESTAMP%?}0;; 
    *[6789]) TIMESTAMP=${TIMESTAMP%?}5;; 
esac

${TIMESTAMP%?} removes the last character. Ref: http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
5

Not exactly bash only but using dateutils' dround utility, it boils down to:

$ dround now -5m
2014-04-01T10:35:00

or with your format specifiers:

$ dround now -5m -f '%Y%m%d%H%M'
201404011035

Disclaimer: I am the author of that project.

hroptatyr
  • 4,702
  • 1
  • 35
  • 38
2

using the remainder operator the we can determine the number of excess minutes, that can then be subtracted from the original:

TIMESTAMP=$(date "+%Y%m%d%H%M")
(( TIMESTAMP -= TIMESTAMP%15 ))
echo $TIMESTAMP

this only works because both 100 and 60 are divisible by 5 if 15 minutes was wanted instead of 5 the expression would be more complex:

(( TIMESTAMP -= TIMESTAMP%100%5 ))
Jasen
  • 11,837
  • 2
  • 30
  • 48
1

The solution of Fedorqui is very smart and versatile:

seconds=$(date +%s) 
echo $seconds $(date --date="@$seconds" +%H:%M:%S) # say 1559449650 # 06:27:30
# (( seconds /= 3600, seconds *= 3600 )) # rounds to this hour
# (( seconds += 3600, seconds /= 3600, seconds *= 3600 )) # rounds to next hour
(( seconds += 1800, seconds /= 1800, seconds *= 1800 )) # rounds to next half of a hour
next_hour=$(date --date="@$seconds" +%H:%M:%S)
echo $seconds $next_hour # say 1559449800 06:30:00
xerostomus
  • 466
  • 4
  • 11
1

With Bash:
Following one-liner rounds the minutes down to steps of 5 minutes:

d=$(date +'%M'); 
echo $(date +'%d/%m/%Y %H:')$((($(($d / 5))-0)*5))':'$(date +'%M')
output example:
Sat Apr 24 12:16:00 CEST 2021 24/04/2021 12:15:16
shirleyquirk
  • 1,527
  • 5
  • 21
JohanV
  • 31
  • 2
0

With bash:

TIMESTAMP=$(date "+%Y%m%d%H%M")

MINROUND=$((10#${TIMESTAMP:10:2} / 5 * 5))
if [ "${#MINROUND}" = 1 ]; then
  MINROUND="0$MINROUND"
fi
TIMESTAMP="${TIMESTAMP:0:10}${MINROUND}"
ggrandes
  • 2,067
  • 22
  • 16