/
date.go
283 lines (233 loc) · 5.77 KB
/
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
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
275
276
277
278
279
280
281
282
283
// Copyright Suneido Software Corp. All rights reserved.
// Governed by the MIT license found in the LICENSE file.
package builtin
import (
"strings"
"time"
. "github.com/apmckinlay/gsuneido/core"
"github.com/apmckinlay/gsuneido/util/dnum"
"github.com/apmckinlay/gsuneido/util/regex"
"github.com/apmckinlay/gsuneido/util/str"
)
type suDateGlobal struct {
SuBuiltin
}
func init() {
ps := params(`(string=nil, pattern=nil,
year=nil, month=nil, day=nil,
hour=nil, minute=nil, second=nil, millisecond=nil)`)
Global.Builtin("Date", &suDateGlobal{SuBuiltin{Fn: Date,
BuiltinParams: BuiltinParams{ParamSpec: ps}}})
}
var tsPat = regex.Compile(`\A\d\d\d\d\d\d\d\d\.\d\d\d\d\d\d\d\d\d\d\d\d\Z`)
func Date(_ *Thread, args []Value) Value {
if args[0] != nil && hasFields(args) {
panic("usage: Date() or Date(string [, pattern]) or " +
"Date(year:, month:, day:, hour:, minute:, second:)")
}
if args[0] != nil {
if d, ok := args[0].(SuDate); ok {
return d
}
if t, ok := args[0].(SuTimestamp); ok {
return t
}
var d Value
s := AsStr(args[0])
if strings.HasPrefix(s, "#") || tsPat.Matches(s) {
d = DateFromLiteral(s)
} else if args[1] == nil {
d = ParseDate(s, "yMd")
} else {
d = ParseDate(s, AsStr(args[1]))
}
if d == NilDate {
return False
}
return d
} else if hasFields(args) {
return named(args)
}
return Now()
}
func toDate(v Value) SuDate {
if d, ok := v.(SuDate); ok {
return d
}
return v.(SuTimestamp).SuDate
}
func asDate(v Value) (SuDate, bool) {
if d, ok := v.(SuDate); ok {
return d, true
}
if t, ok := v.(SuTimestamp); ok {
return t.SuDate, true
}
return NilDate, false
}
func hasFields(args []Value) bool {
for i := 2; i <= 8; i++ {
if args[i] != nil {
return true
}
}
return false
}
func named(args []Value) Value {
now := Now()
year := now.Year()
month := now.Month()
day := now.Day()
hour := now.Hour()
minute := now.Minute()
second := now.Second()
millisecond := now.Millisecond()
if args[2] != nil {
year = ToInt(args[2])
}
if args[3] != nil {
month = ToInt(args[3])
}
if args[4] != nil {
day = ToInt(args[4])
}
if args[5] != nil {
hour = ToInt(args[5])
}
if args[6] != nil {
minute = ToInt(args[6])
}
if args[7] != nil {
second = ToInt(args[7])
}
if args[8] != nil {
millisecond = ToInt(args[8])
}
d := NormalizeDate(year, month, day, hour, minute, second, millisecond)
if d == NilDate {
return False
}
return d
}
func (d *suDateGlobal) Get(th *Thread, key Value) Value {
m := ToStr(key)
if fn, ok := dateStaticMethods[m]; ok {
return fn.(Value)
}
if fn, ok := ParamsMethods[m]; ok {
return NewSuMethod(d, fn.(Value))
}
return nil
}
func (d *suDateGlobal) Lookup(th *Thread, method string) Callable {
if fn, ok := dateStaticMethods[method]; ok {
return fn
}
return d.SuBuiltin.Lookup(th, method) // for Params
}
func (d *suDateGlobal) String() string {
return "Date /* builtin class */"
}
var msFactor = dnum.FromStr(".001")
var dateStaticMethods = methods()
var _ = staticMethod(date_Begin, "()")
func date_Begin() Value {
return DateBegin
}
var _ = staticMethod(date_End, "()")
func date_End() Value {
return DateEnd
}
var _ = exportMethods(&DateMethods)
var _ = method(date_MinusDays, "(date)")
func date_MinusDays(this Value, val Value) Value {
t1 := toDate(this)
if t2, ok := asDate(val); ok {
return IntVal(t1.MinusDays(t2))
}
panic("date.MinusDays requires date")
}
var _ = method(date_MinusSeconds, "(date)")
func date_MinusSeconds(this Value, val Value) Value {
t1 := toDate(this)
if t2, ok := asDate(val); ok {
if t1.Year()-t2.Year() >= 50 {
panic("date.MinusSeconds interval too large")
}
ms := t1.MinusMs(t2)
return SuDnum{Dnum: dnum.Mul(dnum.FromInt(ms), msFactor)}
}
panic("date.MinusSeconds requires date")
}
var _ = method(date_FormatEn, "(format)")
func date_FormatEn(this, arg Value) Value {
return SuStr(toDate(this).Format(ToStr(arg)))
}
var _ = method(date_GetLocalGMTBias, "()")
func date_GetLocalGMTBias(this Value) Value {
_, offset := toDate(this).ToGoTime().Zone()
return IntVal(-offset / 60)
}
var _ = method(date_Plus, "(years=0, months=0, days=0, "+
"hours=0, minutes=0, seconds=0, milliseconds=0)")
func date_Plus(th *Thread, this Value, args []Value) Value {
d := toDate(this).Plus(ToInt(args[0]), ToInt(args[1]),
ToInt(args[2]), ToInt(args[3]), ToInt(args[4]),
ToInt(args[5]), ToInt(args[6]))
if ts, ok := this.(SuTimestamp); ok {
ts.SuDate = d
return ts
}
return d
}
var _ = method(date_WeekDay, "(firstDay='Sun')")
func date_WeekDay(this, arg Value) Value {
i := dayOfWeek(arg)
return IntVal(((toDate(this).WeekDay() - i) + 7) % 7)
}
var _ = method(date_Year, "()")
func date_Year(this Value) Value {
return IntVal(toDate(this).Year())
}
var _ = method(date_Month, "()")
func date_Month(this Value) Value {
return IntVal(toDate(this).Month())
}
var _ = method(date_Day, "()")
func date_Day(this Value) Value {
return IntVal(toDate(this).Day())
}
var _ = method(date_Hour, "()")
func date_Hour(this Value) Value {
return IntVal(toDate(this).Hour())
}
var _ = method(date_Minute, "()")
func date_Minute(this Value) Value {
return IntVal(toDate(this).Minute())
}
var _ = method(date_Second, "()")
func date_Second(this Value) Value {
return IntVal(toDate(this).Second())
}
var _ = method(date_Millisecond, "()")
func date_Millisecond(this Value) Value {
return IntVal(toDate(this).Millisecond())
}
func dayOfWeek(x Value) int {
if i, ok := x.IfInt(); ok {
return i
}
s := str.ToLower(AsStr(x))
days := []string{"sunday", "monday", "tuesday",
"wednesday", "thursday", "friday", "saturday"}
for i, d := range days {
if strings.HasPrefix(d, s) {
return i
}
}
panic("usage: date.WeekDay(day name or number)")
}
var _ = builtin(UnixTime, "()")
func UnixTime() Value {
return IntVal(int(time.Now().Unix()))
}