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


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



See More: RFC: a batch timer using unix time

Reply ↓  Report •

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=
set usno=

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:
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
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"
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