4

I'm thinking about extracting the time from the 'date' command, subtracting a certain time in the future from it to get the number of seconds left until 'date' reaches that time, then to divide that number by 60 for minutes, and 60 for hours.

I want to use this as an argument for the 'shutdown' command for example.

how do I do this?

7

Something like this?

echo $(( $(date +%s -d "tomorrow 12:00") - $( date +%s ) ))
59856
6
  • You're welcome ;) Feel free to up-vote or accept :D the answer. – tink Dec 7 '18 at 6:28
  • how do I divide by 60? in the same command? – dudawe Dec 7 '18 at 6:30
  • 1
    Awesome! I tried without the additional brackets (noob) – dudawe Dec 7 '18 at 6:36
  • 1
    this does not cater for the case where it is 10h00 now and i want to trigger at next 12h00 , 2h hours from now not 1d+2h – Pieter Feb 24 '20 at 23:28
  • 1
    @Pieter - what an astute observation. – tink Feb 24 '20 at 23:57
0

The conversion to a time string could be done directly in bash (less than 24 hours):

 $ TZ=UTC0 printf '%(%H:%M:%S)T\n' 123
 00:02:03

The time difference could be found with simple math:

 $ now=$(printf '%(%s)T')
 $ future=$(date -d '+10 hours' '+%s')
 $ tdiff=$(( future - now ))
 $ TZ=UTC0 printf '%(%H:%M:%S)T\n' "$tdiff"
 10:00:00

To get up to 364 days use this:

 now=$(printf '%(%s)T')
 future=$(date -d '+10 hour' '+%s')
 tdiff=$(( future - now ))
 j=$(( $(TZ=UTC0 printf '%(%j)T' "$tdiff") - 1 ))
 TZ=UTC0 printf '%s days %(%H:%M:%S)T\n' "$j" "$tdiff"

 0 days 10:00:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.