Last active
January 19, 2021 20:20
-
-
Save neocotic/1095035 to your computer and use it in GitHub Desktop.
Time Difference Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: Time Difference Calculator | |
:: | |
:: Copyright 2011, [neocotic](http://github.com/neocotic) | |
:: Released under the MIT License | |
:: http://en.wikipedia.org/wiki/MIT_License | |
@echo off | |
setLocal enableDelayedExpansion | |
:: To be returned when script ends | |
:: If an unexpected problem occurs, this will be changed to reflect the level of that problem | |
set returnCode=0 | |
set optFormat=N | |
set optUsage=N | |
:: Reads options one-by-one | |
:readOptions | |
set opt=%~1 | |
if "%opt:~0,1%"=="/" ( | |
:: Checks if usage descriptor should be printed | |
if "%opt:~1,1%"=="?" ( | |
set optUsage=Y | |
goto nextOption | |
) | |
:: Checks if formatted time difference should be printed | |
if /i "%opt:~1,1%"=="F" ( | |
set optFormat=Y | |
goto nextOption | |
) | |
goto invalidOptionError | |
:nextOption | |
shift | |
goto readOptions | |
) | |
:: Determines if usage should be printed and whether or not to continue with process afterwards | |
if "%optUsage%"=="Y" goto printUsage | |
:process | |
:: Validates all required parameters specified | |
set time1=%~1 | |
set time2=%~2 | |
if "%time1%"=="" goto timeNotSpecifiedError | |
if "%time2%"=="" set time2=%TIME% | |
goto calculateTimeDiff | |
:: Provides user feedback on any unexpected errors | |
:invalidOptionError | |
(echo [ ERROR ] Invalid option: %opt%)1>&2 | |
goto error | |
:timeNotSpecifiedError | |
(echo [WARNING] Initial time not specified)1>&2 | |
goto warning | |
:: Handles exiting script correctly due to an unexcepted problem | |
:error | |
set returnCode=2 | |
goto end | |
:warning | |
set returnCode=1 | |
goto end | |
:calculateTimeDiff | |
:: Prepares time by removing milliseconds from time and prepending an space where required | |
set time1=%time1:~0,-3% | |
set time2=%time2:~0,-3% | |
if "%time1:~1,1%"==":" set time1= %time1% | |
if "%time2:~1,1%"==":" set time2= %time2% | |
:: Converts time to seconds | |
set /a hh1=60*60*%time1:~0,2% | |
set /a hh2=60*60*%time2:~0,2% | |
set /a mm1=60*%time1:~3,2% | |
set /a mm2=60*%time2:~3,2% | |
set /a ss1=%time1:~6,2% | |
set /a ss2=%time2:~6,2% | |
:: Calculates time difference in seconds | |
set /a t1=hh1+mm1+ss1 | |
set /a t2=hh2+mm2+ss2 | |
set /a ds=t2-t1 | |
if %ds% LSS 0 set /a ds=%ds%+24*60*60 | |
set /a h=(%ds%/3600) | |
set /a m=(%ds%/60)-60*%h% | |
set /a s=%ds%-60*(%ds%/60) | |
:: Prints time difference which many be formatted | |
if "%optFormat%"=="Y" ( | |
set fds=%s% sec | |
if not %s%==1 set fds=!fds!s | |
if not %m%==0 ( | |
if %m%==1 ( | |
set fds=%m% min !fds! | |
) else ( | |
set fds=%m% mins !fds! | |
) | |
) | |
if not %h%==0 ( | |
if %h%==1 ( | |
set fds=%h% hr !fds! | |
) else ( | |
set fds=%h% hrs !fds! | |
) | |
) | |
echo !fds! | |
) else ( | |
echo hours=%h% | |
echo minutes=%m% | |
echo seconds=%s% | |
) | |
goto end | |
:: Prints usage descriptor for this command | |
:printUsage | |
echo Usage: timediff [options] [time1 [time2]] | |
echo Options: | |
echo /F prints formatted time difference | |
if not "%~1"=="" ( | |
echo. | |
goto process | |
) | |
:end | |
endLocal&exit /b %returnCode% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment