Script to give the difference between numbers (times)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ncakovski
Posts: 3
Joined: 17 Sep 2015 08:17

Script to give the difference between numbers (times)

#1 Post by ncakovski » 07 Oct 2015 01:04

Hello 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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Script to give the difference between numbers (times)

#2 Post by Squashman » 07 Oct 2015 06:34

Dave wrote a great hybrid script that you can use.
viewtopic.php?p=38387

ncakovski
Posts: 3
Joined: 17 Sep 2015 08:17

Re: Script to give the difference between numbers (times)

#3 Post by ncakovski » 08 Oct 2015 05:21

i make it and is working but it gives me error
Unbalanced parenthesis.
Unbalanced parenthesis.
Unbalanced parenthesis.
Missing operator.
but gives me the result..and between 08 and 09 a clock it gives me invalid number numeric constants are either decimal etc because as i know 08 and 09 is thinking that is octal..can anyone help me to solve the problem
Thanks in advance

Code: Select all

@echo off
setlocal enableextensions enabledelayedexpansion
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
set /A "localtime=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
for /F "tokens=1-4 delims=:.," %%a in ('w32tm /stripchart /computer:uk.pool.ntp.org /samples:1 /dataonly') do (
set /A "ntptime=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
set /a "val=10000%val% %% 10000"
set timecode1=localtime
set timecode2=ntptime
set /A durration=localtime-ntptime
set /A hh=durration/(60*60*100), rest=durration%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %cc% lss 10 set cc=0%cc%
echo %hh%:%mm%:%ss%,%cc%
Last edited by Squashman on 08 Oct 2015 07:57, edited 1 time in total.
Reason: MOD EDIT: Please use CODE TAGS.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Script to give the difference between numbers (times)

#4 Post by Squashman » 08 Oct 2015 08:08

Does this code output milliseconds for you? It does not for me but your FOR /F command is setup to grab a 4th token.

Code: Select all

w32tm /stripchart /computer:uk.pool.ntp.org /samples:1 /dataonly

ncakovski
Posts: 3
Joined: 17 Sep 2015 08:17

Re: Script to give the difference between numbers (times)

#5 Post by ncakovski » 08 Oct 2015 08:16

yes it does it was working till 07 a clock and after that i saw that it crashed and gives errors

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Script to give the difference between numbers (times)

#6 Post by Squashman » 08 Oct 2015 09:10

ncakovski wrote:yes it does it was working till 07 a clock and after that i saw that it crashed and gives errors

Correct! Because you are not prefixing your HOUR with a 1 like you are with your other time values.

Code: Select all

H:\>set /a 08*60
Invalid number.  Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Script to give the difference between numbers (times)

#7 Post by Squashman » 08 Oct 2015 21:44

The way I read the documentation for W32TM it does not give you the milliseconds in the time output. It gives you the TIME and the OFFSET. So there is no need to calculate the time difference it is already giving you the time difference.

Before I resynced

Code: Select all

C:\Users\Squash>w32tm /stripchart /computer:uk.pool.ntp.org /samples:1 /dataonly
Tracking uk.pool.ntp.org [129.250.35.251:123].
Collecting 1 samples.
The current time is 10/8/2015 10:11:16 PM.
22:11:16, -08.7557036s

After I resynced

Code: Select all

C:\Users\Squash>w32tm /stripchart /computer:uk.pool.ntp.org /samples:1 /dataonly
Tracking uk.pool.ntp.org [188.114.116.1:123].
Collecting 1 samples.
The current time is 10/8/2015 10:25:57 PM.
22:25:57, +00.0112369s

Post Reply