diff --git a/public/javascripts/app/models/post.js b/public/javascripts/app/models/post.js index c7ad731c6..1a27b5a8a 100644 --- a/public/javascripts/app/models/post.js +++ b/public/javascripts/app/models/post.js @@ -21,7 +21,31 @@ app.models.Post = Backbone.Model.extend({ }, timeOf: function(field) { - return new Date(this.get(field)) /1000; + var timestamp = new Date(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", diff --git a/spec/javascripts/app/views/post_view_spec.js b/spec/javascripts/app/views/post_view_spec.js index 9eb66d9a2..17080155a 100644 --- a/spec/javascripts/app/views/post_view_spec.js +++ b/spec/javascripts/app/views/post_view_spec.js @@ -119,5 +119,13 @@ 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); + }) + }) + }) });