Yes! Thanks to your suggestions I was able to do it. Thank you so much!
I’ll leave what I did here if anyone else wants to do this:
First, I formatted the frontmatter to look like this:
["00:10:30", "00:06:09", "00:12:00", "00:08:07", "00:08:31", "00:14:41"]
Then, I created a $total
variable to hold the final tally in seconds.
Next, I wrote a range statement for this array, and inside it I created these variables to represent the hours, minutes, and seconds (the dot refers to the array):
{{ $times := split . ":" }}
{{ $hours := 0 }}
{{ $minutes := 0 }}
{{ $seconds := 0 }}
I then made a few if else statements to check if these values, with the leading 0s removed, are null; it outputs 0 if they are and the number without the leading 0s if they are not:
{{ if (eq (strings.TrimLeft "0" (index $times 0) ) "") }}
{{ $hours = int "0" }}
{{ else }}
{{ $hours = int (strings.TrimLeft "0" (index $times 0)) }}
{{ end }}
{{ if (eq (strings.TrimLeft "0" (index $times 1) ) "") }}
{{ $minutes = int "0" }}
{{ else }}
{{ $minutes = int (strings.TrimLeft "0" (index $times 1)) }}
{{ end }}
{{ if (eq (strings.TrimLeft "0" (index $times 2) ) "") }
{{ $seconds = int "0" }}
{{ else }}
{{ $seconds = int (strings.TrimLeft "0" (index $times 2)) }}
{{ end }}
Then, I converted the hours and minutes to seconds…
{{ $convHours := mul $hours 3600 }}
{{ $convMinutes := mul $minutes 60 }}
…added these and the seconds value together…
{{ $totalSeconds := add (add $convMinutes $convHours) $seconds }}`
…and then added this to the $total
:
{{ $total = add $total $totalSeconds }}
Here, I ended the range, and I converted the $total
back to individual time units, making sure to cast them as ints:
{{ $totalHours := int (math.Floor (div $total 3600)) }}
{{ $totalMinutes := int (math.Floor (div $total 60)) }}
{{ $totalSeconds := int (mod $total 60) }}
And lastly, I displayed them the way I want:
{{ printf "%02d" $totalHours }}:{{ printf "%02d" $totalMinutes }}:{{printf "%02d" $totalSeconds }}
So altogether my code looks like this:
{{ $total := 0 }}
{{ range $.Params.tracklengths }}
{{ $times := split . ":" }}
{{ $hours := 0 }}
{{ $minutes := 0 }}
{{ $seconds := 0 }}
{{ if (eq (strings.TrimLeft "0" (index $times 0) ) "") }}
{{ $hours = int "0" }}
{{ else }}
{{ $hours = int (strings.TrimLeft "0" (index $times 0)) }}
{{ end }}
{{ if (eq (strings.TrimLeft "0" (index $times 1) ) "") }}
{{ $minutes = int "0" }}
{{ else }}
{{ $minutes = int (strings.TrimLeft "0" (index $times 1)) }}
{{ end }}
{{ if (eq (strings.TrimLeft "0" (index $times 2) ) "") }}
{{ $seconds = int "0" }}
{{ else }}
{{ $seconds = int (strings.TrimLeft "0" (index $times 2)) }}
{{ end }}
{{ $convHours := mul $hours 3600 }}
{{ $convMinutes := mul $minutes 60 }}
{{ $totalSeconds := add (add $convMinutes $convHours) $seconds }}
{{ $total = add $total $totalSeconds }}
{{ end }}
{{ $totalHours := int (math.Floor (div $total 3600)) }}
{{ $totalMinutes := int (math.Floor (div $total 60)) }}
{{ $totalSeconds := int (mod $total 60) }}
<p>{{ printf "%02d" $totalHours }}:{{ printf "%02d" $totalMinutes }}:{{printf "%02d" $totalSeconds }}</p>
Again, thank you for guiding me in the right direction!