[#152,view/timeline][s]: more robust data parsing with tests - fixes #152.

* Also discover and fix "bug" re momentjs timezone calculation and toDate.
This commit is contained in:
Rufus Pollock
2012-06-10 12:35:39 +01:00
parent 3cdd1c278b
commit 75a6bbd80a
2 changed files with 40 additions and 4 deletions

View File

@@ -79,12 +79,11 @@ my.Timeline = Backbone.View.extend({
}
};
this.model.currentRecords.each(function(doc) {
var start = doc.get(self.state.get('startField'));
var start = self._parseDate(doc.get(self.state.get('startField')));
var end = self._parseDate(doc.get(self.state.get('endField')));
if (start) {
var end = doc.get(self.state.get('endField'));
end = end ? moment(end).toDate() : null;
var tlEntry = {
"startDate": moment(start).toDate(),
"startDate": start,
"endDate": end,
"headline": String(doc.get('title') || ''),
"text": doc.summary()
@@ -103,6 +102,23 @@ my.Timeline = Backbone.View.extend({
return out;
},
_parseDate: function(date) {
var out = date.trim();
out = out.replace(/(\d)th/g, '$1');
out = out.replace(/(\d)st/g, '$1');
out = out.trim() ? moment(out) : null;
if (out.toDate() == 'Invalid Date') {
return null;
} else {
// fix for moment weirdness around date parsing and time zones
// moment('1914-08-01').toDate() => 1914-08-01 00:00 +01:00
// which in iso format (with 0 time offset) is 31 July 1914 23:00
// meanwhile native new Date('1914-08-01') => 1914-08-01 01:00 +01:00
out = out.subtract('minutes', out.zone());
return out.toDate();
}
},
_setupTemporalField: function() {
this.state.set({
startField: this._checkField(this.startFieldNames),