-
Notifications
You must be signed in to change notification settings - Fork 0
/
w.go
193 lines (159 loc) · 4.58 KB
/
w.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package dm
import (
"strings"
"time"
)
const (
Seconds_1900_1970 = 2209017600
OFFSET_YEAR = 0
OFFSET_MONTH = 1
OFFSET_DAY = 2
OFFSET_HOUR = 3
OFFSET_MINUTE = 4
OFFSET_SECOND = 5
OFFSET_MILLISECOND = 6
OFFSET_TIMEZONE = 7
DT_LEN = 8
INVALID_VALUE = int(INT32_MIN)
)
type DmTimestamp struct {
dt []int
dtype int
scale int
oracleFormatPattern string
oracleDateLanguage int
}
func newDmTimestampFromDt(dt []int, dtype int, scale int) *DmTimestamp {
dmts := new(DmTimestamp)
dmts.dt = dt
dmts.dtype = dtype
dmts.scale = scale
return dmts
}
func newDmTimestampFromBytes(bytes []byte, column column, conn *Connection) *DmTimestamp {
dmts := new(DmTimestamp)
dmts.dt = decode(bytes, column.isBdta, int(column.colType), int(column.scale), int(conn.dmConnector.localTimezone), int(conn.DbTimezone))
if isLocalTimeZone(int(column.colType), int(column.scale)) {
dmts.scale = getLocalTimeZoneScale(int(column.colType), int(column.scale))
} else {
dmts.scale = int(column.scale)
}
dmts.dtype = int(column.colType)
dmts.scale = int(column.scale)
dmts.oracleDateLanguage = int(conn.OracleDateLanguage)
switch column.colType {
case DATE:
dmts.oracleFormatPattern = conn.FormatDate
case TIME:
dmts.oracleFormatPattern = conn.FormatTime
case TIME_TZ:
dmts.oracleFormatPattern = conn.FormatTimeTZ
case DATETIME:
dmts.oracleFormatPattern = conn.FormatTimestamp
case DATETIME_TZ:
dmts.oracleFormatPattern = conn.FormatTimestampTZ
}
return dmts
}
func NewDmTimestampFromString(str string) (*DmTimestamp, error) {
dt := make([]int, DT_LEN)
dtype, err := toDTFromString(strings.TrimSpace(str), dt)
if err != nil {
return nil, err
}
if dtype == DATE {
return newDmTimestampFromDt(dt, dtype, 0), nil
}
return newDmTimestampFromDt(dt, dtype, 6), nil
}
func NewDmTimestampFromTime(time time.Time) *DmTimestamp {
dt := toDTFromTime(time)
return newDmTimestampFromDt(dt, DATETIME, 6)
}
func (dmTimestamp *DmTimestamp) ToTime() time.Time {
return toTimeFromDT(dmTimestamp.dt, 0)
}
// 获取年月日时分秒毫秒时区
func (dmTimestamp *DmTimestamp) GetDt() []int {
return dmTimestamp.dt
}
func (dmTimestamp *DmTimestamp) CompareTo(ts DmTimestamp) int {
if dmTimestamp.ToTime().Equal(ts.ToTime()) {
return 0
} else if dmTimestamp.ToTime().Before(ts.ToTime()) {
return -1
} else {
return 1
}
}
func (dmTimestamp *DmTimestamp) String() string {
if dmTimestamp.oracleFormatPattern != "" {
return dtToStringByOracleFormat(dmTimestamp.dt, dmTimestamp.oracleFormatPattern, dmTimestamp.oracleDateLanguage)
}
return dtToString(dmTimestamp.dt, dmTimestamp.dtype, dmTimestamp.scale)
}
func (dest *DmTimestamp) Scan(src interface{}) error {
if dest == nil {
return ECGO_STORE_IN_NIL_POINTER.throw()
}
switch src := src.(type) {
case nil:
*dest = *new(DmTimestamp)
return nil
case *DmTimestamp:
*dest = *src
return nil
case time.Time:
ret := NewDmTimestampFromTime(src)
*dest = *ret
return nil
default:
return UNSUPPORTED_SCAN
}
}
func (dmTimestamp *DmTimestamp) toBytes() ([]byte, error) {
return encode(dmTimestamp.dt, dmTimestamp.dtype, dmTimestamp.scale, dmTimestamp.dt[OFFSET_TIMEZONE])
}
/**
* 获取当前对象的年月日时分秒,如果原来没有decode会先decode;
*/
func (dmTimestamp *DmTimestamp) getDt() []int {
return dmTimestamp.dt
}
func (dmTimestamp *DmTimestamp) getTime() int64 {
sec := toTimeFromDT(dmTimestamp.dt, 0).Unix()
return sec + int64(dmTimestamp.dt[OFFSET_MILLISECOND])
}
func (dmTimestamp *DmTimestamp) setTime(time int64) {
timeInMillis := (time / 1000) * 1000
nanos := (int64)((time % 1000) * 1000000)
if nanos < 0 {
nanos = 1000000000 + nanos
timeInMillis = (((time / 1000) - 1) * 1000)
}
dmTimestamp.dt = toDTFromUnix(timeInMillis, nanos)
}
func (dmTimestamp *DmTimestamp) setTimezone(tz int) error {
// DM中合法的时区取值范围为-12:59至+14:00
if tz <= -13*60 || tz > 14*60 {
return ECGO_INVALID_DATETIME_FORMAT.throw()
}
dmTimestamp.dt[OFFSET_TIMEZONE] = tz
return nil
}
func (dmTimestamp *DmTimestamp) getNano() int64 {
return int64(dmTimestamp.dt[OFFSET_MILLISECOND] * 1000)
}
func (dmTimestamp *DmTimestamp) setNano(nano int64) {
dmTimestamp.dt[OFFSET_MILLISECOND] = (int)(nano / 1000)
}
func (dmTimestamp *DmTimestamp) string() string {
if dmTimestamp.oracleFormatPattern != "" {
return dtToStringByOracleFormat(dmTimestamp.dt, dmTimestamp.oracleFormatPattern, dmTimestamp.oracleDateLanguage)
}
return dtToString(dmTimestamp.dt, dmTimestamp.dtype, dmTimestamp.scale)
}