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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
package otto
import (
"fmt"
"math"
"regexp"
Time "time"
)
type _dateObject struct {
time Time.Time // Time from the "time" package, a cached version of time
epoch int64
value Value
isNaN bool
}
var (
invalidDateObject = _dateObject{
time: Time.Time{},
epoch: -1,
value: NaNValue(),
isNaN: true,
}
)
type _ecmaTime struct {
year int
month int
day int
hour int
minute int
second int
millisecond int
location *Time.Location // Basically, either local or UTC
}
func ecmaTime(goTime Time.Time) _ecmaTime {
return _ecmaTime{
goTime.Year(),
dateFromGoMonth(goTime.Month()),
goTime.Day(),
goTime.Hour(),
goTime.Minute(),
goTime.Second(),
goTime.Nanosecond() / (100 * 100 * 100),
goTime.Location(),
}
}
func (self *_ecmaTime) goTime() Time.Time {
return Time.Date(
self.year,
dateToGoMonth(self.month),
self.day,
self.hour,
self.minute,
self.second,
self.millisecond*(100*100*100),
self.location,
)
}
func (self *_dateObject) Time() Time.Time {
return self.time
}
func (self *_dateObject) Epoch() int64 {
return self.epoch
}
func (self *_dateObject) Value() Value {
return self.value
}
// FIXME A date should only be in the range of -100,000,000 to +100,000,000 (1970): 15.9.1.1
func (self *_dateObject) SetNaN() {
self.time = Time.Time{}
self.epoch = -1
self.value = NaNValue()
self.isNaN = true
}
func (self *_dateObject) SetTime(time Time.Time) {
self.Set(timeToEpoch(time))
}
func epoch2dateObject(epoch float64) _dateObject {
date := _dateObject{}
date.Set(epoch)
return date
}
func (self *_dateObject) Set(epoch float64) {
// epoch
self.epoch = epochToInteger(epoch)
// time
time, err := epochToTime(epoch)
self.time = time // Is either a valid time, or the zero-value for time.Time
// value & isNaN
if err != nil {
self.isNaN = true
self.epoch = -1
self.value = NaNValue()
} else {
self.value = toValue_int64(self.epoch)
}
}
func epochToInteger(value float64) int64 {
if value > 0 {
return int64(math.Floor(value))
}
return int64(math.Ceil(value))
}
func epochToTime(value float64) (time Time.Time, err error) {
epochWithMilli := value
if math.IsNaN(epochWithMilli) || math.IsInf(epochWithMilli, 0) {
err = fmt.Errorf("Invalid time %v", value)
return
}
epoch := int64(epochWithMilli / 1000)
milli := int64(epochWithMilli) % 1000
time = Time.Unix(int64(epoch), milli*1000000).UTC()
return
}
func timeToEpoch(time Time.Time) float64 {
return float64(time.UnixNano() / (1000 * 1000))
}
func (runtime *_runtime) newDateObject(epoch float64) *_object {
self := runtime.newObject()
self.class = "Date"
// FIXME This is ugly...
date := _dateObject{}
date.Set(epoch)
self.value = date
return self
}
func (self *_object) dateValue() _dateObject {
value, _ := self.value.(_dateObject)
return value
}
func dateObjectOf(rt *_runtime, _dateObject *_object) _dateObject {
if _dateObject == nil || _dateObject.class != "Date" {
panic(rt.panicTypeError())
}
return _dateObject.dateValue()
}
// JavaScript is 0-based, Go is 1-based (15.9.1.4)
func dateToGoMonth(month int) Time.Month {
return Time.Month(month + 1)
}
func dateFromGoMonth(month Time.Month) int {
return int(month) - 1
}
// Both JavaScript & Go are 0-based (Sunday == 0)
func dateToGoDay(day int) Time.Weekday {
return Time.Weekday(day)
}
func dateFromGoDay(day Time.Weekday) int {
return int(day)
}
func newDateTime(argumentList []Value, location *Time.Location) (epoch float64) {
pick := func(index int, default_ float64) (float64, bool) {
if index >= len(argumentList) {
return default_, false
}
value := argumentList[index].float64()
if math.IsNaN(value) || math.IsInf(value, 0) {
return 0, true
}
return value, false
}
if len(argumentList) >= 2 { // 2-argument, 3-argument, ...
var year, month, day, hour, minute, second, millisecond float64
var invalid bool
if year, invalid = pick(0, 1900.0); invalid {
goto INVALID
}
if month, invalid = pick(1, 0.0); invalid {
goto INVALID
}
if day, invalid = pick(2, 1.0); invalid {
goto INVALID
}
if hour, invalid = pick(3, 0.0); invalid {
goto INVALID
}
if minute, invalid = pick(4, 0.0); invalid {
goto INVALID
}
if second, invalid = pick(5, 0.0); invalid {
goto INVALID
}
if millisecond, invalid = pick(6, 0.0); invalid {
goto INVALID
}
if year >= 0 && year <= 99 {
year += 1900
}
time := Time.Date(int(year), dateToGoMonth(int(month)), int(day), int(hour), int(minute), int(second), int(millisecond)*1000*1000, location)
return timeToEpoch(time)
} else if len(argumentList) == 0 { // 0-argument
time := Time.Now().UTC()
return timeToEpoch(time)
} else { // 1-argument
value := valueOfArrayIndex(argumentList, 0)
value = toPrimitive(value)
if value.IsString() {
return dateParse(value.string())
}
return value.float64()
}
INVALID:
epoch = math.NaN()
return
}
var (
dateLayoutList = []string{
"2006",
"2006-01",
"2006-01-02",
"2006T15:04",
"2006-01T15:04",
"2006-01-02T15:04",
"2006T15:04:05",
"2006-01T15:04:05",
"2006-01-02T15:04:05",
"2006T15:04:05.000",
"2006-01T15:04:05.000",
"2006-01-02T15:04:05.000",
"2006T15:04-0700",
"2006-01T15:04-0700",
"2006-01-02T15:04-0700",
"2006T15:04:05-0700",
"2006-01T15:04:05-0700",
"2006-01-02T15:04:05-0700",
"2006T15:04:05.000-0700",
"2006-01T15:04:05.000-0700",
"2006-01-02T15:04:05.000-0700",
Time.RFC1123,
}
matchDateTimeZone = regexp.MustCompile(`^(.*)(?:(Z)|([\+\-]\d{2}):(\d{2}))$`)
)
func dateParse(date string) (epoch float64) {
// YYYY-MM-DDTHH:mm:ss.sssZ
var time Time.Time
var err error
{
date := date
if match := matchDateTimeZone.FindStringSubmatch(date); match != nil {
if match[2] == "Z" {
date = match[1] + "+0000"
} else {
date = match[1] + match[3] + match[4]
}
}
for _, layout := range dateLayoutList {
time, err = Time.Parse(layout, date)
if err == nil {
break
}
}
}
if err != nil {
return math.NaN()
}
return float64(time.UnixNano()) / (1000 * 1000) // UnixMilli()
}
|