forked from goccy/go-zetasqlite
/
function_date.go
199 lines (182 loc) · 5.42 KB
/
function_date.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
194
195
196
197
198
199
package internal
import (
"fmt"
"time"
)
func CURRENT_DATE(zone string) (Value, error) {
loc, err := toLocation(zone)
if err != nil {
return nil, err
}
return CURRENT_DATE_WITH_TIME(time.Now().In(loc))
}
func CURRENT_DATE_WITH_TIME(v time.Time) (Value, error) {
return DateValue(v), nil
}
func DATE(args ...Value) (Value, error) {
if len(args) == 3 {
year, err := args[0].ToInt64()
if err != nil {
return nil, err
}
month, err := args[1].ToInt64()
if err != nil {
return nil, err
}
day, err := args[2].ToInt64()
if err != nil {
return nil, err
}
return DateValue(time.Time{}.AddDate(int(year)-1, int(month)-1, int(day)-1)), nil
} else if len(args) == 2 {
t, err := args[0].ToTime()
if err != nil {
return nil, err
}
zone, err := args[1].ToString()
if err != nil {
return nil, err
}
loc, err := toLocation(zone)
if err != nil {
return nil, err
}
return DateValue(t.In(loc)), nil
} else {
t, err := args[0].ToTime()
if err != nil {
return nil, err
}
return DateValue(t), nil
}
}
func DATE_ADD(t time.Time, v int64, part string) (Value, error) {
switch part {
case "DAY":
return DateValue(t.AddDate(0, 0, int(v))), nil
case "WEEK":
return DateValue(t.AddDate(0, 0, int(v*7))), nil
case "MONTH":
return DateValue(addMonth(t, int(v))), nil
case "YEAR":
return DateValue(addYear(t, int(v))), nil
}
return nil, fmt.Errorf("unexpected part value %s", part)
}
func DATE_SUB(t time.Time, v int64, part string) (Value, error) {
switch part {
case "DAY":
return DateValue(t.AddDate(0, 0, int(-v))), nil
case "WEEK":
return DateValue(t.AddDate(0, 0, int(-v*7))), nil
case "MONTH":
return DateValue(addMonth(t, int(-v))), nil
case "YEAR":
return DateValue(addYear(t, int(-v))), nil
}
return nil, fmt.Errorf("unexpected part value %s", part)
}
func DATE_DIFF(a, b time.Time, part string) (Value, error) {
switch part {
case "DAY":
return IntValue(a.Day() - b.Day()), nil
case "WEEK":
_, aWeek := a.ISOWeek()
_, bWeek := b.ISOWeek()
return IntValue(aWeek - bWeek), nil
case "MONTH":
return IntValue(a.Month() - b.Month()), nil
case "YEAR":
return IntValue(a.Year() - b.Year()), nil
}
return nil, fmt.Errorf("unexpected part value %s", part)
}
func DATE_TRUNC(t time.Time, part string) (Value, error) {
switch part {
case "DAY":
return DateValue(time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())), nil
case "ISOWEEK":
return nil, fmt.Errorf("currently unsupported DATE_TRUNC with ISO_WEEK")
case "WEEK":
return DateValue(t.AddDate(0, 0, -int(t.Weekday()))), nil
case "MONTH":
return DateValue(time.Time{}.AddDate(t.Year()-1, int(t.Month())-1, 0)), nil
case "QUARTER":
return nil, fmt.Errorf("currently unsupported DATE_TRUNC with QUARTER")
case "YEAR":
return DateValue(time.Time{}.AddDate(t.Year()-1, 0, 0)), nil
case "ISOYEAR":
return nil, fmt.Errorf("currently unsupported DATE_TRUNC with ISO_YAER")
}
return nil, fmt.Errorf("unexpected part value %s", part)
}
func DATE_FROM_UNIX_DATE(unixdate int64) (Value, error) {
t := time.Unix(int64(time.Duration(unixdate)*24*time.Hour/time.Second), 0)
return DateValue(t), nil
}
func FORMAT_DATE(format string, t time.Time) (Value, error) {
s, err := formatTime(format, &t, FormatTypeDate)
if err != nil {
return nil, err
}
return StringValue(s), nil
}
func LAST_DAY(t time.Time, part string) (Value, error) {
switch part {
case "YEAR":
return DateValue(time.Date(t.Year()+1, time.Month(1), 0, 0, 0, 0, 0, t.Location())), nil
case "QUARTER":
return nil, fmt.Errorf("LAST_DAY: unimplemented QUARTER part")
case "MONTH":
return DateValue(t.AddDate(0, 1, -t.Day())), nil
case "WEEK":
return DateValue(t.AddDate(0, 0, 6-int(t.Weekday()))), nil
case "WEEK_MONDAY":
return DateValue(t.AddDate(0, 0, 7-int(t.Weekday()))), nil
case "WEEK_TUESDAY":
return DateValue(t.AddDate(0, 0, 8-int(t.Weekday()))), nil
case "WEEK_WEDNESDAY":
return DateValue(t.AddDate(0, 0, 9-int(t.Weekday()))), nil
case "WEEK_THURSDAY":
return DateValue(t.AddDate(0, 0, 10-int(t.Weekday()))), nil
case "WEEK_FRIDAY":
return DateValue(t.AddDate(0, 0, 11-int(t.Weekday()))), nil
case "WEEK_SATURDAY":
return DateValue(t.AddDate(0, 0, 12-int(t.Weekday()))), nil
case "ISOWEEK":
return DateValue(t.AddDate(0, 0, 6-int(t.Weekday()))), nil
case "ISOYEAR":
return DateValue(time.Date(t.Year()+1, time.Month(1), 0, 0, 0, 0, 0, t.Location())), nil
}
return nil, fmt.Errorf("LAST_DAY: unexpected part %s", part)
}
func PARSE_DATE(format, date string) (Value, error) {
t, err := parseTimeFormat(format, date, FormatTypeDate)
if err != nil {
return nil, err
}
return DateValue(*t), nil
}
func UNIX_DATE(t time.Time) (Value, error) {
return IntValue(t.Unix() / int64(24*time.Hour/time.Second)), nil
}
func addMonth(t time.Time, m int) time.Time {
curYear, curMonth, curDay := t.Date()
first := time.Date(curYear, curMonth, 1, 0, 0, 0, 0, t.Location())
year, month, _ := first.AddDate(0, m, 0).Date()
after := time.Date(year, month, curDay, 0, 0, 0, 0, time.UTC)
if month != after.Month() {
return first.AddDate(0, m+1, -1)
}
return t.AddDate(0, m, 0)
}
func addYear(t time.Time, y int) time.Time {
curYear, curMonth, curDay := t.Date()
first := time.Date(curYear, curMonth, 1, 0, 0, 0, 0, t.Location())
year, month, _ := first.AddDate(y, 0, 0).Date()
after := time.Date(year, month, curDay, 0, 0, 0, 0, t.Location())
if month != after.Month() {
return first.AddDate(y, 1, -1)
}
return t.AddDate(y, 0, 0)
}