diff --git a/bower.json b/bower.json index 30a83068..fa01af90 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "moment-timezone", "description" : "Parse and display moments in any timezone.", - "version": "0.1.0", + "version": "0.2.0", "main": "builds/moment-timezone-with-data-2010-2020.js", "dependencies" : { "moment" : ">= 2.6.0" diff --git a/builds/moment-timezone-with-data-2010-2020.js b/builds/moment-timezone-with-data-2010-2020.js index acc7ab29..438ec386 100644 --- a/builds/moment-timezone-with-data-2010-2020.js +++ b/builds/moment-timezone-with-data-2010-2020.js @@ -1,5 +1,5 @@ //! moment-timezone.js -//! version : 0.1.0 +//! version : 0.2.0 //! author : Tim Wood //! license : MIT //! github.com/moment/moment-timezone @@ -21,7 +21,7 @@ // Do not load moment-timezone a second time. if (moment.tz !== undefined) { return moment; } - var VERSION = "0.1.0", + var VERSION = "0.2.0", zones = {}, links = {}; @@ -119,14 +119,19 @@ ************************************/ function Zone (packedString) { - var unpacked = unpack(packedString); - this.name = unpacked.name; - this.abbrs = unpacked.abbrs; - this.untils = unpacked.untils; - this.offsets = unpacked.offsets; + if (packedString) { + this._set(unpack(packedString)); + } } Zone.prototype = { + _set : function (unpacked) { + this.name = unpacked.name; + this.abbrs = unpacked.abbrs; + this.untils = unpacked.untils; + this.offsets = unpacked.offsets; + }, + _index : function (timestamp) { var target = +timestamp, untils = this.untils, @@ -143,13 +148,26 @@ var target = +timestamp, offsets = this.offsets, untils = this.untils, - i; + max = untils.length - 1, + offset, offsetNext, offsetPrev, i; + + for (i = 0; i < max; i++) { + offset = offsets[i]; + offsetNext = offsets[i + 1]; + offsetPrev = offsets[i ? i - 1 : i]; + + if (offset < offsetNext && tz.moveAmbiguousForward) { + offset = offsetNext; + } else if (offset > offsetPrev && tz.moveInvalidForward) { + offset = offsetPrev; + } - for (i = 0; i < untils.length; i++) { - if (target < untils[i] - (offsets[i] * 60000)) { + if (target < untils[i] - (offset * 60000)) { return offsets[i]; } } + + return offsets[max]; }, abbr : function (mom) { @@ -170,7 +188,7 @@ } function addZone (packed) { - var i, zone; + var i, zone, zoneName; if (typeof packed === "string") { packed = [packed]; @@ -178,19 +196,14 @@ for (i = 0; i < packed.length; i++) { zone = new Zone(packed[i]); - zones[normalizeName(zone.name)] = zone; + zoneName = normalizeName(zone.name); + zones[zoneName] = zone; + upgradeLinksToZones(zoneName); } } function getZone (name) { - name = normalizeName(name); - var linkName = links[name]; - - if (linkName && zones[linkName]) { - name = linkName; - } - - return zones[name] || null; + return zones[normalizeName(name)] || null; } function getNames () { @@ -213,9 +226,42 @@ } for (i = 0; i < aliases.length; i++) { - alias = normalizeName(aliases[i]).split('|'); - links[alias[0]] = alias[1]; - links[alias[1]] = alias[0]; + alias = aliases[i].split('|'); + pushLink(alias[0], alias[1]); + pushLink(alias[1], alias[0]); + } + } + + function upgradeLinksToZones (zoneName) { + if (!links[zoneName]) { + return; + } + + var i, + zone = zones[zoneName], + linkNames = links[zoneName]; + + for (i = 0; i < linkNames.length; i++) { + copyZoneWithName(zone, linkNames[i]); + } + + links[zoneName] = null; + } + + function copyZoneWithName (zone, name) { + var linkZone = zones[normalizeName(name)] = new Zone(); + linkZone._set(zone); + linkZone.name = name; + } + + function pushLink (zoneName, linkName) { + zoneName = normalizeName(zoneName); + + if (zones[zoneName]) { + copyZoneWithName(zones[zoneName], linkName); + } else { + links[zoneName] = links[zoneName] || []; + links[zoneName].push(linkName); } } @@ -239,6 +285,12 @@ return !!(m._a && (m._tzm === undefined)); } + function logError (message) { + if (typeof console !== 'undefined' && typeof console.error === 'function') { + console.error(message); + } + } + /************************************ moment.tz namespace ************************************/ @@ -272,6 +324,8 @@ tz.unpack = unpack; tz.unpackBase60 = unpackBase60; tz.needsOffset = needsOffset; + tz.moveInvalidForward = true; + tz.moveAmbiguousForward = false; /************************************ Interface with Moment.js @@ -297,6 +351,8 @@ this._z = getZone(name); if (this._z) { moment.updateOffset(this); + } else { + logError("Moment Timezone has no data for " + name + ". See http://momentjs.com/timezone/docs/#/data-loading/."); } return this; } diff --git a/builds/moment-timezone-with-data-2010-2020.min.js b/builds/moment-timezone-with-data-2010-2020.min.js index f055d9de..0cd47600 100644 --- a/builds/moment-timezone-with-data-2010-2020.min.js +++ b/builds/moment-timezone-with-data-2010-2020.min.js @@ -1,6 +1,6 @@ //! moment-timezone.js -//! version : 0.1.0 +//! version : 0.2.0 //! author : Tim Wood //! license : MIT //! github.com/moment/moment-timezone -!function(a,b){"use strict";"function"==typeof define&&define.amd?define(["moment"],b):"object"==typeof exports?module.exports=b(require("moment")):b(a.moment)}(this,function(a){"use strict";function b(a){return a>96?a-87:a>64?a-29:a-48}function c(a){var c,d=0,e=a.split("."),f=e[0],g=e[1]||"",h=1,i=0,j=1;for(45===a.charCodeAt(0)&&(d=1,j=-1),d;dc;c++)a[c]=Math.round((a[c-1]||0)+6e4*a[c]);a[b-1]=1/0}function f(a,b){var c,d=[];for(c=0;c96?a-87:a>64?a-29:a-48}function c(a){var c,d=0,e=a.split("."),f=e[0],g=e[1]||"",h=1,i=0,j=1;for(45===a.charCodeAt(0)&&(d=1,j=-1),d;dc;c++)a[c]=Math.round((a[c-1]||0)+6e4*a[c]);a[b-1]=1/0}function f(a,b){var c,d=[];for(c=0;ce;e++)if(b=g[e],c=g[e+1],d=g[e?e-1:e],c>b&&u.moveAmbiguousForward?b=c:b>d&&u.moveInvalidForward&&(b=d),f offsetPrev && tz.moveInvalidForward) { + offset = offsetPrev; + } - for (i = 0; i < untils.length; i++) { - if (target < untils[i] - (offsets[i] * 60000)) { + if (target < untils[i] - (offset * 60000)) { return offsets[i]; } } + + return offsets[max]; }, abbr : function (mom) { @@ -170,7 +188,7 @@ } function addZone (packed) { - var i, zone; + var i, zone, zoneName; if (typeof packed === "string") { packed = [packed]; @@ -178,19 +196,14 @@ for (i = 0; i < packed.length; i++) { zone = new Zone(packed[i]); - zones[normalizeName(zone.name)] = zone; + zoneName = normalizeName(zone.name); + zones[zoneName] = zone; + upgradeLinksToZones(zoneName); } } function getZone (name) { - name = normalizeName(name); - var linkName = links[name]; - - if (linkName && zones[linkName]) { - name = linkName; - } - - return zones[name] || null; + return zones[normalizeName(name)] || null; } function getNames () { @@ -213,9 +226,42 @@ } for (i = 0; i < aliases.length; i++) { - alias = normalizeName(aliases[i]).split('|'); - links[alias[0]] = alias[1]; - links[alias[1]] = alias[0]; + alias = aliases[i].split('|'); + pushLink(alias[0], alias[1]); + pushLink(alias[1], alias[0]); + } + } + + function upgradeLinksToZones (zoneName) { + if (!links[zoneName]) { + return; + } + + var i, + zone = zones[zoneName], + linkNames = links[zoneName]; + + for (i = 0; i < linkNames.length; i++) { + copyZoneWithName(zone, linkNames[i]); + } + + links[zoneName] = null; + } + + function copyZoneWithName (zone, name) { + var linkZone = zones[normalizeName(name)] = new Zone(); + linkZone._set(zone); + linkZone.name = name; + } + + function pushLink (zoneName, linkName) { + zoneName = normalizeName(zoneName); + + if (zones[zoneName]) { + copyZoneWithName(zones[zoneName], linkName); + } else { + links[zoneName] = links[zoneName] || []; + links[zoneName].push(linkName); } } @@ -239,6 +285,12 @@ return !!(m._a && (m._tzm === undefined)); } + function logError (message) { + if (typeof console !== 'undefined' && typeof console.error === 'function') { + console.error(message); + } + } + /************************************ moment.tz namespace ************************************/ @@ -272,6 +324,8 @@ tz.unpack = unpack; tz.unpackBase60 = unpackBase60; tz.needsOffset = needsOffset; + tz.moveInvalidForward = true; + tz.moveAmbiguousForward = false; /************************************ Interface with Moment.js @@ -297,6 +351,8 @@ this._z = getZone(name); if (this._z) { moment.updateOffset(this); + } else { + logError("Moment Timezone has no data for " + name + ". See http://momentjs.com/timezone/docs/#/data-loading/."); } return this; } diff --git a/builds/moment-timezone-with-data.min.js b/builds/moment-timezone-with-data.min.js index 8cf130eb..c42dabc8 100644 --- a/builds/moment-timezone-with-data.min.js +++ b/builds/moment-timezone-with-data.min.js @@ -1,7 +1,7 @@ //! moment-timezone.js -//! version : 0.1.0 +//! version : 0.2.0 //! author : Tim Wood //! license : MIT //! github.com/moment/moment-timezone -!function(a,b){"use strict";"function"==typeof define&&define.amd?define(["moment"],b):"object"==typeof exports?module.exports=b(require("moment")):b(a.moment)}(this,function(a){"use strict";function b(a){return a>96?a-87:a>64?a-29:a-48}function c(a){var c,d=0,e=a.split("."),f=e[0],g=e[1]||"",h=1,i=0,j=1;for(45===a.charCodeAt(0)&&(d=1,j=-1),d;dc;c++)a[c]=Math.round((a[c-1]||0)+6e4*a[c]);a[b-1]=1/0}function f(a,b){var c,d=[];for(c=0;c96?a-87:a>64?a-29:a-48}function c(a){var c,d=0,e=a.split("."),f=e[0],g=e[1]||"",h=1,i=0,j=1;for(45===a.charCodeAt(0)&&(d=1,j=-1),d;dc;c++)a[c]=Math.round((a[c-1]||0)+6e4*a[c]);a[b-1]=1/0}function f(a,b){var c,d=[];for(c=0;ce;e++)if(b=g[e],c=g[e+1],d=g[e?e-1:e],c>b&&u.moveAmbiguousForward?b=c:b>d&&u.moveInvalidForward&&(b=d),f96?a-87:a>64?a-29:a-48}function c(a){var c,d=0,e=a.split("."),f=e[0],g=e[1]||"",h=1,i=0,j=1;for(45===a.charCodeAt(0)&&(d=1,j=-1),d;dc;c++)a[c]=Math.round((a[c-1]||0)+6e4*a[c]);a[b-1]=1/0}function f(a,b){var c,d=[];for(c=0;c96?a-87:a>64?a-29:a-48}function c(a){var c,d=0,e=a.split("."),f=e[0],g=e[1]||"",h=1,i=0,j=1;for(45===a.charCodeAt(0)&&(d=1,j=-1),d;dc;c++)a[c]=Math.round((a[c-1]||0)+6e4*a[c]);a[b-1]=1/0}function f(a,b){var c,d=[];for(c=0;ce;e++)if(b=g[e],c=g[e+1],d=g[e?e-1:e],c>b&&u.moveAmbiguousForward?b=c:b>d&&u.moveInvalidForward&&(b=d),f=2.6.0` as a dependency in `bower.json`. [#107](https://github.com/moment/moment-timezone/issues/107) +* Fixed getting the name of a zone that was added as a linked zone. [#104](https://github.com/moment/moment-timezone/pull/104) +* Added an error message when a zone was not loaded. [#106](https://github.com/moment/moment-timezone/issues/106) + ### `0.1.0` _2014-06-23_ * *Breaking:* Changed data format from Zones+Rules to just Zones. [#82](https://github.com/moment/moment-timezone/pull/82) * *Breaking:* Removed `moment.tz.{addRule,addZone,zoneExists,zones}` as they are no longer relevant with the new data format. diff --git a/moment-timezone-utils.js b/moment-timezone-utils.js index ecd542f0..93c20b49 100644 --- a/moment-timezone-utils.js +++ b/moment-timezone-utils.js @@ -1,5 +1,5 @@ //! moment-timezone-utils.js -//! version : 0.1.0 +//! version : 0.2.0 //! author : Tim Wood //! license : MIT //! github.com/moment/moment-timezone diff --git a/moment-timezone.js b/moment-timezone.js index e40ec689..82dd7e2e 100644 --- a/moment-timezone.js +++ b/moment-timezone.js @@ -1,5 +1,5 @@ //! moment-timezone.js -//! version : 0.1.0 +//! version : 0.2.0 //! author : Tim Wood //! license : MIT //! github.com/moment/moment-timezone @@ -21,7 +21,7 @@ // Do not load moment-timezone a second time. if (moment.tz !== undefined) { return moment; } - var VERSION = "0.1.0", + var VERSION = "0.2.0", zones = {}, links = {}; diff --git a/package.json b/package.json index f98211bb..1189bf03 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "moment-timezone", - "version": "0.1.0", + "version": "0.2.0", "description": "Parse and display moments in any timezone.", "homepage": "http://momentjs.com/timezone/", "author": "Tim Wood (http://timwoodcreates.com/)",