-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clock.go
43 lines (38 loc) · 1.2 KB
/
Clock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package berlinclock
import (
"strings"
"github.com/corbym/berlinclock/converter"
"strconv"
"regexp"
"errors"
"fmt"
)
var validTimeFormat = regexp.MustCompile(`^[0-9]*:[0-9]*:[0-9]*$`).MatchString
var lastError error
func Clock(timeInHHmmSS string) (string, error) {
if !validTimeFormat(timeInHHmmSS) {
return "", errors.New("invalid argument, " + timeInHHmmSS + " is invalid time format")
}
hhmmSS := strings.Split(timeInHHmmSS, ":")
hours, err := strconv.Atoi(hhmmSS[0])
minutes, err := strconv.Atoi(hhmmSS[1])
seconds, err := strconv.Atoi(hhmmSS[2])
if err != nil {
return "", err
}
secondsRow := errorHandler(converter.ConvertSecondsRow(seconds))
fiveHoursRow := errorHandler(converter.ConvertFiveHours(hours))
singleHoursRow := errorHandler(converter.ConvertSingleHours(hours))
fiveMinutesRow := errorHandler(converter.ConvertFiveMinutes(minutes))
singleMinutesRow := errorHandler(converter.ConvertSingleMinutes(minutes))
if lastError != nil {
return "", lastError
}
return fmt.Sprintf("%s%s%s%s%s", secondsRow, fiveHoursRow, singleHoursRow, fiveMinutesRow, singleMinutesRow), nil
}
func errorHandler(result string, err error) (string) {
if err != nil {
lastError = err
}
return result
}