# 101. Total Time

# Description

Total the time entries from a list or a repeated dialog.

# Code

Repeated Dialog:

SET TotalHours-n TO 0

SET TotalMins-n TO 0

REPEAT Time Entries

// ** If you need to compute elapsed time (Hours & Mins), do it here **

SET TotalHours-n TO TotalHours-n + ZERO( Hours-n )

SET TotalMins-n TO TotalMins-n + ZERO( Mins-n )

END REPEAT

// Dump extra mins into hours

IF TotalMins-n > 59

SET TotalHours-n TO TotalHours-n + TRUNCATE( TotalMins-n / 60, 0 )

SET TotalMins-n TO MOD( TotalMins-n / 60 )

END IF

// Nice output

"«TotalHours-n» hour"

IF TotalHours-n != 1

RESULT + "s"

END IF

RESULT + " and «TotalMins-n» minute"

IF TotalMins-n != 1

RESULT + "s"

END IF

List:

SET TotalHours-n TO 0

SET TotalMins-n TO 0

SET TotalHours-n TO TotalHours-n + ZERO( HoursA-n )

SET TotalHours-n TO TotalHours-n + ZERO( HoursB-n )

SET TotalHours-n TO TotalHours-n + ZERO( HoursC-n )

SET TotalHours-n TO TotalHours-n + ZERO( HoursD-n )

SET TotalMins-n TO TotalMins-n + ZERO( MinsA-n )

SET TotalMins-n TO TotalMins-n + ZERO( MinsB-n )

SET TotalMins-n TO TotalMins-n + ZERO( MinsC-n )

SET TotalMins-n TO TotalMins-n + ZERO( MinsD-n )

// Dump extra mins into hours

IF TotalMins-n > 59

SET TotalHours-n TO TotalHours-n + TRUNCATE( TotalMins-n / 60, 0 )

SET TotalMins-n TO REMAINDER( TotalMins-n, 60 )

END IF

// Nice output

"«TotalHours-n» hour"

IF TotalHours-n != 1

RESULT + "s"

END IF

RESULT + " and «TotalMins-n» minute"

IF TotalMins-n != 1

RESULT + "s"

END IF

# Explanation

This computation totals time entries from either a repeated dialog or from a list. The calculated hours and minutes values are placed in number variables, and a nicely formatted string is also returned.

Variables: The computation assumes the following variables:

• Hours-n - A number variable which holds the number of hours for a single time entry.

• Mins-n - A number variable which holds the number of minutes for a single time entry.

• TotalHours-n - A number variable which holds the total hours computed from the time entries.

• TotalMins-n - A number variable which holds the total minutes computed from the time entries.

• Time Entries - A repeated dialog box which contains two variables, Hours-n and Mins-n.

Elapsed Time: This computation assumes that you already know the elapsed time for each time entry. If, however, your time entries simply have a start time and and end time, you will need to compute the elapsed time as well. Computation #0100: Elapsed Time shows how to do this. You would include the elapsed time calculation within the REPEAT where shown.

The computation should be quite straightforward. We run through the REPEAT, adding each Hours-n and Mins-n value into TotalHours-n and TotalMins-n. Note that we use the ZERO model to give unanswered variables a value of 0. Once we have summed all of the time entries, the TotalMins-n variable will likely have a value much greater than 60. These extra minutes should be converted into hours. We add one hour to TotalHours-n for every 60 minutes in TotalMinutes-n, then set TotalMinutes-n to the REMAINDER.