Help with Time maths

Dell PRECISION
February 6, 2009 at 05:23:19
Specs: win xp 5.1.2600, 3mhz/2 gig
Guys, this works, but the end result (goto :FINAL_ANSWER) isnt displaying corrrectly.
I need to get the numbers without any leading zeros to display. Please advise.

time_log.log has two times in it... example:
16:28:50.52
16:29:07.27

Cheers Lee

::get information from file
@echo off & cls
setlocal enabledelayedexpansion
for /f %%j in (time_log.log) do (
set /a number +=1
set reaction_time=%%j
set reaction!number!=!reaction_time!
)

::set information into variables
set hh1=%reaction1:~0,2%
set mm1=%reaction1:~3,2%
set ss1=%reaction1:~6,2%
set ms1=%reaction1:~9,2%
set hh2=%reaction2:~0,2%
set mm2=%reaction2:~3,2%
set ss2=%reaction2:~6,2%
set ms2=%reaction2:~9,2%

::a bit of maths to get varibales correct for time
:GET_MS
set /a answer_ms = %ms2% - %ms1%
if %answer_ms% lss 0 (
goto :MS_NEGATIVE
) else (
goto :GET_SS
)

:MS_NEGATIVE
set answer_ms=
set /a ms2 +=100
set /a answer_ms = %ms2% - %ms1%
set /a ss2 -=1

:GET_SS
set /a answer_ss = %ss2% - %ss1%
if %answer_ss% lss 0 (
goto :SS_NEGATIVE
) else (
goto :GET_MM
)

:SS_NEGATIVE
set answer_ss=
set /a ss2 +=100
set /a answer_ss = %ss2% - %ss1%
set /a mm2 -=1

:GET_MM
set /a answer_mm = %mm2% - %mm1%
if %answer_mm% lss 0 (
goto :MM_NEGATIVE
) else (
goto :GET_HH
)

:MM_NEGATIVE
set answer_mm=
set /a mm2 +=100
set /a answer_mm = %mm2% - %mm1%
set /a hh2 -=1

:GET_HH
set /a answer_hh = %hh2% - %hh1%
if %answer_hh% lss 0 (
goto :HH_NEGATIVE
) else (
goto :FINAL_ANSWER
)

:HH_NEGATIVE
set answer_hh=
set /a hh2 +=100
set /a answer_hh = %hh2% - %hh1%

::final answer should display without any leading zero's
:FINAL_ANSWER
if %answer_hh% == 0 goto :ANSWER_FROM_MINUTES
if %answer_mm% == 0 goto :ANSWER_FROM_SECONDS
if %answer_ss% == 0 goto :ANSWER_FROM_MILLISECONDS
echo %answer_hh%:%answer_mm%:%answer_ss%:%answer_ms%
ping -n 2 -w 1000 1.1.1.1 >nul
goto :EOF

:ANSWER_FROM_MINUTES
echo %answer_mm%:%answer_ss%:%answer_ms%
ping -n 2 -w 1000 1.1.1.1 >nul
goto :EOF

:ANSWER_FROM_SECONDS
echo %answer_ss%:%answer_ms%
ping -n 2 -w 1000 1.1.1.1 >nul
goto :EOF

:ANSWER_FROM_MILLISECONDS
echo %answer_ms%
ping -n 2 -w 1000 1.1.1.1 >nul
goto :EOF


See More: Help with Time maths

Report •


#1
February 6, 2009 at 06:45:45
I don't understand if you just want no leading zero for hours only or for minutes and so on, anyway I show you how to set the hour variable by substring manipulation and you can repeat the process if needed

if %answer_hh% lss 10 set answer_hh=%answer_hh:~1,1%

Type set /? to know more about substrings.

Better do not leave spaces in fromt and after the =(=) operators for variables.


Report •

#2
February 6, 2009 at 06:52:45
Thanks IVO... I will test it and get back to you.

Report •

#3
February 6, 2009 at 07:03:17
IVO... it seems your answer just sets the answer to one decimal place. (%answer_hh:~1,1%)
What I actually want is when %answer_hh% or %answer_mm% is zero '0' then I don't want it displayed.
I will give an example:
if %answer_hh% is 0
%answer_hh% is 0
%answer_hh% is 43
%answer_hh% is 21
-------- all I want displayed is 42:21 not 0:0:42:21

I hope it is clearer now!!
Thanks


Report •

Related Solutions

#4
February 6, 2009 at 07:18:06
I figured it out after some playing around... Thanks anyway.
Have a good weekend.

::final answer should display without any leading zero's
:FINAL_ANSWER
if %answer_hh% == 0 goto :NO_HOUR
echo %answer_hh%:%answer_mm%:%answer_ss%:%answer_ms%
ping -n 1 -w 1000 1.1.1.1 >nul
goto :EOF

:NO_HOUR
if %answer_mm% == 0 goto :NO_MINUTE
echo %answer_mm%:%answer_ss%:%answer_ms%
ping -n 1 -w 1000 1.1.1.1 >nul
goto :EOF

:NO_MINUTE
if %answer_ss% == 0 goto :NO_SECOND
echo %answer_ss%:%answer_ms%
ping -n 1 -w 1000 1.1.1.1 >nul
goto :EOF

:NO_SECOND
if %answer_ms% == 0 goto :NO_MILISECOND
echo %answer_ms%
ping -n 1 -w 1000 1.1.1.1 >nul
goto :EOF

:NO_MILISECOND
echo No Time
ping -n 1 -w 1000 1.1.1.1 >nul
goto :EOF


Report •


Ask Question