Editor

Clock Time to Seconds

#35 · Parsing · Beginner · 35 XP · about 7 min

Video players and timers write durations as clock text: "1:02:03" is 1 hour, 2 minutes and 3 seconds, "4:05" is 4 minutes and 5 seconds, and "59" is 59 seconds. Write to_seconds(clock) that returns the total number of seconds as an integer.

The parts are separated by colons. The last part is always seconds, the one before it minutes, and the one before that hours. Parts may have leading zeros, and the first part can be larger than usual: "90:00" means 90 minutes.

Examples

to_seconds("1:02:03")  # returns 3723
to_seconds("4:05")     # returns 245
to_seconds("59")       # returns 59

The first one is 1 hour (3600 seconds) plus 2 minutes (120 seconds) plus 3 seconds.

Note

The text always has one, two or three parts, and each part is made of digits only.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.