@echo off
rem ------------------------------Read me--------------------------------------
rem Revision : 1.0.3
rem     Date : 2012-09-30
rem  Created : 2012-05-19
rem  Purpose : Calculate time
rem    usege : call Lib_CalculateTotalTime.bat 01:02:03.400 04:03:10.510 [display]
rem results in TOTAL_TIME Environment variables
rem ------------------Calculate the total time---------------------------------
rem                        012345678901
rem set      start_pos_tmp=01:02:03.400
rem set        end_pos_tmp=04:03:10.510

set start_pos_tmp=%1
set end_pos_tmp=%2

if "%start_pos_tmp:~1,1%"==":" set "start_pos_tmp=0%start_pos_tmp%"
if "%end_pos_tmp:~1,1%"==":" set "end_pos_tmp=0%end_pos_tmp%"

set hour=%start_pos_tmp:~0,2%
set min=%start_pos_tmp:~3,2%
set sec=%start_pos_tmp:~6,2%
set msec=%start_pos_tmp:~9,3%

rem --------for skip 08 09---------------
if %hour:~0,1%==0  set hour=%hour:~1,1%
if %min:~0,1%==0   set min=%min:~1,1%
if %sec:~0,1%==0   set sec=%sec:~1,1%
if %msec:~0,1%==0  set msec=%msec:~1,2%
if %msec:~0,1%==0  set msec=%msec:~1,1%

set /a hour=%hour%
set /a min=%min%
set /a sec=%sec%
set /a msec=%msec%

set /a start_millsec=%hour%*60*60+%min%*60+%sec%
set /a start_millsec=%start_millsec%*1000+%msec%

set ehour=%end_pos_tmp:~0,2%
set emin=%end_pos_tmp:~3,2%
set esec=%end_pos_tmp:~6,2%
set emsec=%end_pos_tmp:~9,3%

rem --------for skip 08 09---------------
if %ehour:~0,1%==0  set ehour=%ehour:~1,1%
if %emin:~0,1%==0   set emin=%emin:~1,1%
if %esec:~0,1%==0   set esec=%esec:~1,1%
if %emsec:~0,1%==0  set emsec=%emsec:~1,2%
if %emsec:~0,1%==0  set emsec=%emsec:~1,1%

set /a ehour=%ehour%
set /a emin=%emin%
set /a esec=%esec%
set /a emsec=%emsec%

set /a end_millsec=%ehour%*60*60+%emin%*60+%esec%
set /a end_millsec=%end_millsec%*1000+%emsec%

set /a total_time_millsec=%end_millsec%-%start_millsec%
rem echo %hour% %min% %sec% %msec%
rem echo %ehour% %emin% %esec% %emsec%

rem echo %start_millsec% %end_millsec% %total_time_millsec%
set /a tsec=%total_time_millsec%/1000
set /a tmsec=%total_time_millsec%-%tsec%*1000
set /a tmin=%tsec%/60
set /a tsec=%tsec%-%tmin%*60
set /a thour=%tmin%/60
set /a tmin=%tmin%-%thour%*60

if %thour% LEQ 9 (set thour=0%thour%)
if %tmin%  LEQ 9 (set tmin=0%tmin%)
if %tsec%  LEQ 9 (set tsec=0%tsec%)
if %tmsec%  LEQ 9 (set tmsec=00%tmsec%) else (if %tmsec%  LEQ 99 (set tmsec=0%tmsec%))
set total_time=%thour%:%tmin%:%tsec%.%tmsec%
if "%3"=="display" (echo [%start_pos_tmp% - %end_pos_tmp%] = %total_time%)
rem --------clear tmp var---------------
set start_pos_tmp=
set end_pos_tmp=
set hour=
set min=
set sec=
set msec=
set ehour=
set emin=
set esec=
set emsec=
set tsec=
set tmsec=
set tmin=
set tsec=
set thour=
set tmin=
set total_time_millsec=
set start_millsec=
set end_millsec=
rem ------------------Calculate the total time---------------------------------
