-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathconverter.go
155 lines (131 loc) · 4.43 KB
/
converter.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
package types
import (
"encoding/binary"
"math/big"
"strconv"
"strings"
"time"
)
func TimeToTIME_MILLIS(t time.Time, adjustedToUTC bool) int64 {
return TimeToTIME_MICROS(t, adjustedToUTC) / time.Millisecond.Microseconds()
}
func TimeToTIME_MICROS(t time.Time, adjustedToUTC bool) int64 {
if adjustedToUTC {
tu := t.UTC()
h, m, s, ns := int64(tu.Hour()), int64(tu.Minute()), int64(tu.Second()), int64(tu.Nanosecond())
nanos := h*time.Hour.Nanoseconds() + m*time.Minute.Nanoseconds() + s*time.Second.Nanoseconds() + ns*time.Nanosecond.Nanoseconds()
return nanos / time.Microsecond.Nanoseconds()
} else {
h, m, s, ns := int64(t.Hour()), int64(t.Minute()), int64(t.Second()), int64(t.Nanosecond())
nanos := h*time.Hour.Nanoseconds() + m*time.Minute.Nanoseconds() + s*time.Second.Nanoseconds() + ns*time.Nanosecond.Nanoseconds()
return nanos / time.Microsecond.Nanoseconds()
}
}
func TimeToTIMESTAMP_MILLIS(t time.Time, adjustedToUTC bool) int64 {
return TimeToTIMESTAMP_MICROS(t, adjustedToUTC) / time.Millisecond.Microseconds()
}
func TIMESTAMP_MILLISToTime(millis int64, adjustedToUTC bool) time.Time {
return TIMESTAMP_MICROSToTime(millis*time.Millisecond.Microseconds(), adjustedToUTC)
}
func TimeToTIMESTAMP_MICROS(t time.Time, adjustedToUTC bool) int64 {
return TimeToTIMESTAMP_NANOS(t, adjustedToUTC) / time.Microsecond.Nanoseconds()
}
func TIMESTAMP_MICROSToTime(micros int64, adjustedToUTC bool) time.Time {
return TIMESTAMP_NANOSToTime(micros*time.Microsecond.Nanoseconds(), adjustedToUTC)
}
func TimeToTIMESTAMP_NANOS(t time.Time, adjustedToUTC bool) int64 {
if adjustedToUTC {
return t.UnixNano()
} else {
epoch := time.Date(1970, 1, 1, 0, 0, 0, 0, t.Location())
return t.Sub(epoch).Nanoseconds()
}
}
func TIMESTAMP_NANOSToTime(nanos int64, adjustedToUTC bool) time.Time {
if adjustedToUTC {
return time.Unix(0, nanos).UTC()
} else {
epoch := time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local)
t := epoch.Add(time.Nanosecond * time.Duration(nanos))
return t
}
}
//From Spark
//https://github.com/apache/spark/blob/b9f2f78de59758d1932c1573338539e485a01112/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala#L47
const (
JULIAN_DAY_OF_EPOCH int64 = 2440588
MICROS_PER_DAY int64 = 3600 * 24 * 1000 * 1000
)
//From Spark
//https://github.com/apache/spark/blob/b9f2f78de59758d1932c1573338539e485a01112/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala#L180
func toJulianDay(t time.Time) (int32, int64) {
utc := t.UTC()
nanos := utc.UnixNano()
micros := nanos / time.Microsecond.Nanoseconds()
julianUs := micros + JULIAN_DAY_OF_EPOCH*MICROS_PER_DAY
days := int32(julianUs / MICROS_PER_DAY)
us := (julianUs % MICROS_PER_DAY) * 1000
return days, us
}
//From Spark
//https://github.com/apache/spark/blob/b9f2f78de59758d1932c1573338539e485a01112/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala#L170
func fromJulianDay(days int32, nanos int64) time.Time {
nanos = ((int64(days)-JULIAN_DAY_OF_EPOCH)*MICROS_PER_DAY + nanos/1000) * 1000
sec, nsec := nanos/time.Second.Nanoseconds(), nanos%time.Second.Nanoseconds()
t := time.Unix(sec, nsec)
return t.UTC()
}
func TimeToINT96(t time.Time) string {
days, nanos := toJulianDay(t)
bs1 := make([]byte, 8)
binary.LittleEndian.PutUint64(bs1, uint64(nanos))
bs2 := make([]byte, 4)
binary.LittleEndian.PutUint32(bs2, uint32(days))
bs := append(bs1, bs2...)
return string(bs)
}
func INT96ToTime(int96 string) time.Time {
nanos := binary.LittleEndian.Uint64([]byte(int96[:8]))
days := binary.LittleEndian.Uint32([]byte(int96[8:]))
return fromJulianDay(int32(days), int64(nanos))
}
func DECIMAL_INT_ToString(dec int64, precision int, scale int) string {
sign := ""
if dec < 0 {
sign = "-"
dec = -dec
}
ans := strconv.FormatInt(dec, 10)
if scale > 0 {
if scale > len(ans) {
ans = strings.Repeat("0", scale-(len(ans))+1) + ans
}
radixLoc := len(ans) - scale
ans = ans[:radixLoc] + "." + ans[radixLoc:]
}
return sign + ans
}
func DECIMAL_BYTE_ARRAY_ToString(dec []byte, precision int, scale int) string {
sign := ""
if dec[0] > 0x7f {
sign = "-"
for i := range dec {
dec[i] = dec[i] ^ 0xff
}
}
a := new(big.Int)
a.SetBytes(dec)
if sign == "-" {
a = a.Add(a, big.NewInt(1))
}
sa := a.Text(10)
if scale > 0 {
ln := len(sa)
if ln < scale+1 {
sa = strings.Repeat("0", scale+1-ln) + sa
ln = scale + 1
}
sa = sa[:ln-scale] + "." + sa[ln-scale:]
}
return sign + sa
}