-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
time.go
111 lines (101 loc) · 2.24 KB
/
time.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package toki
import (
"bytes"
"database/sql/driver"
"fmt"
"strconv"
"strings"
"time"
)
// Time represents an SQL TIME value.
type Time struct {
Hours int
Minutes int
Seconds int
}
// UnmarshalText implements the encoding TextUnmarshaler interface.
func (t *Time) UnmarshalText(text []byte) error {
parts := strings.Split(string(text), ":")
loop:
for i, p := range parts {
n, err := strconv.Atoi(p)
if err != nil {
return err
}
switch i {
case 0:
t.Hours = n
case 1:
t.Minutes = n
case 2:
t.Seconds = n
default:
break loop
}
}
return nil
}
// Scan implements the driver Scanner interface.
func (t *Time) Scan(src interface{}) error {
switch x := src.(type) {
case []byte:
return t.UnmarshalText(x)
case string:
return t.UnmarshalText([]byte(x))
case time.Time:
t.Hours = x.Hour()
t.Minutes = x.Minute()
t.Seconds = x.Second()
return nil
}
return fmt.Errorf("unsupported type: %T", src)
}
// MarshalText implements the encoding TextMarshaler interface.
// Encodes to hh:mm:ss and omits the seconds if 0.
func (t Time) MarshalText() (text []byte, err error) {
var buf bytes.Buffer
buf.WriteString(strconv.Itoa(t.Hours))
buf.WriteByte(':')
if t.Minutes < 10 {
buf.WriteByte('0')
}
buf.WriteString(strconv.Itoa(t.Minutes))
if t.Seconds != 0 {
buf.WriteByte(':')
if t.Seconds < 10 {
buf.WriteByte('0')
}
buf.WriteString(strconv.Itoa(t.Seconds))
}
return buf.Bytes(), nil
}
// Value implements the driver Valuer interface.
func (t Time) Value() (driver.Value, error) {
return t.MarshalText()
}
// String returns a string representation of this Time.
func (t Time) String() string {
text, _ := t.MarshalText()
return string(text)
}
// Equals returns true if this Time and the given Time are equal.
// If they are both null it will return true.
func (t Time) Equals(other Time) bool {
return t.Hours == other.Hours &&
t.Minutes == other.Minutes &&
t.Seconds == other.Seconds
}
// ParseTime tries to parse the given time.
func ParseTime(text string) (Time, error) {
t := &Time{}
err := t.UnmarshalText([]byte(text))
return *t, err
}
// MustParseTime parses the given time or panics.
func MustParseTime(text string) Time {
t, err := ParseTime(text)
if err != nil {
panic(err)
}
return t
}