Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature suggestion: timer to specific time #6

Open
smjure opened this issue Feb 22, 2022 · 2 comments
Open

Feature suggestion: timer to specific time #6

smjure opened this issue Feb 22, 2022 · 2 comments

Comments

@smjure
Copy link

smjure commented Feb 22, 2022

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.

@caarlos0
Copy link
Owner

seems nice, happy to review a pr

@ManuLinares
Copy link

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants