-
Notifications
You must be signed in to change notification settings - Fork 0
/
validated_time_of_day.go
69 lines (55 loc) · 1.49 KB
/
validated_time_of_day.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package server
import (
"bytes"
"fmt"
"github.com/pkg/errors"
)
// ValidatedTimeOfDay time is a valid time of day
type ValidatedTimeOfDay struct {
hour int
min int
sec int
}
// DefaultTimeOfDay returns a ValidatedTimeOfDay with midnight
func DefaultTimeOfDay() *ValidatedTimeOfDay {
return &ValidatedTimeOfDay{
hour: 0,
min: 0,
sec: 0}
}
// UnmarshalText unmarshals a text in the format hh:mm:ss to a ValidatedTimeOfDay.
// Hour must be less than 24 and greater than 0
// Min must be less then 60 and greater than 0
// Sec must be less than 60 and greater than 0
// Failing any of the above validation criteria returns an error
func (tod *ValidatedTimeOfDay) UnmarshalText(text []byte) error {
_, err := fmt.Fscanf(bytes.NewReader(text), "%2d:%2d:%2d", &tod.hour, &tod.min, &tod.sec)
if err != nil {
return errors.Wrapf(err, "Could not parse time of day")
}
return tod.validate()
}
func (tod *ValidatedTimeOfDay) validate() error {
if tod.hour >= 24 || tod.hour < 0 {
return fmt.Errorf("Invalid hour value %d", tod.hour)
}
if tod.min >= 60 || tod.min < 0 {
return fmt.Errorf("Invalid minute value %d", tod.min)
}
if tod.sec >= 60 || tod.sec < 0 {
return fmt.Errorf("Invalid second value %d", tod.sec)
}
return nil
}
// Hour returns the hour
func (tod *ValidatedTimeOfDay) Hour() int {
return tod.hour
}
// Min returns the minute
func (tod *ValidatedTimeOfDay) Min() int {
return tod.min
}
// Sec returns the second
func (tod *ValidatedTimeOfDay) Sec() int {
return tod.sec
}