forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.go
140 lines (117 loc) · 2.58 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
package goja
import (
"math"
"time"
)
const (
dateTimeLayout = "Mon Jan 02 2006 15:04:05 GMT-0700 (MST)"
utcDateTimeLayout = "Mon, 02 Jan 2006 15:04:05 GMT"
isoDateTimeLayout = "2006-01-02T15:04:05.000Z"
dateLayout = "Mon Jan 02 2006"
timeLayout = "15:04:05 GMT-0700 (MST)"
datetimeLayout_en_GB = "01/02/2006, 15:04:05"
dateLayout_en_GB = "01/02/2006"
timeLayout_en_GB = "15:04:05"
maxTime = 8.64e15
timeUnset = math.MinInt64
)
type dateObject struct {
baseObject
msec int64
}
var (
dateLayoutList = []string{
"2006-01-02T15:04:05Z0700",
"2006-01-02T15:04:05",
"2006-01-02",
"2006-01-02 15:04:05",
time.RFC1123,
time.RFC1123Z,
dateTimeLayout,
time.UnixDate,
time.ANSIC,
time.RubyDate,
"Mon, 02 Jan 2006 15:04:05 GMT-0700 (MST)",
"Mon, 02 Jan 2006 15:04:05 -0700 (MST)",
"2006",
"2006-01",
"2006T15:04",
"2006-01T15:04",
"2006-01-02T15:04",
"2006T15:04:05",
"2006-01T15:04:05",
"2006T15:04Z0700",
"2006-01T15:04Z0700",
"2006-01-02T15:04Z0700",
"2006T15:04:05Z0700",
"2006-01T15:04:05Z0700",
}
)
func dateParse(date string) (time.Time, bool) {
var t time.Time
var err error
for _, layout := range dateLayoutList {
t, err = parseDate(layout, date, time.UTC)
if err == nil {
break
}
}
unix := timeToMsec(t)
return t, err == nil && unix >= -maxTime && unix <= maxTime
}
func (r *Runtime) newDateObject(t time.Time, isSet bool, proto *Object) *Object {
v := &Object{runtime: r}
d := &dateObject{}
v.self = d
d.val = v
d.class = classDate
d.prototype = proto
d.extensible = true
d.init()
if isSet {
d.msec = timeToMsec(t)
} else {
d.msec = timeUnset
}
return v
}
func dateFormat(t time.Time) string {
return t.Local().Format(dateTimeLayout)
}
func timeFromMsec(msec int64) time.Time {
sec := msec / 1000
nsec := (msec % 1000) * 1e6
return time.Unix(sec, nsec)
}
func timeToMsec(t time.Time) int64 {
return t.Unix()*1000 + int64(t.Nanosecond())/1e6
}
func (d *dateObject) toPrimitive() Value {
return d.toPrimitiveString()
}
func (d *dateObject) export(*objectExportCtx) interface{} {
if d.isSet() {
return d.time()
}
return nil
}
func (d *dateObject) setTimeMs(ms int64) Value {
if ms >= 0 && ms <= maxTime || ms < 0 && ms >= -maxTime {
d.msec = ms
return intToValue(ms)
}
d.unset()
return _NaN
}
func (d *dateObject) isSet() bool {
return d.msec != timeUnset
}
func (d *dateObject) unset() {
d.msec = timeUnset
}
func (d *dateObject) time() time.Time {
return timeFromMsec(d.msec)
}
func (d *dateObject) timeUTC() time.Time {
return timeFromMsec(d.msec).In(time.UTC)
}