100. Elapsed Time
Description
Compute elapsed time in days (optional), hours, and minutes.
Code
// Days (omit if you don't care)
SET Days-n TO DAYS FROM( DayStart-n, DayEnd-n )
// Hours
SET Hours-n TO HourEnd-n - HourStart-n
IF Hours-n < 0 // omit section if you aren't calculating days
SET Hours-n TO Hours-n + 24
SET Days-n TO Days-n - 1
END IF
// Minutes
SET Minutes-n TO MinEnd-n - MinStart-n
IF Minutes-n < 0
SET Minutes-n TO Minutes-n + 60
SET Hours-n TO Hours-n - 1
IF Hours-n < 0 // omit section if you aren't calculating days
SET Hours-n TO Hours-n + 24
SET Days-n TO Days-n - 1
END IF
END IF
// Pretty output (optional)
"«Days-n» day"
IF Days-n != 1
RESULT + "s"
END IF
RESULT + ", «Hours-n» hour"
IF Hours-n != 1
RESULT + "s"
END IF
RESULT + ", and «Minutes-n» minute"
IF Minutes-n != 1
RESULT + "s"
END IF
Explanation
This computation takes a begin time and an end time and calculates the amount of time elapsed between them in hours and minutes. Days are optional. The calculated days, hours and minutes are placed in number variables, and a nicely formatted string is also returned.
Variables: The computation assumes the following variables:
• DayStart-n (Optional) - A date variable which holds the start date.
• HourStart-n - A number variable which holds the start hour.
• MinStart-n - A number variable which holds the start minute.
• DayEnd-n (Optional) - A date variable which holds the end date.
• HourEnd-n - A number variable which holds the end hour.
• MinEnd-n - A number variable which holds the end minute.
• Days-n (Optional) - A number variable which will be set to the number of elapsed days.
• Hours-n - A number variable which will be set to the number of elapsed hours.
• Minutes-n - A number variable which will be set to the number of elapsed minutes.
Preparing the Variables: Before you can do time calculations, you must make sure that the hours and minutes are placed in separate number variables, and that the hours have been converted into military time. If your time value is in a text variable, you should use Computation 99: Parsing Time Values. Use Computation #0102: Convert Hours to Military Time if your hours value needs to be converted to military time.
Days: Elapsed days are optional. To exclude days from the calculation, remove the first section of the computation and the two IF ... END IF blocks marked for removal.
Hours: As long as the hours are in military time, we can arrive at elapsed hours by subtracting HourStart-n from HourEnd-n. If this results in a negative number, we reduce the Days-n variable by one day and add 24 hours.
Minutes: Like hours, we arrive at elapsed minutes by subtracting MinStart-n from MinEnd-n. If this results in a negative number, we reduce the Hours-n variable by one hour and add 60 minutes to Days-n.
Caveat: If your start time is later than your end time, you'll get inaccurate results. That should go without saying, but there is always someone ...