timer icon indicating copy to clipboard operation
timer copied to clipboard

Feature suggestion: timer to specific time

Open smjure opened this issue 3 years ago • 2 comments

First of all, thank you for lovely package.

Many times I use sleep to execute a function at certain time, especially over night. Usual steps I take are to calculate number of seconds, say to 02:14am and then simply use sleep nbSeconds; ./functionToExecute.sh.

It would be aweseome e.g. to have something like this: timer -t 02:14am like -t or --time to time

Please consider implementing it.

smjure avatar Feb 22 '22 13:02 smjure

seems nice, happy to review a pr

caarlos0 avatar Apr 11 '23 19:04 caarlos0

It would be aweseome e.g. to have something like this: timer -t 02:14am like -t or --time to time

This would be a nice feature to have.

This is a bash function that achieves that. Hope it helps smjure

sleepuntil() {
    if [[ $# -eq 0 ]]; then
        echo "Syntax:"
        echo "sleepUntil [-q] <HH[:MM[:SS]]> [more days]"
        echo "    -q Quiet: don't print sleep computed argument"
        echo "    HH          Hours (minimal required argument)"
        echo "    MM          Minutes (00 if not set)"
        echo "    SS          Seconds (00 if not set)"
        echo "    more days   multiplied by 86400 (0 by default)"
        return
    fi
    
    local slp tzoff now quiet=false
    [ "$1" = "-q" ] && { quiet=true; shift; }
    local -a hms=(${1//:/ })
    now=$(date +%s)
    tzoff=$(date +%z)
    tzoff=$((0${tzoff:0:1}(3600*${tzoff:1:2}+60*${tzoff:3:2})))
    slp=$(( (86400+(now-now%86400) + 10#$hms*3600 + 10#${hms[1]}*60 + ${hms[2]}-tzoff-now) % 86400 + ${2:-0}*86400 ))
    $quiet || printf 'sleep %ss, -> %(%c)T\n' $slp $((now+slp))
    sleep $slp
}

ManuLinares avatar Jul 24 '24 23:07 ManuLinares