Skip to content

Commit fa85f58

Browse files
Merge pull request #465 from xianjianlf2/fix/date-tostring-parse-148
fix(parse): support Date.toString() format
2 parents 4b8f030 + d290be7 commit fa85f58

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

src/input/formats/02-mdy.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,26 @@ export default [
8888
s = parseTime(s, time)
8989
return s
9090
}
91+
},
92+
// Date.toString() - 'Mon Jun 17 2019 11:00:00 GMT-0700 (Pacific Daylight Time)'
93+
// (the weekday is already stripped by normalize)
94+
{
95+
reg: /^([a-z]+) ([0-9]{1,2}) ([0-9]{4}) ([0-9]{1,2}:[0-9]{2}:?[0-9]{0,2}) (?:gmt)?([+-][0-9]{4})/i,
96+
parse: (s, arr) => {
97+
const [, month, date, year, time, tz] = arr
98+
const obj = {
99+
year: parseYear(year, s._today),
100+
month: parseMonth(month),
101+
date: toCardinal(date || '')
102+
}
103+
if (validate(obj) === false) {
104+
s.epoch = null
105+
return s
106+
}
107+
walkTo(s, obj)
108+
s = parseOffset(s, tz)
109+
s = parseTime(s, time)
110+
return s
111+
}
91112
}
92113
]

test/tostring-parse.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import test from 'tape'
2+
import spacetime from './lib/index.js'
3+
4+
// support the format produced by javascript's Date.toString()
5+
// eg. "Mon Jun 17 2019 11:00:00 GMT-0700 (Pacific Daylight Time)"
6+
test('Date.toString() format', (t) => {
7+
const arr = [
8+
['Mon Jun 17 2019 11:00:00 GMT-0700 (Pacific Daylight Time)', '2019-06-17T11:00:00-07:00'],
9+
['Mon Jun 17 2019 11:00:00 GMT+0530 (India Standard Time)', '2019-06-17T11:00:00+05:30'],
10+
['Sat Feb 01 2020 00:00:00 GMT+0000 (Coordinated Universal Time)', '2020-02-01T00:00:00Z'],
11+
// bare numeric offset, no GMT prefix, no tz-name
12+
['Mon Jun 17 2019 11:00:00 -0700', '2019-06-17T11:00:00-07:00']
13+
]
14+
arr.forEach((a) => {
15+
const left = spacetime(a[0])
16+
const right = spacetime(a[1])
17+
t.equal(left.isValid(), true, 'is-valid: ' + a[0])
18+
t.equal(left.epoch, right.epoch, a[0])
19+
})
20+
t.end()
21+
})

0 commit comments

Comments
 (0)