forked from moisespsena-go/aorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
148 lines (123 loc) · 2.67 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
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
package types
import (
"database/sql/driver"
"fmt"
"strconv"
"time"
"github.com/go-aorm/aorm"
)
type Time struct {
h, m, s int
}
func NewTime(h, m, s int) Time {
return Time{h, m, s}
}
func (this Time) Time() time.Time {
return time.Date(0, 0, 0, this.h, this.m, this.s, 0, time.UTC)
}
func (this Time) Hour() int {
return this.h
}
func (this Time) Minute() int {
return this.m
}
func (this Time) Second() int {
return this.s
}
func (Time) AormDataType(aorm.Dialector) string {
return "TIME"
}
func (this Time) Value() (driver.Value, error) {
return this.String(), nil
}
func (this *Time) Scan(src interface{}) (err error) {
var t time.Time
if src != nil {
switch st := src.(type) {
case string:
t, err = time.Parse("15:04:05", st)
this.h, this.m, this.s = t.Hour(), t.Minute(), t.Second()
return
case []byte:
return this.Scan(string(st))
}
}
return nil
}
func (this Time) String() string {
return fmt.Sprintf("%02d:%02d:%02d", this.h, this.m, this.s)
}
type TimeZ struct {
h, m, s, zh, zm int
}
func NewTimeZ(h, m, s, zh, zm int) TimeZ {
return TimeZ{h, m, s, zh, zm}
}
func (this TimeZ) Hour() int {
return this.h
}
func (this TimeZ) Minute() int {
return this.m
}
func (this TimeZ) Second() int {
return this.s
}
func (this TimeZ) Zone() (h, m int) {
return this.zh, this.zm
}
func (this TimeZ) Time() time.Time {
loc, _ := time.Parse("-0700", fmt.Sprintf("%03d%02d", this.zh, this.zm))
return time.Date(0, 0, 0, this.h, this.m, this.s, 0, loc.Location())
}
func (TimeZ) AormDataType(aorm.Dialector) string {
return "TIMETZ"
}
func (this TimeZ) Value() (driver.Value, error) {
return this.String(), nil
}
func (this *TimeZ) Scan(src interface{}) (err error) {
var t time.Time
if src != nil {
switch st := src.(type) {
case string:
if t, err = time.Parse("15:04:05 -07:00", st); err != nil {
if t, err = time.Parse("15:04:05 -0700", st); err != nil {
return
}
}
this.h, this.m, this.s = t.Hour(), t.Minute(), t.Second()
this.zh, this.zm = TZOfTime(t)
return
case []byte:
return this.Scan(string(st))
}
}
return nil
}
func (this TimeZ) String() string {
var signal string
var zh = this.zh
if zh < 0 {
signal = "-"
zh *= -1
} else {
signal = "+"
}
return fmt.Sprintf("%02d:%02d:%02d %s%02d:%02d", this.h, this.m, this.s, signal, zh, this.zm)
}
func ParseTZ(tzs string) (zh, zm int, err error) {
var t time.Time
if t, err = time.Parse("-07:00", tzs); err != nil {
if t, err = time.Parse("-0700", tzs); err != nil {
return
}
}
zh, zh = TZOfTime(t)
return
}
func TZOfTime(t time.Time) (zh, zm int) {
loc := t.Format("-0700")
zh, _ = strconv.Atoi(loc[0:3])
zm, _ = strconv.Atoi(loc[3:])
return
}