/ Published in: Bash
Print difference between two ISO8601 formatted timestamps in seconds.
Expand |
Embed | Plain Text
#! /bin/bash function usage { echo 'Subtract two timestamps in ISO 8601 format.' echo 'If end timestamp is missing, assume now.' echo 'Returns value in seconds.' echo '' echo 'usage: $0 start [end]' echo '' exit } # ISO 8601 format is "yyyy-MM-dd'T'HH:mm:ssZ" function iso8601_to_lbolt { u="$(echo $1 | sed -e 's/T/ /' | sed -e 's/-05:00/ EST/')" echo $(date -d "$u" +%s ) } if [ $# -lt 1 ] ; then usage fi start=$(iso8601_to_lbolt $1) if [ $# -eq 1 ] ; then end=$(date +%s) else end=$(iso8601_to_lbolt $2) fi echo $(($end - $start))
You need to login to post a comment.
