Lurker News
Back to discussion

Comment by tlrobinson

tlrobinson1 reply#
And `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.
tlrobinson1 reply#
Accidentally left this running for a few hours...

      71 FAIL :(
    5747 OK! :)
So about 1% failure rate.
arjie#
Ha ha, nice. Not so bad, then.