Skip to content

Commit

Permalink
[BUGFIX beta] Ensure ISO-8601 regex correctly matches timestamps (#4771)
Browse files Browse the repository at this point in the history
* [BUGFIX beta] Ensure ISO-8601 regex correctly matches timestamps with a timezone offset

* Wrap Date prototype stubbing in try/finally to ensure it always reverts after tests complete

(cherry picked from commit 3506441)
  • Loading branch information
tomlagier authored and bmac committed Jan 26, 2017
1 parent 5d11fd3 commit 6e1ee33
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion addon/-private/ext/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const parseDate = function (date) {
// before falling back to any implementation-specific date parsing, so that’s what we do, even if native
// implementations could be faster
// 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?:(\d{2}))?)?)?$/.exec(date))) {
if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2}):?(?:(\d{2}))?)?)?$/.exec(date))) {
// avoid NaN timestamps caused by “undefined” values being passed to Date.UTC
for (var i = 0, k; (k = numericKeys[i]); ++i) {
struct[k] = +struct[k] || 0;
Expand Down
19 changes: 16 additions & 3 deletions tests/unit/transform/date-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,22 @@ test("#deserialize with different offset formats", function(assert) {
var dateStringColon = '2013-03-15T23:22:00.000+00:00';
var dateStringShortOffset = '2016-12-02T17:30:00.000+00';

assert.equal(transform.deserialize(dateString).getTime(), 1053817200000);
assert.equal(transform.deserialize(dateStringShortOffset).getTime(), 1480699800000);
assert.equal(transform.deserialize(dateStringColon).getTime(), 1363389720000);
assert.expect(6);

var _dateUTC = Date.UTC;

try {
Date.UTC = function () {
assert.equal(arguments.length, 7);
return _dateUTC.apply(this, [].slice.call(arguments));
};

assert.equal(transform.deserialize(dateString).getTime(), 1053817200000);
assert.equal(transform.deserialize(dateStringShortOffset).getTime(), 1480699800000);
assert.equal(transform.deserialize(dateStringColon).getTime(), 1363389720000);
} finally {
Date.UTC = _dateUTC;
}
});

testInDebug('Ember.Date.parse has been deprecated', function(assert) {
Expand Down

0 comments on commit 6e1ee33

Please sign in to comment.