4 ms·
Why would it have to be the exact millisecond? "$(date)" will have that value for 1 second, assuming the format is correct. There is some very small timespan in
by boomlinde 11y ago
Why would it have to be the exact millisecond? "$(date)" will have that value for 1 second, assuming the format is correct. There is some very small timespan in which it could fail, but the chances are very much in favor of successfully exiting the loop.
- arjie 11y ago`sleep` sleeps for seconds.
- tlrobinson 11y agoAnd `date` returns a timestamp with seconds precision. `time sh -c 'if [ "$(date)" != "Sat Oct 31 16:00:00 EDT 2015" ]; then sleep 1; fi'` took around 1.010 seconds on my machine, but that includes starting a new shell, so there's probably a much smaller than 1% chance that that particular second will be missed. EDIT: here's a little program to test this: #!/usr/bin/env bash while true; do target=$(expr $(date +%s) + 2) while true; do now=$(date +%s) if [ "$now" != "$target" ]; then if [ "$now" -gt "$target" ]; then echo "FAIL :(" break else sleep 1 fi else echo "OK! :)" break fi sleep 0.1 done done Adjust the "sleep 0.1" to increase or decrease the perecentage of failures. Remove to see that it (almost) never fails.
- tlrobinson 11y agoAccidentally left this running for a few hours... 71 FAIL :( 5747 OK! :) So about 1% failure rate.
- arjie 11y agoHa ha, nice. Not so bad, then.
- deleted 11y ago[deleted]
- untog 11y agoYes, but presumably it rounds that last number, right? So as long as it ticks somewhere between 15:59:59.50 and 16:00:00.49 it should run?