From b5fefc9ab31f5f76438b2846b185969d7bb15cd8 Mon Sep 17 00:00:00 2001 From: Hadrien Tulipe Date: Tue, 4 Feb 2014 22:36:42 +0100 Subject: [PATCH] Supprt parsing of kml:datetime (#1585) This commit modifies the previous regex used to match following datetimes: - 2014 - 2014-02 - 2014-02-15 The new regex also matches dates that were matched by the previous regex. This commit also include a unit test for kml:dateTime parsing. --- src/ol/format/kmlformat.js | 22 +++++++++--------- test/spec/ol/format/kmlformat.test.js | 33 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index aaa05103cf..fa0ca3ba7d 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -1102,21 +1102,21 @@ ol.format.KML.whenParser_ = function(node, objectStack) { var whens = gxTrackObject.whens; var s = ol.xml.getAllTextContent(node, false); var re = - /^\s*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|(?:([+\-])(\d{2})(?::(\d{2}))?))\s*$/; + /^\s*(\d{4})($|-(\d{2})($|-(\d{2})($|T(\d{2}):(\d{2}):(\d{2})(Z|(?:([+\-])(\d{2})(?::(\d{2}))?)))))\s*$/; var m = re.exec(s); if (m) { var year = parseInt(m[1], 10); - var month = parseInt(m[2], 10) - 1; - var day = parseInt(m[3], 10); - var hour = parseInt(m[4], 10); - var minute = parseInt(m[5], 10); - var second = parseInt(m[6], 10); + var month = goog.isDef(m[3]) ? parseInt(m[3], 10) - 1 : 0; + var day = goog.isDef(m[5]) ? parseInt(m[5], 10) : 1; + var hour = goog.isDef(m[7]) ? parseInt(m[7], 10) : 0; + var minute = goog.isDef(m[8]) ? parseInt(m[8], 10) : 0; + var second = goog.isDef(m[9]) ? parseInt(m[9], 10) : 0; var when = Date.UTC(year, month, day, hour, minute, second); - if (m[7] != 'Z') { - var sign = m[8] == '-' ? -1 : 1; - when += sign * 60 * parseInt(m[9], 10); - if (goog.isDef(m[10])) { - when += sign * 60 * 60 * parseInt(m[10], 10); + if (goog.isDef(m[10]) && m[10] != 'Z') { + var sign = m[11] == '-' ? -1 : 1; + when += sign * 60 * parseInt(m[12], 10); + if (goog.isDef(m[13])) { + when += sign * 60 * 60 * parseInt(m[13], 10); } } whens.push(when); diff --git a/test/spec/ol/format/kmlformat.test.js b/test/spec/ol/format/kmlformat.test.js index 1dd15895e6..67f7e7c92c 100644 --- a/test/spec/ol/format/kmlformat.test.js +++ b/test/spec/ol/format/kmlformat.test.js @@ -373,6 +373,39 @@ describe('ol.format.KML', function() { expect(gs).to.have.length(2); expect(gs[0]).to.be.an(ol.geom.LineString); }); + + it('can read dateTime', function() { + var text = + '' + + ' ' + + ' ' + + ' 2014' + + ' 2014-02' + + ' 2014-02-06' + + ' 2014-02-06T19:39:03Z' + + ' 2014-02-06T19:39:10+03:00' + + ' 8.1 46.1 1909.9' + + ' 8.2 46.2 1925.2' + + ' 8.3 46.3 1926.2' + + ' 8.4 46.4 1927.2' + + ' 8.5 46.5 1928.2' + + ' ' + + ' ' + + ''; + var fs = format.readFeatures(text); + var f = fs[0]; + var g = f.getGeometry(); + var flatCoordinates = g.flatCoordinates; + expect(flatCoordinates[3]).to.be.eql(Date.UTC(2014, 0, 1, 0, 0, 0)); + expect(flatCoordinates[7]).to.be.eql(Date.UTC(2014, 1, 1, 0, 0, 0)); + expect(flatCoordinates[11]).to.be.eql(Date.UTC(2014, 1, 6, 0, 0, 0)); + expect(flatCoordinates[15]).to.be.eql(Date.UTC(2014, 1, 6, 19, 39, 3)); + expect(flatCoordinates[19]).to.be.eql( + Date.UTC(2014, 1, 6, 19, 39, 10) + 3 * 60 + ); + }); + }); describe('attributes', function() {