how to get the time after two minutes [closed]

To get the date-string in bash:

echo "Current: " $(date +"%Y"-"%m"-"%d"T"%H"-"%M"-"%S")
echo "+2 min : " $(date --date="@$(($(date +%s)+120))" +"%Y"-"%m"-"%d"T"%H"-"%M"-"%S")

prints

Current:  2014-09-10T15-58-15
+2 min :  2014-09-10T16-00-15

Read the time from string and print +2min string

str="2014-09-10T15-58-15"
new=$(date --date="@$(($(
        IFS="-T" read y m d H M S <<< "$str";date --date="$y-$m-${d}T$H:$M:$S" +%s
    )+120))" +"%Y"-"%m"-"%d"T"%H"-"%M"-"%S")
echo "From string: $str"
echo "String +2m : $new"

prints

From string: 2014-09-10T15-58-15
String +2m : 2014-09-10T16-00-15

Execute an command from the “current time”, as others already says, use:

sleep 120 ; commands...

Execute a command 2 minute later as is specified in the string:

sec2date() { date --date="@$1"; }
countdown() { s="$1";while (($s)) ; do printf "%04d\r" $s; sleep 1; let s--; done; }

str="2014-09-10T16-55-10"

current=$(date +%s)
stringsec=$(IFS="-T" read y m d H M S <<< "$str";date --date="$y-$m-${d}T$H:$M:$S" +%s)
wanted=$(date --date="@$(($stringsec + 120))" +%s)

diffsec=$(($wanted - $current))
(( $diffsec > 0 )) || { echo "Can't execute in the past" ; exit 1; }

echo "time in the string :" $(sec2date $stringsec)
echo "+2min = execute at :" $(sec2date $wanted)
echo "current time       :" $(date)
echo "need wait          :" $diffsec
countdown $diffsec
echo "running at         :" $(date)

prints

time in the string : st sep 10 16:55:10 CEST 2014
+2min = execute at : st sep 10 16:57:10 CEST 2014
current time       : st sep 10 16:56:52 CEST 2014
need wait          : 18
running at         : st sep 10 16:57:10 CEST 2014

or, simply use the at command. 🙂 🙂

Leave a Comment