forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kitchen_time.go
174 lines (145 loc) · 3.83 KB
/
kitchen_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package jsonx
import (
"fmt"
"strconv"
"strings"
"time"
)
// KitchenTimeLayout represents the "3:04 PM" Go time format, similar to time.Kitchen.
const KitchenTimeLayout = "3:04 PM"
// KitchenTime holds a json "3:04 PM" time.
type KitchenTime time.Time
var ErrParseKitchenTimeColon = fmt.Errorf("parse kitchen time: missing ':' character")
func parseKitchenTime(s string) (KitchenTime, error) {
// Remove any second,millisecond variable (probably given by postgres 00:00:00.000000).
// required(00:00)remove(:00.000000)
firstIndex := strings.IndexByte(s, ':')
if firstIndex == -1 {
return KitchenTime{}, ErrParseKitchenTimeColon
} else {
nextIndex := strings.LastIndexByte(s, ':')
spaceIdx := strings.LastIndexByte(s, ' ')
if nextIndex > firstIndex && spaceIdx > 0 {
tmp := s[0:nextIndex]
s = tmp + s[spaceIdx:]
}
}
tt, err := time.Parse(KitchenTimeLayout, s)
if err != nil {
return KitchenTime{}, err
}
return KitchenTime(tt), nil
}
// ParseKitchenTime reads from "s" and returns the KitchenTime time.
func ParseKitchenTime(s string) (KitchenTime, error) {
if s == "" || s == "null" {
return KitchenTime{}, nil
}
return parseKitchenTime(s)
}
// UnmarshalJSON binds the json "data" to "t" with the `KitchenTimeLayout`.
func (t *KitchenTime) UnmarshalJSON(data []byte) error {
if t == nil {
return fmt.Errorf("kitchen time: dest is nil")
}
if isNull(data) {
return nil
}
data = trimQuotes(data)
if len(data) == 0 {
return nil
}
tt, err := parseKitchenTime(string(data))
if err != nil {
return err
}
*t = KitchenTime(tt)
return nil
}
// MarshalJSON returns the json representation of the "t".
func (t KitchenTime) MarshalJSON() ([]byte, error) {
if s := t.String(); s != "" {
s = strconv.Quote(s)
return []byte(s), nil
}
return emptyQuoteBytes, nil
}
// IsZero reports whether "t" is zero time.
// It completes the pg.Zeroer interface.
func (t KitchenTime) IsZero() bool {
return t.Value().IsZero()
}
// Value returns the standard time type.
func (t KitchenTime) Value() time.Time {
return time.Time(t)
}
// String returns the text representation of the date
// formatted based on the `KitchenTimeLayout`.
// If date is zero it returns an empty string.
func (t KitchenTime) String() string {
tt := t.Value()
if tt.IsZero() {
return ""
}
return tt.Format(KitchenTimeLayout)
}
// Scan completes the pg and native sql driver.Scanner interface
// reading functionality of a custom type.
func (t *KitchenTime) Scan(src interface{}) error {
switch v := src.(type) {
case time.Time: // type was set to timestamp.
if v.IsZero() {
return nil // don't set zero, ignore it.
}
*t = KitchenTime(v)
case string: // type was set to time, input example: 10:00:00.000000
d, err := ParseTimeNotationDuration(v)
if err != nil {
return fmt.Errorf("kitchen time: convert to time notation first: %w", err)
}
s := kitchenTimeStringFromDuration(d.ToDuration())
*t, err = ParseKitchenTime(s)
return err
case int64: // timestamp with integer.
u := time.Unix(v/1000, v%1000)
s := kitchenTimeStringFromHourAndMinute(u.Hour(), u.Minute())
tt, err := ParseKitchenTime(s)
if err != nil {
return err
}
*t = tt
case nil:
*t = KitchenTime(time.Time{})
default:
return fmt.Errorf("KitchenTime: unknown type of: %T", v)
}
return nil
}
func kitchenTimeStringFromDuration(dt time.Duration) string {
hour := int(dt.Hours())
minute := 0
if totalMins := dt.Minutes(); totalMins > 0 {
minute := int(totalMins / 60)
if minute < 0 {
minute = 0
}
}
return kitchenTimeStringFromHourAndMinute(hour, minute)
}
func kitchenTimeStringFromHourAndMinute(hour, minute int) string {
ampm := "AM"
if hour/12 == 1 {
ampm = "PM"
}
th := hour % 12
hh := strconv.Itoa(th)
if th < 10 {
hh = "0" + hh
}
tm := minute
mm := strconv.Itoa(tm)
if tm < 10 {
mm = "0" + mm
}
return hh + ":" + mm + " " + ampm
}