MCPcopy Create free account
hub / github.com/dolthub/doltgresql / getFieldFromTimeVal

Function getFieldFromTimeVal

server/functions/extract.go:229–290  ·  view source on GitHub ↗

getFieldFromTimeVal returns the value for given field extracted from non-interval values.

(field string, tVal time.Time)

Source from the content-addressed store, hash-verified

227
228// getFieldFromTimeVal returns the value for given field extracted from non-interval values.
229func getFieldFromTimeVal(field string, tVal time.Time) (*apd.Decimal, error) {
230 switch strings.ToLower(field) {
231 case "century", "centuries":
232 if year := tVal.Year(); year <= 0 {
233 return numeric(math.Floor(float64(year-1)/100), false, 0)
234 } else {
235 return numeric(math.Ceil(float64(year)/100), false, 0)
236 }
237 case "day", "days":
238 return numeric(int64(tVal.Day()), false, 0)
239 case "decade", "decades":
240 return numeric(math.Floor(float64(tVal.Year())/10), false, 0)
241 case "dow":
242 return numeric(int64(tVal.Weekday()), false, 0)
243 case "doy":
244 return numeric(int64(tVal.YearDay()), false, 0)
245 case "epoch":
246 return numeric(float64(tVal.UnixMicro())/1000000, true, 6)
247 case "hour", "hours":
248 return numeric(int64(tVal.Hour()), false, 0)
249 case "isodow":
250 wd := int64(tVal.Weekday())
251 if wd == 0 {
252 wd = 7
253 }
254 return numeric(wd, false, 0)
255 case "isoyear":
256 year, _ := tVal.ISOWeek()
257 return numeric(int64(year), false, 0)
258 case "julian":
259 return numeric(int64(date2J(tVal.Year(), int(tVal.Month()), tVal.Day())), false, 0)
260 case "microsecond", "microseconds", "usec", "usecs":
261 w := float64(tVal.Second() * 1000000)
262 f := float64(tVal.Nanosecond()) / float64(1000)
263 return numeric(w+f, false, 0)
264 case "millennium", "millenniums":
265 return numeric(math.Ceil(float64(tVal.Year())/1000), false, 0)
266 case "millisecond", "milliseconds", "msec", "msecs":
267 w := float64(tVal.Second() * 1000)
268 f := float64(tVal.Nanosecond()) / float64(1000000)
269 return numeric(w+f, true, 3)
270 case "minute", "minutes":
271 return numeric(int64(tVal.Minute()), false, 0)
272 case "month", "months":
273 return numeric(int64(tVal.Month()), false, 0)
274 case "quarter":
275 q := (int(tVal.Month())-1)/3 + 1
276 return numeric(int64(q), false, 0)
277 case "second", "seconds":
278 w := float64(tVal.Second())
279 f := float64(tVal.Nanosecond()) / float64(1000000000)
280 return numeric(w+f, true, 6)
281
282 case "week":
283 _, week := tVal.ISOWeek()
284 return numeric(int64(week), false, 0)
285 case "year", "years":
286 return numeric(int64(tVal.Year()), false, 0)

Callers 2

extract.goFile · 0.85
date_part.goFile · 0.85

Calls 6

numericFunction · 0.85
date2JFunction · 0.85
HourMethod · 0.80
SecondMethod · 0.80
MinuteMethod · 0.80
ErrorfMethod · 0.65

Tested by

no test coverage detected