Skip to content

Commit

Permalink
[#2375] Fix inconsistency with Firefox date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Aug 9, 2012
1 parent f49437c commit d47a278
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
30 changes: 28 additions & 2 deletions ckan/public/base/javascript/client.js
Expand Up @@ -240,8 +240,10 @@
* Returns an object of dataset keys.
*/
convertStorageMetadataToResource: function (meta) {
var modified = new Date(meta._last_modified);
var created = new Date(meta._creation_date);
// TODO: Check this is supported by IE7. U believe that the IE
// Date constructor chokes on hyphens and timezones.
var modified = new Date(this.normalizeTimestamp(meta._last_modified));
var created = new Date(this.normalizeTimestamp(meta._creation_date));

var createdISO = jQuery.date.toCKANString(created);
var modifiedISO = jQuery.date.toCKANString(modified);
Expand Down Expand Up @@ -271,6 +273,30 @@
cache_url: meta._location,
cache_url_updated: modifiedISO
};
},

/* Adds a timezone to the provided timestamp if one is not present. This
* fixes an inconsistency between Webkit and Firefox where Firefox parses
* the date in the current users timezone but Webkit uses UTC.
*
* string - A timestamp string.
*
* Examples
*
* client.normalizeTimestamp("2012-07-17T14:35:35");
* // => "2012-07-17T14:35:35Z"
*
* client.normalizeTimestamp("2012-07-17T14:35:35+0100");
* // => "2012-07-17T14:35:35+0100"
*
* Returns a new timestamp with timezone.
*/
normalizeTimestamp: function (string) {
var tz = /[+\-]\d{4}|Z/;
if (!tz.test(string)) {
string += 'Z';
}
return string;
}
});

Expand Down
18 changes: 18 additions & 0 deletions ckan/public/base/test/spec/client.spec.js
Expand Up @@ -354,4 +354,22 @@ describe('ckan.Client()', function () {
assert.equal(target.mimetype, 'image/jpeg', 'mimetype');
});
});

describe('.normalizeTimestamp(timestamp)', function () {
it('should add a timezone to a timestamp without one', function () {
var target = this.client.normalizeTimestamp("2012-07-17T14:35:35");
assert.equal(target, "2012-07-17T14:35:35Z");
});

it('should not add a timezone to a timestamp with one already', function () {
var target = this.client.normalizeTimestamp("2012-07-17T14:35:35Z");
assert.equal(target, "2012-07-17T14:35:35Z", 'timestamp with Z');

target = this.client.normalizeTimestamp("2012-07-17T14:35:35+0100");
assert.equal(target, "2012-07-17T14:35:35+0100", 'timestamp with +0100');

target = this.client.normalizeTimestamp("2012-07-17T14:35:35-0400");
assert.equal(target, "2012-07-17T14:35:35-0400", 'timestamp with -0400');
});
});
});

0 comments on commit d47a278

Please sign in to comment.