[rrd-users] Re: Help with Bash script to calc end-time in multiples of 300 ?

Alex van den Bogaerdt alex at ergens.op.het.net
Sat Nov 18 14:39:00 MET 2006

On Sat, Nov 18, 2006 at 09:32:15AM +0000, Simon Hobson wrote:
> Rob Conway wrote:
> >I just use "date +%s" to get the unix time but how can I easily 
> >round this value ?
> etime=`date +%s`
> step=300
> etime=$(( ${etime} - ( ${etime} % ${step} ) ))

etime=$(( ${etime} / ${step} * ${step}  ))

is 10% faster, at least on my system. I tried this by looping 100,000
times doing those calculations, several runs.  Bash uses integer
calculations, and my sequence saves a calculation internally.

Printing the value can be done using perl, but unless you're going to
use perl for other purposes as well you are better off with gnu-date.
This runs in 44% of the time needed for starting perl:

/bin/date -d 19700101\ 00:00\ +0000\ ${etime}sec

It means: the unix epoch (19700101 00:00, timezone UTC) and then
${etime} seconds further in time (so: reverse of date +%s)

I use a hardcoded path, to avoid PATH search (more work, more cpu
cycles needed, more time!).

Time formatting works, so you can add '+%F %T' to the command and
get a nicer format:

/bin/date -d 19700101\ 00:00\ +0000\ ${etime}sec +%F\ %T

To get it into a variable, to be used in your graph script:

printedtime=$(/bin/date -d 19700101\ 00:00\ +0000\ ${etime}sec +%F\ %T)
printedtime=$(/bin/date -d "19700101 00:00 +0000 ${etime}sec" +"%F %T")

Alex van den Bogaerdt

Unsubscribe mailto:rrd-users-request at list.ee.ethz.ch?subject=unsubscribe
Help        mailto:rrd-users-request at list.ee.ethz.ch?subject=help
Archive     http://lists.ee.ethz.ch/rrd-users
WebAdmin    http://lists.ee.ethz.ch/lsg2.cgi

More information about the rrd-users mailing list