Last active
October 25, 2017 18:04
-
-
Save sidola/9c6411d64133de0a1f91c6e733ee0933 to your computer and use it in GitHub Desktop.
TimeDiff.ahk
This file contains hidden or 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
/* | |
TimeDiff.ahk | |
Usage: | |
TimeDiff.ShowTimeDiff() | |
*/ | |
class TimeDiff { | |
; Public API | |
ShowTimeDiff() { | |
InputBox, timeInput, TimeDiff,,, 250, 100 | |
try { | |
result := this.TimeDurationsStr(timeInput) | |
TrayTip, Time Diff, %result%, 5 | |
} catch { | |
TrayTip, Time Diff, Bad input - try again, 5 | |
} | |
} | |
; Private API | |
TimeDurationsStr(timesStr) { | |
return this.TimeDurations(StrSplit(timesStr, ",")*) | |
} | |
TimeDurations(times*) { | |
if (Mod(times.Length(), 2) || times.Length() == 0) { | |
throw Exception("Must pass an even amount of times, recieved: " times.Length(), -1) | |
} | |
duration := 0 | |
Loop % times.Length() | |
{ | |
if (!Mod(A_Index, 2)) | |
continue | |
duration += times[A_Index + 1] - times[A_Index] | |
} | |
return this.PrettyDuration(duration) | |
} | |
PrettyDuration(duration) { | |
min := SubStr(duration, -1) | |
hour := SubStr(duration, 1, -2) | |
hour += Floor(min/60) | |
min := Mod(min, 60) | |
return hour "h, " . min . "m" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment