-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclocktime.go
92 lines (82 loc) · 1.68 KB
/
clocktime.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package timeext
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
)
var errInvalidType = errors.New("invalid type")
var ErrInvalidTimeFormat = errors.New("Invalid time format.")
type ClockTime struct {
Hour, Min, Sec int
}
func Clock(t time.Time) ClockTime {
return ClockTime{
t.Hour(),
t.Minute(),
t.Second(),
}
}
func (ct *ClockTime) Scan(value interface{}) error {
if value == nil {
ct.Hour = 0
ct.Min = 0
ct.Sec = 0
return nil
}
var s string
switch v := value.(type) {
case []byte:
s = string(v)
case string:
s = v
default:
return errInvalidType
}
parts := strings.Split(s, ":")
ct.Hour, _ = strconv.Atoi(parts[0])
ct.Min, _ = strconv.Atoi(parts[1])
ct.Sec, _ = strconv.Atoi(parts[2])
return nil
}
func (ct ClockTime) Value() (driver.Value, error) {
return ct.String(), nil
}
func (ct ClockTime) String() string {
return fmt.Sprintf("%02d:%02d:%02d", ct.Hour, ct.Min, ct.Sec)
}
func (ct *ClockTime) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err == nil {
parts := strings.Split(s, ":")
ct.Hour, ct.Min, ct.Sec = 0, 0, 0
ct.Hour, err = strconv.Atoi(parts[0])
if err != nil {
return ErrInvalidTimeFormat
}
if len(parts) > 1 {
ct.Min, err = strconv.Atoi(parts[1])
if err != nil {
return ErrInvalidTimeFormat
}
}
if len(parts) > 2 {
ct.Sec, err = strconv.Atoi(parts[2])
if err != nil {
return ErrInvalidTimeFormat
}
}
return nil
} else {
return err
}
}
func (ct ClockTime) MarshalJSON() ([]byte, error) {
return json.Marshal(ct.String())
}
func (ct ClockTime) SecondOfDay() int {
return ct.Sec + (ct.Min * 60) + (ct.Hour * 3600)
}