You are not logged in.

#1 06 Oct 2015 15:48

ncakovski
New Member
Registered: 06 Oct 2015
Posts: 3

Script to give the difference between numbers

I need develop a script that will get the local windows time and compare it with a time pulled from an external ntp server. In case of difference to write in a file.
The numbers will be like 12:44:45.5  or 10:42:45, i cannot figure out how to solve to subtract the difference the other thing i wrote here:

for /f "tokens=1-2 delims=/ " %%x in ("%TIME%") do (set localtime=%%x)
for /f "tokens=*" %%x in ('w32tm /stripchart /computer:mk.pool.ntp.org /samples:1 /dataonly') do (set ntptime=%%x)

Can you please help me
Thanks in advance

Offline

#2 07 Oct 2015 10:08

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Script to give the difference between numbers

X:\>w32tm /stripchart /computer:mk.pool.ntp.org /samples:1 /dataonly
Tracking mk.pool.ntp.org [79.99.56.5:123].
Collecting 1 samples.
The current time is 07/10/2015 10:04:02.
10:04:02, +00.0085278s

That's the difference there, right at the end.  The reference clock is showing as being 0.0085278 seconds ahead of the system clock.

So you're almost there already with your second command, just a minor tweak required:

for /f "tokens=2" %%x in ('w32tm /stripchart /computer:mk.pool.ntp.org /samples:1 /dataonly') do (set ntptime=%%x)

You can then do some text manipulations to get the level of granularity you want to report on.

NB - I ran this on Win7, other versions may differ in output.

Last edited by bluesxman (07 Oct 2015 13:10)


cmd | *sh | ruby | chef

Offline

#3 07 Oct 2015 14:23

ncakovski
New Member
Registered: 06 Oct 2015
Posts: 3

Re: Script to give the difference between numbers

bluesxman in my other post i wrote that i can get the local time and time from external ntp server but in my script need to compare and if any different in times to be exported in txt document..so i don't know how to compare and subtract the two timers..

Offline

#4 09 Oct 2015 08:23

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Script to give the difference between numbers

so i don't know how to compare and subtract the two timers

The comparison of the time is in the report you are generating (at least it is on my Win7 machine).
What I'm saying is it is already telling you the difference so you don't need to make a calculation, just report the result.

For clarity, I've changed the code slightly:

for /f "tokens=2" %%x in ('w32tm /stripchart /computer:mk.pool.ntp.org /samples:1 /dataonly') do (set timeoffset=%%x)
if "%timeoffset%" EQU "error:" (
  echo NTP source did not respond as expected.
  exit /b 1
) else (
  echo The time offset is: %timeoffset%
)

The difference reported has a high level of granularity, so you will probably want to manipulate the result to remove detail, such that you're only reporting differences that you find significant.
You could expand this by adding the following code (rounds to 1 second):

for /f "usebackq tokens=1,2 delims=." %%x in ('%timediff%') do (set "timediff.a=%%x" & set "timediff.b=%%y")

if "%timediff.a:~0,1%" EQU "0" set "timediff.a=%timediff.a:~1%"
set "timediff.round=%timediff.a%"
if "%timediff.b:~0,1%" GEQ "5" set /a "timediff.round+=1"

if "%timesign%" EQU "-" (set "timesign.word=ahead of") else (set "timesign.word=behind")
if "%timediff.round%" equ "0" (
  echo The local clock is in sync
) else (
  echo The local clock is %timediff.round% second^(s^) %timesign.word% the reference
)

Last edited by bluesxman (09 Oct 2015 08:56)


cmd | *sh | ruby | chef

Offline

#5 09 Oct 2015 12:22

ncakovski
New Member
Registered: 06 Oct 2015
Posts: 3

Re: Script to give the difference between numbers

Thank you bluesxman it works like a charm
  big_smile  cool

Offline

Board footer

Powered by