/
parsing.go
274 lines (245 loc) · 9.37 KB
/
parsing.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package pgdate
import (
"time"
"github.com/ruiaylin/pgparser/pgwire/pgcode"
"github.com/ruiaylin/pgparser/pgwire/pgerror"
"github.com/ruiaylin/pgparser/utils/timeutil"
"github.com/cockroachdb/errors"
)
// Various keywords that appear in timestamps.
const (
// Alias for UTC. No idea who actually uses this.
keywordAllBalls = "allballs"
keywordAM = "am"
keywordEpoch = "epoch"
keywordEraAD = "ad"
keywordEraBC = "bc"
keywordEraBCE = "bce"
keywordEraCE = "ce"
// Alias for UTC.
keywordGMT = "gmt"
keywordInfinity = "infinity"
keywordNow = "now"
keywordPM = "pm"
keywordToday = "today"
keywordTomorrow = "tomorrow"
keywordUTC = "utc"
keywordYesterday = "yesterday"
// Alias for UTC.
keywordZ = "z"
// Alias for UTC.
keywordZulu = "zulu"
)
// Commonly-used collections of fields.
var (
dateFields = newFieldSet(fieldYear, fieldMonth, fieldDay, fieldEra)
dateRequiredFields = newFieldSet(fieldYear, fieldMonth, fieldDay)
timeFields = newFieldSet(
fieldHour, fieldMinute, fieldSecond, fieldNanos, fieldMeridian,
fieldTZHour, fieldTZMinute, fieldTZSecond)
timeRequiredFields = newFieldSet(fieldHour, fieldMinute)
db2TimeRequiredFields = newFieldSet(fieldHour, fieldMinute, fieldSecond)
dateTimeFields = dateFields.AddAll(timeFields)
tzFields = newFieldSet(fieldTZHour, fieldTZMinute, fieldTZSecond)
)
// These are sentinel values for handling special values:
// https://www.postgresql.org/docs/10/static/datatype-datetime.html#DATATYPE-DATETIME-SPECIAL-TABLE
var (
TimeEpoch = timeutil.Unix(0, 0)
// TimeInfinity represents the "highest" possible time.
// TODO (#41564): this should actually behave as infinity, i.e. any operator
// leaves this as infinity. This time should always be greater than any other time.
// We should probably use the next microsecond after this value, i.e. timeutil.Unix(9224318016000, 0).
// Postgres uses math.MaxInt64 microseconds as the infinity value.
// See: https://github.com/postgres/postgres/blob/42aa1f0ab321fd43cbfdd875dd9e13940b485900/src/include/datatype/timestamp.h#L107.
TimeInfinity = timeutil.Unix(9224318016000-1, 999999000)
// TimeNegativeInfinity represents the "lowest" possible time.
// TODO (#41564): this should actually behave as -infinity, i.e. any operator
// leaves this as -infinity. This time should always be less than any other time.
// We should probably use the next microsecond before this value, i.e. timeutil.Unix(9224318016000-1, 999999000).
// Postgres uses math.MinInt64 microseconds as the -infinity value.
// See: https://github.com/postgres/postgres/blob/42aa1f0ab321fd43cbfdd875dd9e13940b485900/src/include/datatype/timestamp.h#L107.
TimeNegativeInfinity = timeutil.Unix(-210866803200, 0)
)
//go:generate stringer -type=ParseMode
// ParseMode controls the resolution of ambiguous date formats such as
// `01/02/03`.
type ParseMode uint
// These are the various parsing modes that determine in which order
// we should look for years, months, and date.
// ParseModeYMD is the default value.
const (
ParseModeYMD ParseMode = iota
ParseModeDMY
ParseModeMDY
)
// ParseDate converts a string into Date.
//
// Any specified timezone is inconsequential. Examples:
// - "now": parses to the local date (in the current timezone)
// - "2020-06-26 01:09:15.511971": parses to '2020-06-26'
// - "2020-06-26 01:09:15.511971-05": parses to '2020-06-26'
//
// The dependsOnContext return value indicates if we had to consult the given
// `now` value (either for the time or the local timezone).
//
func ParseDate(now time.Time, mode ParseMode, s string) (_ Date, dependsOnContext bool, _ error) {
fe := fieldExtract{
currentTime: now,
mode: mode,
required: dateRequiredFields,
// We allow time fields to be provided since they occur after
// the date fields that we're really looking for and for
// time values like 24:00:00, would push into the next day.
wanted: dateTimeFields,
}
if err := fe.Extract(s); err != nil {
return Date{}, false, parseError(err, "date", s)
}
date, err := fe.MakeDate()
return date, fe.currentTimeUsed, err
}
// ParseTime converts a string into a time value on the epoch day.
//
// The dependsOnContext return value indicates if we had to consult the given
// `now` value (either for the time or the local timezone).
func ParseTime(
now time.Time, mode ParseMode, s string,
) (_ time.Time, dependsOnContext bool, _ error) {
fe := fieldExtract{
currentTime: now,
required: timeRequiredFields,
wanted: timeFields,
}
if err := fe.Extract(s); err != nil {
// It's possible that the user has given us a complete
// timestamp string; let's try again, accepting more fields.
fe = fieldExtract{
currentTime: now,
mode: mode,
required: timeRequiredFields,
wanted: dateTimeFields,
}
if err := fe.Extract(s); err != nil {
return TimeEpoch, false, parseError(err, "time", s)
}
}
res := fe.MakeTime()
return res, fe.currentTimeUsed, nil
}
// ParseTimeWithoutTimezone converts a string into a time value on the epoch
// day, dropping any timezone information. The returned time always has UTC
// location.
//
// Any specified timezone is inconsequential. Examples:
// - "now": parses to the local time of day (in the current timezone)
// - "01:09:15.511971" and "01:09:15.511971-05" parse to the same result
//
// The dependsOnContext return value indicates if we had to consult the given
// `now` value (either for the time or the local timezone).
func ParseTimeWithoutTimezone(
now time.Time, mode ParseMode, s string,
) (_ time.Time, dependsOnContext bool, _ error) {
fe := fieldExtract{
currentTime: now,
required: timeRequiredFields,
wanted: timeFields,
}
if err := fe.Extract(s); err != nil {
// It's possible that the user has given us a complete
// timestamp string; let's try again, accepting more fields.
fe = fieldExtract{
currentTime: now,
mode: mode,
required: timeRequiredFields,
wanted: dateTimeFields,
}
if err := fe.Extract(s); err != nil {
return TimeEpoch, false, parseError(err, "time", s)
}
}
res := fe.MakeTimeWithoutTimezone()
return res, fe.currentTimeUsed, nil
}
// ParseTimestamp converts a string into a timestamp.
//
// The dependsOnContext return value indicates if we had to consult the given
// `now` value (either for the time or the local timezone).
func ParseTimestamp(
now time.Time, mode ParseMode, s string,
) (_ time.Time, dependsOnContext bool, _ error) {
fe := fieldExtract{
mode: mode,
currentTime: now,
// A timestamp only actually needs a date component; the time
// would be midnight.
required: dateRequiredFields,
wanted: dateTimeFields,
}
if err := fe.Extract(s); err != nil {
return TimeEpoch, false, parseError(err, "timestamp", s)
}
res := fe.MakeTimestamp()
return res, fe.currentTimeUsed, nil
}
// ParseTimestampWithoutTimezone converts a string into a timestamp, stripping
// away any timezone information. Any specified timezone is inconsequential. The
// returned time always has UTC location.
//
// For example, all these inputs return 2020-06-26 01:02:03 +0000 UTC:
// - '2020-06-26 01:02:03';
// - '2020-06-26 01:02:03+04';
// - 'now', if the local local time (in the current timezone) is
// 2020-06-26 01:02:03. Note that this does not represent the same time
// instant, but the one that "reads" the same in UTC.
//
// The dependsOnContext return value indicates if we had to consult the given
// `now` value (either for the time or the local timezone).
func ParseTimestampWithoutTimezone(
now time.Time, mode ParseMode, s string,
) (_ time.Time, dependsOnContext bool, _ error) {
fe := fieldExtract{
mode: mode,
currentTime: now,
// A timestamp only actually needs a date component; the time
// would be midnight.
required: dateRequiredFields,
wanted: dateTimeFields,
}
if err := fe.Extract(s); err != nil {
return TimeEpoch, false, parseError(err, "timestamp", s)
}
res := fe.MakeTimestampWithoutTimezone()
return res, fe.currentTimeUsed, nil
}
// badFieldPrefixError constructs an error with pg code InvalidDatetimeFormat.
func badFieldPrefixError(field field, prefix rune) error {
return inputErrorf("unexpected separator '%s' for field %s",
string(prefix), errors.Safe(field.Pretty()))
}
// inputErrorf returns an error with pg code InvalidDatetimeFormat.
func inputErrorf(format string, args ...interface{}) error {
err := errors.Newf(format, args...)
return pgerror.WithCandidateCode(err, pgcode.InvalidDatetimeFormat)
}
// outOfRangeError returns an error with pg code DatetimeFieldOverflow.
func outOfRangeError(field string, val int) error {
err := errors.Newf("field %s value %d is out of range", errors.Safe(field), errors.Safe(val))
return pgerror.WithCandidateCode(err, pgcode.DatetimeFieldOverflow)
}
// parseError ensures that any error we return to the client will
// be some kind of error with a pg code.
func parseError(err error, kind string, s string) error {
return pgerror.WithCandidateCode(
errors.Wrapf(err, "parsing as type %s", errors.Safe(kind)),
pgcode.InvalidDatetimeFormat)
}