Solved RFC: a batch timer using unix time

Clone / X
June 10, 2020 at 09:46:29
Specs: w7 64bit, i3 8G
usage: first CALL starts timer; second stops it
::==================================
:: based on Richie Lawrence :GetUnixTime
@echo off & set/a UT=0 & setLocal enableDELAYedeXpansioN
call :GetUnixTime

if exist \#START# (
  set /p START=<\#START# & del \#START#
  set/a ET = !UT! - !START! & echo.!ET! seconds elapsed
) else (
> \#START# echo.!UT!
)
goto :eof

:GetUnixTime

for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do ( set %%x)
set /a z=(14-100%Month%%%100)/12, y=10000%Year%%%10000-z
set /a ut=y*365+y/4-y/100+y/400+(153*(100%Month%%%100+12*z-3)+2)/5+Day-719469
set /a ut=ut*86400+100%Hour%%%100*3600+100%Minute%%%100*60+100%Second%%%100
set UT=%ut%
goto :EOF

=====================

M2


See More: RFC: a batch timer using unix time

Reply ↓  Report •

#1
June 10, 2020 at 20:49:20
✔ Best Answer
Hello M2: On a similar note, I'm usually more interested in setting my computer's clock to
accurate NTP standards. This batch helps, but requires a couple VBscripts to help it out:
@echo off & setlocal
::=================  N E T C A T   V E R S I O N, REQUIRES NETCAT for windows
:: also: 'addtime.vbs' and 'hexin.vbs' appended below
set tserv=129.6.15.28 129.6.15.29 132.163.4.101 132.163.4.102 192.43.244.18 131.107.1.10 128.138.140.44 time-a.nist.gov time-b.nist.gov time-a.timefreq.bldrdoc.gov time-b.timefreq.bldrdoc.gov time.nist.gov time-nw.nist.gov utcnist.colorado.edu
set usno=128.138.141.172

:xit
echo %usno%
for /f "tokens=3" %%a in ('REG query HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation ^| find /i "activetimebias"') do set dsav=%%a
set dsav=%dsav:0x=%
for /f %%a in ('hexin.vbs %dsav%') do set adj=%%a
set /a adj/=60

set c=0
set max=2
for /f "tokens=3" %%a in ('nc %usno% 13^|^|echo X X FAIL') do set clok=%%a
if not defined clok set clok=FAIL
if "%clok%" equ "FAIL" (
echo %usno% failed to deliver, aborting"
goto :eof
)
:: note that vbs puts a f---ed-up date if the time rolls over midnight.
:: that's handled crudely in addtime.vbs. This next gets 24hr time after adj
echo clok %clok%
for /f "tokens=2,3,5 delims=: " %%a in ('c:\vbs\addtime.vbs %clok% -%adj%') do (
set t=%%c:%%a:%%b
)
echo ntp-server:       %clok%
echo zone-adjusted:    %t%
echo local:            %time:~0,8%
set dt=%date% %time% NTP: %t%
if /i "%1" neq "set" exit /b
echo changing system-time from %time% to %t%
echo %t% | time>nul
echo Adjusted::  %time%
goto :eof
::================ end batch, begin vbs supplements:
'-----------addtime.vbs
c=wscript.arguments.count
if c < 2 then
wscript.echo "Usage: addtime time adjustment_value [element]  where 'element' is:"
wscript.echo "h=hours, m=minutes, and s=seconds, defaults to 'h'."
wscript.echo "Primarily aimed at time, date not intentionally supported."
wscript.quit 1
end if
adj=wscript.arguments(1)
if c=3 then v=wscript.arguments(2) else v="h"
wscript.echo "v: "&v
' "m" is used for Month, so here i swap out the m for the counter-intuitive
' "n" which is used for seconds
if "v"="m" then v="n"
p=dateadd(v,adj,wscript.arguments(0))
q=instr(p,"/")
if q>0 then p=mid(p,12)
wscript.echo p&" "&formatdatetime(p,vbshorttime)

'---------------- hexin.vbs: input is HEX, output DECIMAL
if wscript.arguments.count=0 then k=wscript.stdin.readline else k=wscript.arguments(0)
wscript.echo eval("&H"&k)

message edited by nbrane


Reply ↓  Report •
Related Solutions


Ask Question