forked from expr-lang/expr
/
func_date.go
107 lines (96 loc) · 2.09 KB
/
func_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
package expr
import (
"github.com/datasweet/cast"
)
// DATE_DIFF(x date, y date)
// Returns the difference in days between X and Y (X - Y)
var dateDiff binaryOperatorFunc = func(x interface{}, y interface{}) interface{} {
cx, okx := cast.AsTime(x)
cy, oky := cast.AsTime(y)
if !okx || !oky {
return nil
}
return cx.Sub(cy).Hours() / 24
}
// DAY(x date)
// Returns the day of X
var day unaryOperatorFunc = func(x interface{}) interface{} {
cx, okx := cast.AsTime(x)
if !okx {
return nil
}
return cx.Day()
}
// HOUR(x date)
// Returns the hours in X in UTC timezone
var hour unaryOperatorFunc = func(x interface{}) interface{} {
cx, okx := cast.AsTime(x)
if !okx {
return nil
}
return cx.Hour()
}
// MINUTE(x date)
// Returns the minutes in X in UTC timezone
var minute unaryOperatorFunc = func(x interface{}) interface{} {
cx, okx := cast.AsTime(x)
if !okx {
return nil
}
return cx.Minute()
}
// MONTH(x date)
// Returns the month in X
var month unaryOperatorFunc = func(x interface{}) interface{} {
cx, okx := cast.AsTime(x)
if !okx {
return nil
}
return int(cx.Month())
}
// QUARTER(x date)
// Returns the quarter of X
var quarter unaryOperatorFunc = func(x interface{}) interface{} {
cx, okx := cast.AsTime(x)
if !okx {
return nil
}
return ((int(cx.Month())-1)/3 + 1)
}
// SECOND(x date)
// Returns the seconds in X in UTC timezone
var second unaryOperatorFunc = func(x interface{}) interface{} {
cx, okx := cast.AsTime(x)
if !okx {
return nil
}
return cx.Second()
}
// WEEK(x date)
// Returns the week of X from start of year as per ISO 8601 standard
var week unaryOperatorFunc = func(x interface{}) interface{} {
cx, okx := cast.AsTime(x)
if !okx {
return nil
}
_, w := cx.ISOWeek()
return w
}
// WEEKDAY(x date)
// Returns the day of the week of X
var weekday unaryOperatorFunc = func(x interface{}) interface{} {
cx, okx := cast.AsTime(x)
if !okx {
return nil
}
return int(cx.Weekday())
}
// YEAR(x date)
// Returns the year of X
var year unaryOperatorFunc = func(x interface{}) interface{} {
cx, okx := cast.AsTime(x)
if !okx {
return nil
}
return cx.Year()
}