This idea cropped up a while back and slowly coalesced into a rough draft of a working script yesterday and finally into a more robust variant today. Mainly for shop workers who want to be able to easily calculate how long their shift has been, this is a script that takes two parameters (both times in the 'standard' h:mm or hh:mm format) and calculates the difference between them. The script is commented to describe what it does so, linked to the mIRC help file to look up functions such as $calc, $regex and $regml, etc., it should be possible to follow what and how it does what it does (though be warned, the help file is woefully brief on $regex!).
- Code: Select all
/elapse {
; Script to calculate the time difference of two input times.
; Currently the script does not care in which order the start and end times
; are entered, and thus it does not handle crossing midnight!
;
; Initialise time variables
var %a = $null
var %b = $null
; If we have two command line parameters then set our variables
if ($0 == 2) {
var %a = $1
var %b = $2
}
; If we have no command line parameters ask for the first one
if ($0 == 0) {
var %a $$?="Enter first time"-
}
; If we have none or one command line parameters ask for the second one
if ($0 == 0 || $0 == 1) {
if ($0 == 1) {
var %a = $1
}
var %b $$?="Enter second time"-
}
; If either of the time variables remain unset for any reason call a halt to the script
if (%a == $null || %b == $null) {
notice $me Need 0, 1 or 2 paramters, not $0
halt
}
; Use a regular expression command to parse the first time for being in valid format
var %re = $regex(t,%a,^([01]?[0-9]|2[0-3]):([0-5][0-9])$)
; If it doesn't look like a time, tell us and halt
if (%re == 0) {
notice $me First time must be in form [h]h:mm, not %a
halt
}
; Pull out the hour and minute components of the first input time
var %ha = $regml(t,1)
var %ma = $regml(t,2)
; Convert the first time to a number of minutes
var %minsa = $calc( (%ha * 60) + %ma )
; Use the regulasr expression on the second time, again reporting any issues and stopping
var %re = $regex(t,%b,^([01]?[0-9]|2[0-3]):([0-5][0-9])$)
if (%re == 0) {
notice $me Second time must be in form [h]h:mm, not %b
halt
}
; Convert the second time to a number of minutes by extracting hours and minutes from the input
var %hb = $regml(t,1)
var %mb = $regml(t,2)
var %minsb = $calc( (%hb * 60) + %mb )
; Find the difference in minutes between the two numbers of minutes, using $abs(...) to produce the
; absolute number so we don't care which number is higher
var %minst = $abs($calc( %minsa - %minsb ))
; Convert the difference in minutes to hours and minutes
var %hhx = $calc( %minst / 60 )
var %hh = $int(%hhx)
var %mm = $calc( %minst % 60 )
; Tell us the result
notice $me Difference is %minst minutes, which is %hh $+ h: $+ %mm $+ m
}