Move date parsing to a helper.
This commit is contained in:
parent
8df0bb85fe
commit
7b47edff14
5 changed files with 71 additions and 35 deletions
35
public/javascripts/app/helpers/date_formatter.js
Normal file
35
public/javascripts/app/helpers/date_formatter.js
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
(function(){
|
||||||
|
var dateFormatter = function dateFormatter() {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
dateFormatter.parse = function(date_string) {
|
||||||
|
var timestamp = new Date(date_string).getTime();
|
||||||
|
|
||||||
|
if (isNaN(timestamp)) {
|
||||||
|
timestamp = dateFormatter.parseISO8601UTC(date_string);
|
||||||
|
}
|
||||||
|
|
||||||
|
return timestamp;
|
||||||
|
},
|
||||||
|
|
||||||
|
dateFormatter.parseISO8601UTC = function(date_string) {
|
||||||
|
var iso8601_utc_pattern = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(.(\d{3}))?Z$/;
|
||||||
|
var time_components = date_string.match(iso8601_utc_pattern);
|
||||||
|
var timestamp = 0;
|
||||||
|
|
||||||
|
if (time_components != null) {
|
||||||
|
if (time_components[8] == undefined) {
|
||||||
|
time_components[8] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
timestamp = Date.UTC(time_components[1], time_components[2] - 1, time_components[3],
|
||||||
|
time_components[4], time_components[5], time_components[6],
|
||||||
|
time_components[8]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return timestamp;
|
||||||
|
},
|
||||||
|
|
||||||
|
app.helpers.dateFormatter = dateFormatter;
|
||||||
|
})();
|
||||||
|
|
@ -8,7 +8,7 @@ app.models.Photo = Backbone.Model.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
timeOf: function(field) {
|
timeOf: function(field) {
|
||||||
return new Date(this.get(field)) /1000;
|
return app.helpers.dateFormatter.parse(this.get(field)) / 1000;
|
||||||
},
|
},
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -21,31 +21,7 @@ app.models.Post = Backbone.Model.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
timeOf: function(field) {
|
timeOf: function(field) {
|
||||||
var timestamp = new Date(this.get(field)) /1000;
|
return app.helpers.dateFormatter.parse(this.get(field)) / 1000;
|
||||||
|
|
||||||
if (isNaN(timestamp)) {
|
|
||||||
timestamp = this.legacyTimeOf(field);
|
|
||||||
}
|
|
||||||
|
|
||||||
return timestamp;
|
|
||||||
},
|
|
||||||
|
|
||||||
legacyTimeOf: function(field) {
|
|
||||||
var iso8601_utc_pattern = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(.(\d{3}))?Z$/;
|
|
||||||
var time_components = this.get(field).match(iso8601_utc_pattern);
|
|
||||||
var timestamp = 0;
|
|
||||||
|
|
||||||
if (time_components != null) {
|
|
||||||
if (time_components[8] == undefined) {
|
|
||||||
time_components[8] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
timestamp = Date.UTC(time_components[1], time_components[2] - 1, time_components[3],
|
|
||||||
time_components[4], time_components[5], time_components[6],
|
|
||||||
time_components[8]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return timestamp /1000;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
createReshareUrl : "/reshares",
|
createReshareUrl : "/reshares",
|
||||||
|
|
|
||||||
33
spec/javascripts/app/helpers/date_formatter_spec.js
Normal file
33
spec/javascripts/app/helpers/date_formatter_spec.js
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
describe("app.helpers.dateFormatter", function(){
|
||||||
|
|
||||||
|
beforeEach(function(){
|
||||||
|
this.statusMessage = factory.post();
|
||||||
|
this.formatter = app.helpers.dateFormatter;
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("parse", function(){
|
||||||
|
context("modern web browsers", function(){
|
||||||
|
it ("supports ISO 8601 UTC dates", function(){
|
||||||
|
var timestamp = new Date(this.statusMessage.get("created_at")).getTime();
|
||||||
|
expect(this.formatter.parse(this.statusMessage.get("created_at"))).toEqual(timestamp);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
context("legacy web browsers", function(){
|
||||||
|
it("supports ISO 8601 UTC dates", function(){
|
||||||
|
var timestamp = new Date(this.statusMessage.get("created_at")).getTime();
|
||||||
|
|
||||||
|
expect(this.formatter.parseISO8601UTC(this.statusMessage.get("created_at"))).toEqual(timestamp);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
context("status messages", function(){
|
||||||
|
it("uses ISO 8601 UTC dates", function(){
|
||||||
|
var iso8601_utc_pattern = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(.(\d{3}))?Z$/;
|
||||||
|
|
||||||
|
expect(iso8601_utc_pattern.test(this.statusMessage.get("created_at"))).toBe(true);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
@ -119,13 +119,5 @@ describe("app.views.Post", function(){
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
context("legacy browsers", function(){
|
|
||||||
it("supports iso8501 utc timestamps", function(){
|
|
||||||
var timestamp = new Date(this.statusMessage.get("created_at")) /1000;
|
|
||||||
|
|
||||||
expect(this.statusMessage.legacyTimeOf("created_at")).toEqual(timestamp);
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue