###Batch Software Clock:
@echo off
setlocal
set "s=0"
set "m=0"
:clock
set "mm=00%m%"
set "ss=00%s%"
echo %mm:~-2% : %ss:~-2%
set /a "s+=1"
if %s% gtr 59 set "s=0" & set /a "m+=1"
ping 192.0.2.2 -n 1 -w 1000 >nul
goto clock
endlocal
Note: that this clock is not perfectly accurate and will most likely have decay as with any software solution relying upon a ping timeout. The decay will result from the processor taking longer than a second to process the all the commands. This can be easily seen if the processor is under load and the batch script priority is just normal. However, in most use cases where the processor is not under load, the clock should perform with decent enough accuracy for minutes and seconds. Just be aware.
###About Ping Delay:
Notes that I have compiled on the usage of the ping
command for wait and delay functionality.
Success Method:
PING 127.0.0.1 -n 6 >nul
How it Works:
ping
sends 6
echos to the loopback IP address.
ping
pauses for 1 second pause between each echo totaling 5 seconds.
-n
cannot be less than 2 or there will be no delay.
- This method requires a fast and responsive valid IP address.
- That is why the localhost loopback IP
127.0.0.1
is chosen.
- Precision in seconds. Minimum delay of 1 second.
Timeout Method:
PING 192.0.2.2 -n 1 -w 200 >nul
How it Works:
ping
sends 1 echo -n 1
with a request timeout of 200 milliseconds -w 200
.
- ( only adjust the
-w
value and leave -n
as 1
when using this method )
- This method requires an unused IP address. Because it needs to fail to cause the delay.
- The only IP address that can be guaranteed to be unused are the private IP address ranges over which the user has control, or the TEST-NET range.
- Precision in hundreds of milliseconds. Minimum delay of 100 milliseconds.
Private IP Address Range:
The Internet Assigned Numbers Authority (IANA) has reserved the
following three blocks of the IP address space for private internets:
10.0.0.0 - 10.255.255.255 (10/8 prefix)
172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
- In my experience
10.1.1.1
is just a common private IP address that is usually left unused. That is why I prefer it when selecting a private IP address. Otherwise, use a TEST-NET IP address.
TEST-NET IP Address Range:
192.0.2.0/24
- This block is assigned as "TEST-NET" for use in
documentation and example code. It is often used in conjunction with
domain names example.com or example.net in vendor and protocol
documentation. Addresses within this block should not appear on the
public Internet.
Public IP Addresses:
As of January 2010, IP Address 1.1.1.1 has been assigned to APNIC by the IANA. This means that is can no longer be guaranteed as an unused IP address.
###References
###Update:
- Expounded upon time decay notice.
- Switched to TEST-NET IP address.
- Added information regarding
ping
and IP addresses.