timezone-js
Advanced tools
Comparing version 0.4.4 to 0.4.5
@@ -13,3 +13,3 @@ { | ||
"description": "JavaScript timezone library based on Olson timezone data", | ||
"version": "0.4.4", | ||
"version": "0.4.5", | ||
"main": "src/date.js", | ||
@@ -16,0 +16,0 @@ "homepage": "https://github.com/mde/timezone-js", |
@@ -54,2 +54,19 @@ # TimezoneJS.Date | ||
The `timezoneJS.Date` constructor is compatible to the normal JavaScript Date constructor, but additional allows to pass an optional `tz` (timezone). In the following cases the passed date/time is unambiguous: | ||
timezoneJS.Date(millis, [tz]) | ||
timezoneJS.Date(Date, [tz]) | ||
timezoneJS.Date(dt_str_tz, [tz]) | ||
`dt_str_tz` is a date string containing timezone information, i.e. containing 'Z', 'T' or a timezone offset matching the regular expression /[+-][0-9]{4}/ (e.g. '+0200'). The [one-stop shop for cross-browser JavaScript Date parsing behavior](http://dygraphs.com/date-formats.html) provides detailed information about JavaScript date formats. | ||
In the following cases the date is assumed to be a date in timezone `tz` or a locale date if `tz` is not provided: | ||
timezoneJS.Date(year, mon, day, [hour], [min], [second], [tz]) | ||
timezoneJS.Date(dt_str, [tz]) | ||
`dt_str_tz` is a date string containing no timezone information. | ||
### Examples | ||
Create a `timezoneJS.Date` the same way as a normal JavaScript Date, but append a timezone parameter on the end: | ||
@@ -56,0 +73,0 @@ |
@@ -10,2 +10,7 @@ var TestUtils = require('./test-utils') | ||
it('should have handled March correctly (not replacing h) when initialized', function () { | ||
var date = new timezoneJS.Date(2011, 2, 2, 11, 1, 1, 'Etc/UTC'); | ||
expect(date.toString('MMMM')).toEqual('March'); | ||
}); | ||
it('should format string correctly in toISOString', function () { | ||
@@ -156,2 +161,14 @@ var date = new timezoneJS.Date(); | ||
it('should take in String (other format) and Etc/UTC as constructor', function () { | ||
var dt = new timezoneJS.Date('2013/06/18 10:37', 'Etc/UTC'); | ||
expect(dt.toString('yyyy/MM/dd HH:mm', 'America/Los_Angeles')).toEqual('2013/06/18 03:37'); | ||
}); | ||
it('should take in String (other format) and Europe/Athens as constructor', function () { | ||
var dt = new timezoneJS.Date('2013/06/18 10:37', 'Europe/Athens'); | ||
expect(dt.toString('yyyy/MM/dd HH:mm', 'America/Los_Angeles')).toEqual('2013/06/18 00:37'); | ||
}); | ||
it('should take in String as constructor', function () { | ||
@@ -238,2 +255,265 @@ var dtA = new Date() | ||
}); | ||
// Years | ||
it("accepts an optional parameter for month in setUTCFullYear", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC"); | ||
expect(date.getUTCMonth()).toEqual(11); | ||
expect(date.getUTCDate()).toEqual(31); | ||
date.setUTCFullYear(2012, 1); | ||
//Febuary does not have a 31st, thus we should wrap to march | ||
expect(date.getUTCMonth()).toEqual(2); | ||
expect(date.getUTCDate()).toEqual(2); | ||
}); | ||
it("accepts a second optional parameter for date in setUTCFullYear", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC"); | ||
expect(date.getUTCMonth()).toEqual(11); | ||
expect(date.getUTCDate()).toEqual(31); | ||
date.setUTCFullYear(2012, 1, 1); | ||
//Do not wrap to March because we are setting the date as well | ||
expect(date.getUTCMonth()).toEqual(1); | ||
expect(date.getUTCDate()).toEqual(1); | ||
}); | ||
it("accepts an optional parameter for month in setFullYear", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York"); | ||
expect(date.getMonth()).toEqual(11); | ||
expect(date.getDate()).toEqual(31); | ||
date.setFullYear(2012, 1); | ||
//Febuary does not have a 31st, thus we should wrap to march | ||
expect(date.getMonth()).toEqual(2); | ||
expect(date.getDate()).toEqual(2); | ||
}); | ||
it("accepts a second optional parameter for date in setFullYear", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York"); | ||
expect(date.getMonth()).toEqual(11); | ||
expect(date.getDate()).toEqual(31); | ||
date.setFullYear(2012, 1, 1); | ||
//Do not wrap to March because we are setting the date as well | ||
expect(date.getMonth()).toEqual(1); | ||
expect(date.getDate()).toEqual(1); | ||
}); | ||
// Months | ||
it("accepts an optional parameter for date in setUTCMonths", function() { | ||
var date = new timezoneJS.Date(2000, 7, 14, 0, 0, 0, "Etc/UTC"); | ||
expect(date.getUTCMonth()).toEqual(7); | ||
expect(date.getUTCDate()).toEqual(14); | ||
date.setUTCMonth(1, 5); | ||
expect(date.getUTCMonth()).toEqual(1); | ||
expect(date.getUTCDate()).toEqual(5); | ||
date.setUTCMonth(0, 0); | ||
expect(date.getUTCMonth()).toEqual(11); | ||
expect(date.getUTCDate()).toEqual(31); | ||
}); | ||
it("accepts an optional parameter for date in setMonths", function() { | ||
var date = new timezoneJS.Date(2000, 7, 14, 0, 0, 0, "America/New_York"); | ||
expect(date.getMonth()).toEqual(7); | ||
expect(date.getDate()).toEqual(14); | ||
date.setMonth(1, 5); | ||
expect(date.getMonth()).toEqual(1); | ||
expect(date.getDate()).toEqual(5); | ||
date.setMonth(0, 0); | ||
expect(date.getMonth()).toEqual(11); | ||
expect(date.getDate()).toEqual(31); | ||
}); | ||
// Hours | ||
it("accepts an optional parameter for minutes in setUTCHours", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC"); | ||
expect(date.getUTCMinutes()).toEqual(0); | ||
date.setUTCHours(0, 1); | ||
expect(date.getUTCMinutes()).toEqual(1); | ||
date.setUTCHours(0, 0); | ||
expect(date.getUTCMinutes()).toEqual(0); | ||
}); | ||
it("accepts an optional parameter for seconds in setUTCHours", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC"); | ||
expect(date.getUTCSeconds()).toEqual(0); | ||
date.setUTCHours(0, 1, 2); | ||
expect(date.getUTCSeconds()).toEqual(2); | ||
date.setUTCHours(0, 0, 0); | ||
expect(date.getUTCSeconds()).toEqual(0); | ||
}); | ||
it("accepts an optional parameter for milliseconds in setUTCHours", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC"); | ||
expect(date.getUTCMilliseconds()).toEqual(0); | ||
date.setUTCHours(0, 1, 2, 3); | ||
expect(date.getUTCMilliseconds()).toEqual(3); | ||
date.setUTCHours(0, 0, 0, 0); | ||
expect(date.getUTCMilliseconds()).toEqual(0); | ||
}); | ||
it("accepts an optional parameter for minutes in setHours", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York"); | ||
expect(date.getMinutes()).toEqual(0); | ||
date.setHours(0, 1); | ||
expect(date.getMinutes()).toEqual(1); | ||
date.setHours(0, 0); | ||
expect(date.getMinutes()).toEqual(0); | ||
}); | ||
it("accepts an optional parameter for seconds in setHours", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York"); | ||
expect(date.getSeconds()).toEqual(0); | ||
date.setHours(0, 1, 2); | ||
expect(date.getSeconds()).toEqual(2); | ||
date.setHours(0, 0, 0); | ||
expect(date.getSeconds()).toEqual(0); | ||
}); | ||
it("accepts an optional parameter for milliseconds in setHours", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York"); | ||
expect(date.getMilliseconds()).toEqual(0); | ||
date.setHours(0, 1, 2, 3); | ||
expect(date.getMilliseconds()).toEqual(3); | ||
date.setHours(0, 0, 0, 0); | ||
expect(date.getMilliseconds()).toEqual(0); | ||
}); | ||
// Minutes | ||
it("accepts an optional parameter for seconds in setUTCMinutes", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC"); | ||
expect(date.getUTCSeconds()).toEqual(0); | ||
date.setUTCMinutes(0, 1); | ||
expect(date.getUTCSeconds()).toEqual(1); | ||
date.setUTCMinutes(0, 0); | ||
expect(date.getUTCSeconds()).toEqual(0); | ||
}); | ||
it("accepts an optional parameter for milliseconds in setUTCMinutes", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC"); | ||
expect(date.getUTCMilliseconds()).toEqual(0); | ||
date.setUTCMinutes(0, 1, 2); | ||
expect(date.getUTCMilliseconds()).toEqual(2); | ||
date.setUTCMinutes(0, 0, 0); | ||
expect(date.getUTCMilliseconds()).toEqual(0); | ||
}); | ||
it("accepts an optional parameter for seconds in setMinutes", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York"); | ||
expect(date.getSeconds()).toEqual(0); | ||
date.setMinutes(0, 1); | ||
expect(date.getSeconds()).toEqual(1); | ||
date.setMinutes(0, 0); | ||
expect(date.getSeconds()).toEqual(0); | ||
}); | ||
it("accepts an optional parameter for milliseconds in setMinutes", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York"); | ||
expect(date.getMilliseconds()).toEqual(0); | ||
date.setMinutes(0, 1, 2); | ||
expect(date.getMilliseconds()).toEqual(2); | ||
date.setMinutes(0, 0, 0); | ||
expect(date.getMilliseconds()).toEqual(0); | ||
}); | ||
// Seconds | ||
it("accepts an optional parameter for milliseconds in setUTCSeconds", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC"); | ||
expect(date.getUTCMilliseconds()).toEqual(0); | ||
date.setUTCSeconds(0, 1); | ||
expect(date.getUTCMilliseconds()).toEqual(1); | ||
date.setUTCSeconds(0, 0); | ||
expect(date.getUTCMilliseconds()).toEqual(0); | ||
}); | ||
it("accepts an optional parameter for milliseconds in setSeconds", function() { | ||
var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York"); | ||
expect(date.getMilliseconds()).toEqual(0); | ||
date.setSeconds(0, 1); | ||
expect(date.getMilliseconds()).toEqual(1); | ||
date.setSeconds(0, 0); | ||
expect(date.getMilliseconds()).toEqual(0); | ||
}); | ||
it("handles static dst offsets in Zones like '1:00' instead of DST rule references.", function(){ | ||
var date = new timezoneJS.Date(Date.UTC(2012, 2, 1, 0, 0, 0, 0), "Pacific/Apia"); | ||
expect(date.getTimezoneOffset()).toBe(-840); | ||
expect(date.toString("yyyy-MM-dd HH:mm:ss Z")).toBe("2012-03-01 14:00:00 WSDT"); | ||
}); | ||
it("returns the milis value for setTime", function() { | ||
var date = new timezoneJS.Date("America/New_York"); | ||
expect(date.setTime(123456789)).toEqual(123456789); | ||
}); | ||
it("returns the milis value for every set* command", function() { | ||
milis = 1193851822000 | ||
var date = new timezoneJS.Date(milis, "America/New_York"); | ||
expect(date.setFullYear(date.getFullYear())).toEqual(milis); | ||
expect(date.setMonth(date.getMonth())).toEqual(milis); | ||
expect(date.setDate(date.getDate())).toEqual(milis); | ||
expect(date.setHours(date.getHours())).toEqual(milis); | ||
expect(date.setMinutes(date.getMinutes())).toEqual(milis); | ||
expect(date.setSeconds(date.getSeconds())).toEqual(milis); | ||
expect(date.setMilliseconds(date.getMilliseconds())).toEqual(milis); | ||
}); | ||
it("returns the milis value for every setUTC* command", function() { | ||
milis = 1193851822000 | ||
var date = new timezoneJS.Date(milis, "America/New_York"); | ||
expect(date.setUTCFullYear(date.getUTCFullYear())).toEqual(milis); | ||
expect(date.setUTCMonth(date.getUTCMonth())).toEqual(milis); | ||
expect(date.setUTCDate(date.getUTCDate())).toEqual(milis); | ||
expect(date.setUTCHours(date.getUTCHours())).toEqual(milis); | ||
expect(date.setUTCMinutes(date.getUTCMinutes())).toEqual(milis); | ||
expect(date.setUTCSeconds(date.getUTCSeconds())).toEqual(milis); | ||
expect(date.setUTCMilliseconds(date.getUTCMilliseconds())).toEqual(milis); | ||
}); | ||
it("handles getYear appropriately strangely (to spec)", function() { | ||
// http://www.ecma-international.org/ecma-262/5.1/#sec-B.2.4 | ||
var date = new timezoneJS.Date(1998, 11, 31, 0, 0, 0, "America/New_York"); | ||
expect(date.getYear()).toEqual(98); | ||
date.setFullYear(2013); | ||
expect(date.getYear()).toEqual(113); | ||
}); | ||
it("handles setYear appropriately strangely (to spec)", function() { | ||
// http://www.ecma-international.org/ecma-262/5.1/#sec-B.2.5 | ||
var date = new timezoneJS.Date(2001, 11, 31, 0, 0, 0, "America/New_York"); | ||
date.setYear(98) | ||
expect(date.getFullYear()).toEqual(1998); | ||
date.setYear(2003); | ||
expect(date.getFullYear()).toEqual(2003); | ||
}); | ||
it('Represents hours in 24 hour format', function () { | ||
for (var i = 0; i < 24; i ++) { | ||
var time_value = new timezoneJS.Date(2013, 1, 1, i, 0, 0, 0, "America/Chicago"); | ||
expect(time_value.toString("H")).toEqual(i.toString()); | ||
} | ||
}); | ||
xit('Represents hours in 12 hour format by k side effect', function () { | ||
for (var i = 0; i < 24; i ++) { | ||
var ampm = i >= 12 ? "PM" : "AM"; | ||
var hour = (i % 12); | ||
hour = (hour === 0) ? '12' : hour.toString(); | ||
zhour = hour > 9 ? hour.toString() : '0' + hour; | ||
var time_value = new timezoneJS.Date(2013, 1, 1, i, 0, 0, 0, "America/Chicago"); | ||
expect(time_value.toString("H k")).toEqual(hour + " " + ampm); | ||
expect(time_value.toString("HH k")).toEqual(zhour + " " + ampm); | ||
} | ||
}); | ||
it('Represents hours in single digit 12 hour format', function () { | ||
for (var i = 0; i < 24; i ++) { | ||
var ampm = i >= 12 ? "PM" : "AM"; | ||
var hour = (i % 12); | ||
hour = (hour === 0) ? '12' : hour.toString(); | ||
zhour = hour > 9 ? hour.toString() : '0' + hour; | ||
var time_value = new timezoneJS.Date(2013, 1, 1, i, 0, 0, 0, "America/Chicago"); | ||
expect(time_value.toString("h k")).toEqual(hour + " " + ampm); | ||
expect(time_value.toString("hh k")).toEqual(zhour + " " + ampm); | ||
} | ||
}); | ||
}); |
@@ -34,2 +34,3 @@ var fs = require('fs'); | ||
timezoneJS.timezone.loadingScheme = opts.loadingScheme; | ||
timezoneJS.timezone.defaultZoneFile = opts.defaultZoneFile; | ||
if (opts.loadingScheme !== timezoneJS.timezone.loadingSchemes.MANUAL_LOAD) { | ||
@@ -52,3 +53,3 @@ //Set up again | ||
return init(require('../src/date'), options); | ||
} | ||
}; | ||
@@ -55,0 +56,0 @@ TestUtils.parseISO = function (timestring) { |
@@ -21,3 +21,4 @@ var TestUtils = require('./test-utils') | ||
expect(sampleTz.tzAbbr).toEqual('ICT'); | ||
expect(new timezoneJS.Date('America/New_York').getTimezoneOffset() > 0).toBe(true); | ||
}); | ||
}); |
268
src/date.js
@@ -35,6 +35,8 @@ // ----- | ||
*/ | ||
/*jslint laxcomma:true, laxbreak:true, expr:true*/ | ||
(function () { | ||
// Standard initialization stuff to make sure the library is | ||
// usable on both client and server (node) side. | ||
"use strict"; | ||
var root = this; | ||
@@ -49,3 +51,3 @@ | ||
timezoneJS.VERSION = '1.0.0'; | ||
timezoneJS.VERSION = '0.4.4'; | ||
@@ -58,3 +60,3 @@ // Grab the ajax library from global context. | ||
, fleegix = root.fleegix | ||
// Declare constant list of days and months. Unfortunately this doesn't leave room for i18n due to the Olson data being in English itself | ||
// Declare constant list of days and months. Unfortunately this doesn't leave room for i18n due to the Olson data being in English itself | ||
, DAYS = timezoneJS.Days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] | ||
@@ -79,10 +81,33 @@ , MONTHS = timezoneJS.Months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] | ||
//Handle array indexOf in IE | ||
if (!Array.prototype.indexOf) { | ||
Array.prototype.indexOf = function (el) { | ||
for (var i = 0; i < this.length; i++ ) { | ||
if (el === this[i]) return i; | ||
//From https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf | ||
//Extending Array prototype causes IE to iterate thru extra element | ||
var _arrIndexOf = Array.prototype.indexOf || function (el) { | ||
if (this === null) { | ||
throw new TypeError(); | ||
} | ||
var t = Object(this); | ||
var len = t.length >>> 0; | ||
if (len === 0) { | ||
return -1; | ||
} | ||
var n = 0; | ||
if (arguments.length > 1) { | ||
n = Number(arguments[1]); | ||
if (n != n) { // shortcut for verifying if it's NaN | ||
n = 0; | ||
} else if (n !== 0 && n !== Infinity && n !== -Infinity) { | ||
n = (n > 0 || -1) * Math.floor(Math.abs(n)); | ||
} | ||
} | ||
if (n >= len) { | ||
return -1; | ||
} | ||
} | ||
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0); | ||
for (; k < len; k++) { | ||
if (k in t && t[k] === el) { | ||
return k; | ||
} | ||
} | ||
return -1; | ||
}; | ||
@@ -121,3 +146,3 @@ // Format a number to the length = digits. For ex: | ||
var _transport = function (opts) { | ||
if ((!fleegix || typeof fleegix.xhr === 'undefined') && (!$ || typeof $.ajax === 'undefined')) { | ||
if ((!fleegix || typeof fleegix.xhr === 'undefined') && (!jQuery || typeof jQuery.ajax === 'undefined')) { | ||
throw new Error('Please use the Fleegix.js XHR module, jQuery ajax, Zepto ajax, or define your own transport mechanism for downloading zone files.'); | ||
@@ -131,3 +156,3 @@ } | ||
? fleegix.xhr.doReq({ url: opts.url, async: false }) | ||
: $.ajax({ url : opts.url, async : false }).responseText; | ||
: jQuery.ajax({ url : opts.url, async : false }).responseText; | ||
} | ||
@@ -141,3 +166,3 @@ return fleegix && fleegix.xhr | ||
}) | ||
: $.ajax({ | ||
: jQuery.ajax({ | ||
url : opts.url, | ||
@@ -180,2 +205,3 @@ dataType: 'text', | ||
} | ||
var is_dt_local = false; | ||
switch (args.length) { | ||
@@ -187,2 +213,8 @@ case 0: | ||
dt = new Date(args[0]); | ||
// Date strings are local if they do not contain 'Z', 'T' or timezone offsets like '+0200' | ||
// - more info below | ||
if (typeof args[0] == 'string' && args[0].search(/[+-][0-9]{4}/) == -1 | ||
&& args[0].search(/Z/) == -1 && args[0].search(/T/) == -1) { | ||
is_dt_local = true; | ||
} | ||
break; | ||
@@ -194,2 +226,3 @@ default: | ||
dt = new Date(arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], arr[6]); | ||
is_dt_local = true; | ||
break; | ||
@@ -209,7 +242,18 @@ } | ||
this.timezone = tz || null; | ||
//Tricky part: | ||
// For the cases where there are 1/2 arguments: `timezoneJS.Date(millis, [tz])` and `timezoneJS.Date(Date, [tz])`. The | ||
// Date `dt` created should be in UTC. Thus the way I detect such cases is to determine if `arr` is not populated & `tz` | ||
// is specified. Because if `tz` is not specified, `dt` can be in local time. | ||
if (arr.length) { | ||
// Tricky part: | ||
// The date is either given as unambiguous UTC date or otherwise the date is assumed | ||
// to be a date in timezone `tz` or a locale date if `tz` is not provided. Thus, to | ||
// determine how to use `dt` we distinguish between the following cases: | ||
// - UTC (is_dt_local = false) | ||
// `timezoneJS.Date(millis, [tz])` | ||
// `timezoneJS.Date(Date, [tz])` | ||
// `timezoneJS.Date(dt_str_tz, [tz])` | ||
// - local/timezone `tz` (is_dt_local = true) | ||
// `timezoneJS.Date(year, mon, day, [hour], [min], [second], [tz])` | ||
// `timezoneJS.Date(dt_str, [tz])` | ||
// | ||
// `dt_str_tz` is a date string containing timezone information, i.e. containing 'Z', 'T' or | ||
// /[+-][0-9]{4}/ (e.g. '+0200'), while `dt_str` is a string which does not contain | ||
// timezone information. See: http://dygraphs.com/date-formats.html | ||
if (is_dt_local) { | ||
this.setFromDateObjProxy(dt); | ||
@@ -227,3 +271,3 @@ } else { | ||
getMonth: function () { return this.month; }, | ||
getYear: function () { return this.year; }, | ||
getYear: function () { return this.year - 1900; }, | ||
getHours: function () { return this.hours; }, | ||
@@ -263,3 +307,3 @@ getMilliseconds: function () { return this.milliseconds; }, | ||
this._useCache = true; | ||
return res | ||
return res; | ||
}, | ||
@@ -271,21 +315,89 @@ getUTCDateProxy: function () { | ||
}, | ||
setDate: function (n) { this.setAttribute('date', n); }, | ||
setFullYear: function (n) { this.setAttribute('year', n); }, | ||
setMonth: function (n) { this.setAttribute('month', n); }, | ||
setYear: function (n) { this.setUTCAttribute('year', n); }, | ||
setHours: function (n) { this.setAttribute('hours', n); }, | ||
setMilliseconds: function (n) { this.setAttribute('milliseconds', n); }, | ||
setMinutes: function (n) { this.setAttribute('minutes', n); }, | ||
setSeconds: function (n) { this.setAttribute('seconds', n); }, | ||
setDate: function (date) { | ||
this.setAttribute('date', date); | ||
return this.getTime(); | ||
}, | ||
setFullYear: function (year, month, date) { | ||
if (date !== undefined) { this.setAttribute('date', 1); } | ||
this.setAttribute('year', year); | ||
if (month !== undefined) { this.setAttribute('month', month); } | ||
if (date !== undefined) { this.setAttribute('date', date); } | ||
return this.getTime(); | ||
}, | ||
setMonth: function (month, date) { | ||
this.setAttribute('month', month); | ||
if (date !== undefined) { this.setAttribute('date', date); } | ||
return this.getTime(); | ||
}, | ||
setYear: function (year) { | ||
year = Number(year); | ||
if (0 <= year && year <= 99) { year += 1900; } | ||
this.setUTCAttribute('year', year); | ||
return this.getTime(); | ||
}, | ||
setHours: function (hours, minutes, seconds, milliseconds) { | ||
this.setAttribute('hours', hours); | ||
if (minutes !== undefined) { this.setAttribute('minutes', minutes); } | ||
if (seconds !== undefined) { this.setAttribute('seconds', seconds); } | ||
if (milliseconds !== undefined) { this.setAttribute('milliseconds', milliseconds); } | ||
return this.getTime(); | ||
}, | ||
setMinutes: function (minutes, seconds, milliseconds) { | ||
this.setAttribute('minutes', minutes); | ||
if (seconds !== undefined) { this.setAttribute('seconds', seconds); } | ||
if (milliseconds !== undefined) { this.setAttribute('milliseconds', milliseconds); } | ||
return this.getTime(); | ||
}, | ||
setSeconds: function (seconds, milliseconds) { | ||
this.setAttribute('seconds', seconds); | ||
if (milliseconds !== undefined) { this.setAttribute('milliseconds', milliseconds); } | ||
return this.getTime(); | ||
}, | ||
setMilliseconds: function (milliseconds) { | ||
this.setAttribute('milliseconds', milliseconds); | ||
return this.getTime(); | ||
}, | ||
setTime: function (n) { | ||
if (isNaN(n)) { throw new Error('Units must be a number.'); } | ||
this.setFromTimeProxy(n, this.timezone); | ||
return this.getTime(); | ||
}, | ||
setUTCDate: function (n) { this.setUTCAttribute('date', n); }, | ||
setUTCFullYear: function (n) { this.setUTCAttribute('year', n); }, | ||
setUTCHours: function (n) { this.setUTCAttribute('hours', n); }, | ||
setUTCMilliseconds: function (n) { this.setUTCAttribute('milliseconds', n); }, | ||
setUTCMinutes: function (n) { this.setUTCAttribute('minutes', n); }, | ||
setUTCMonth: function (n) { this.setUTCAttribute('month', n); }, | ||
setUTCSeconds: function (n) { this.setUTCAttribute('seconds', n); }, | ||
setUTCFullYear: function (year, month, date) { | ||
if (date !== undefined) { this.setUTCAttribute('date', 1); } | ||
this.setUTCAttribute('year', year); | ||
if (month !== undefined) { this.setUTCAttribute('month', month); } | ||
if (date !== undefined) { this.setUTCAttribute('date', date); } | ||
return this.getTime(); | ||
}, | ||
setUTCMonth: function (month, date) { | ||
this.setUTCAttribute('month', month); | ||
if (date !== undefined) { this.setUTCAttribute('date', date); } | ||
return this.getTime(); | ||
}, | ||
setUTCDate: function (date) { | ||
this.setUTCAttribute('date', date); | ||
return this.getTime(); | ||
}, | ||
setUTCHours: function (hours, minutes, seconds, milliseconds) { | ||
this.setUTCAttribute('hours', hours); | ||
if (minutes !== undefined) { this.setUTCAttribute('minutes', minutes); } | ||
if (seconds !== undefined) { this.setUTCAttribute('seconds', seconds); } | ||
if (milliseconds !== undefined) { this.setUTCAttribute('milliseconds', milliseconds); } | ||
return this.getTime(); | ||
}, | ||
setUTCMinutes: function (minutes, seconds, milliseconds) { | ||
this.setUTCAttribute('minutes', minutes); | ||
if (seconds !== undefined) { this.setUTCAttribute('seconds', seconds); } | ||
if (milliseconds !== undefined) { this.setUTCAttribute('milliseconds', milliseconds); } | ||
return this.getTime(); | ||
}, | ||
setUTCSeconds: function (seconds, milliseconds) { | ||
this.setUTCAttribute('seconds', seconds); | ||
if (milliseconds !== undefined) { this.setUTCAttribute('milliseconds', milliseconds); } | ||
return this.getTime(); | ||
}, | ||
setUTCMilliseconds: function (milliseconds) { | ||
this.setUTCAttribute('milliseconds', milliseconds); | ||
return this.getTime(); | ||
}, | ||
setFromDateObjProxy: function (dt) { | ||
@@ -299,3 +411,3 @@ this.year = dt.getFullYear(); | ||
this.milliseconds = dt.getMilliseconds(); | ||
this._day = dt.getDay(); | ||
this._day = dt.getDay(); | ||
this._dateProxy = dt; | ||
@@ -307,4 +419,3 @@ this._timeProxy = Date.UTC(this.year, this.month, this.date, this.hours, this.minutes, this.seconds, this.milliseconds); | ||
var dt = new Date(utcMillis); | ||
var tzOffset; | ||
tzOffset = tz ? timezoneJS.timezone.getTzInfo(dt, tz).tzOffset : dt.getTimezoneOffset(); | ||
var tzOffset = tz ? timezoneJS.timezone.getTzInfo(utcMillis, tz, true).tzOffset : dt.getTimezoneOffset(); | ||
dt.setTime(utcMillis + (dt.getTimezoneOffset() - tzOffset) * 60000); | ||
@@ -376,2 +487,4 @@ this.setFromDateObjProxy(dt); | ||
.replace(/S+/g, function (token) { return _fixWidth(_this.getMilliseconds(), token.length); }) | ||
// 'h': 12 hour format | ||
.replace(/h+/g, function (token) { return _fixWidth( ((hours%12) === 0) ? 12 : (hours % 12), token.length); }) | ||
// `M`: month. Note: `MM` will be the numeric representation (e.g February is 02) but `MMM` will be text representation (e.g February is Feb) | ||
@@ -433,3 +546,3 @@ .replace(/M+/g, function (token) { | ||
, regionMap = {'Etc':'etcetera','EST':'northamerica','MST':'northamerica','HST':'northamerica','EST5EDT':'northamerica','CST6CDT':'northamerica','MST7MDT':'northamerica','PST8PDT':'northamerica','America':'northamerica','Pacific':'australasia','Atlantic':'europe','Africa':'africa','Indian':'africa','Antarctica':'antarctica','Asia':'asia','Australia':'australasia','Europe':'europe','WET':'europe','CET':'europe','MET':'europe','EET':'europe'} | ||
, regionExceptions = {'Pacific/Honolulu':'northamerica','Atlantic/Bermuda':'northamerica','Atlantic/Cape_Verde':'africa','Atlantic/St_Helena':'africa','Indian/Kerguelen':'antarctica','Indian/Chagos':'asia','Indian/Maldives':'asia','Indian/Christmas':'australasia','Indian/Cocos':'australasia','America/Danmarkshavn':'europe','America/Scoresbysund':'europe','America/Godthab':'europe','America/Thule':'europe','Asia/Yekaterinburg':'europe','Asia/Omsk':'europe','Asia/Novosibirsk':'europe','Asia/Krasnoyarsk':'europe','Asia/Irkutsk':'europe','Asia/Yakutsk':'europe','Asia/Vladivostok':'europe','Asia/Sakhalin':'europe','Asia/Magadan':'europe','Asia/Kamchatka':'europe','Asia/Anadyr':'europe','Africa/Ceuta':'europe','America/Argentina/Buenos_Aires':'southamerica','America/Argentina/Cordoba':'southamerica','America/Argentina/Tucuman':'southamerica','America/Argentina/La_Rioja':'southamerica','America/Argentina/San_Juan':'southamerica','America/Argentina/Jujuy':'southamerica','America/Argentina/Catamarca':'southamerica','America/Argentina/Mendoza':'southamerica','America/Argentina/Rio_Gallegos':'southamerica','America/Argentina/Ushuaia':'southamerica','America/Aruba':'southamerica','America/La_Paz':'southamerica','America/Noronha':'southamerica','America/Belem':'southamerica','America/Fortaleza':'southamerica','America/Recife':'southamerica','America/Araguaina':'southamerica','America/Maceio':'southamerica','America/Bahia':'southamerica','America/Sao_Paulo':'southamerica','America/Campo_Grande':'southamerica','America/Cuiaba':'southamerica','America/Porto_Velho':'southamerica','America/Boa_Vista':'southamerica','America/Manaus':'southamerica','America/Eirunepe':'southamerica','America/Rio_Branco':'southamerica','America/Santiago':'southamerica','Pacific/Easter':'southamerica','America/Bogota':'southamerica','America/Curacao':'southamerica','America/Guayaquil':'southamerica','Pacific/Galapagos':'southamerica','Atlantic/Stanley':'southamerica','America/Cayenne':'southamerica','America/Guyana':'southamerica','America/Asuncion':'southamerica','America/Lima':'southamerica','Atlantic/South_Georgia':'southamerica','America/Paramaribo':'southamerica','America/Port_of_Spain':'southamerica','America/Montevideo':'southamerica','America/Caracas':'southamerica'}; | ||
, regionExceptions = {'Pacific/Honolulu':'northamerica','Atlantic/Bermuda':'northamerica','Atlantic/Cape_Verde':'africa','Atlantic/St_Helena':'africa','Indian/Kerguelen':'antarctica','Indian/Chagos':'asia','Indian/Maldives':'asia','Indian/Christmas':'australasia','Indian/Cocos':'australasia','America/Danmarkshavn':'europe','America/Scoresbysund':'europe','America/Godthab':'europe','America/Thule':'europe','Asia/Istanbul':'europe','Asia/Yekaterinburg':'europe','Asia/Omsk':'europe','Asia/Novosibirsk':'europe','Asia/Krasnoyarsk':'europe','Asia/Irkutsk':'europe','Asia/Yakutsk':'europe','Asia/Vladivostok':'europe','Asia/Sakhalin':'europe','Asia/Magadan':'europe','Asia/Kamchatka':'europe','Asia/Anadyr':'europe','Africa/Ceuta':'europe','America/Argentina/Buenos_Aires':'southamerica','America/Argentina/San_Luis':'southamerica','America/Argentina/Cordoba':'southamerica','America/Argentina/Tucuman':'southamerica','America/Argentina/La_Rioja':'southamerica','America/Argentina/San_Juan':'southamerica','America/Argentina/Jujuy':'southamerica','America/Argentina/Catamarca':'southamerica','America/Argentina/Mendoza':'southamerica','America/Argentina/Rio_Gallegos':'southamerica','America/Argentina/Ushuaia':'southamerica','America/Aruba':'southamerica','America/La_Paz':'southamerica','America/Noronha':'southamerica','America/Belem':'southamerica','America/Fortaleza':'southamerica','America/Recife':'southamerica','America/Araguaina':'southamerica','America/Maceio':'southamerica','America/Bahia':'southamerica','America/Sao_Paulo':'southamerica','America/Campo_Grande':'southamerica','America/Cuiaba':'southamerica','America/Porto_Velho':'southamerica','America/Boa_Vista':'southamerica','America/Manaus':'southamerica','America/Eirunepe':'southamerica','America/Rio_Branco':'southamerica','America/Santiago':'southamerica','Pacific/Easter':'southamerica','America/Bogota':'southamerica','America/Curacao':'southamerica','America/Guayaquil':'southamerica','Pacific/Galapagos':'southamerica','Atlantic/Stanley':'southamerica','America/Cayenne':'southamerica','America/Guyana':'southamerica','America/Asuncion':'southamerica','America/Lima':'southamerica','Atlantic/South_Georgia':'southamerica','America/Paramaribo':'southamerica','America/Port_of_Spain':'southamerica','America/Montevideo':'southamerica','America/Caracas':'southamerica'}; | ||
function invalidTZError(t) { throw new Error('Timezone "' + t + '" is either incorrect, or not loaded in the timezone registry.'); } | ||
@@ -444,6 +557,3 @@ function builtInLoadZoneFile(fileName, opts) { | ||
success : function (str) { | ||
if (_this.parseZones(str) && typeof opts.callback === 'function') { | ||
opts.callback(); | ||
} | ||
return true; | ||
return _this.parseZones(str) && typeof opts.callback === 'function' && opts.callback(); | ||
}, | ||
@@ -476,2 +586,3 @@ error : function () { | ||
} | ||
//str has format hh:mm, can be negative | ||
function parseTimeString(str) { | ||
@@ -483,10 +594,11 @@ var pat = /(\d+)(?::0*(\d*))?(?::0*(\d*))?([wsugz])?$/; | ||
hms[3] = hms[3] ? parseInt(hms[3], 10) : 0; | ||
return hms; | ||
return hms.slice(1, 5); | ||
} | ||
//z is something like `[ '-3:44:40', '-', 'LMT', '1911', 'May', '15', '' ]` or `[ '-5:00', '-', 'EST', '1974', 'Apr', '28', '2:00' ]` | ||
function processZone(z) { | ||
if (!z[3]) { return; } | ||
var yea = parseInt(z[3], 10); | ||
var mon = 11; | ||
var dat = 31; | ||
var yea = parseInt(z[3], 10) | ||
, mon = 11 | ||
, dat = 31; | ||
//If month is there | ||
if (z[4]) { | ||
@@ -496,5 +608,4 @@ mon = SHORT_MONTHS[z[4].substr(0, 3)]; | ||
} | ||
var string = z[6] ? z[6] : '00:00:00' | ||
, t = parseTimeString(string); | ||
return [yea, mon, dat, t[1], t[2], t[3]]; | ||
var t = z[6] ? parseTimeString(z[6]) : [0, 0, 0]; | ||
return [yea, mon, dat, t[0], t[1], t[2]]; | ||
} | ||
@@ -534,6 +645,9 @@ function getZone(dt, tz) { | ||
var off = parseTimeString(time) | ||
, adj = time.indexOf('-') === 0 ? -1 : 1; | ||
off = adj * (((off[1] * 60 + off[2]) * 60 + off[3]) * 1000); | ||
, adj = time.charAt(0) === '-' ? -1 : 1; | ||
off = adj * (((off[0] * 60 + off[1]) * 60 + off[2]) * 1000); | ||
return off/60/1000; | ||
} | ||
function getAdjustedOffset(off, min) { | ||
return -Math.ceil(min - off); | ||
} | ||
@@ -547,2 +661,9 @@ //if isUTC is true, date is given in UTC, otherwise it's given | ||
// If the zone has a DST rule like '1:00', create a rule and return it | ||
// instead of looking it up in the parsed rules | ||
var staticDstMatch = ruleset.match(/^([0-9]):([0-9][0-9])$/); | ||
if (staticDstMatch) { | ||
return [-1000000, 'max', '-', 'Jan', 1, [0, 0, 0], parseInt(staticDstMatch[1],10) * 60 + parseInt(staticDstMatch[2], 10), '-']; | ||
} | ||
//Convert a date to UTC. Depending on the 'type' parameter, the date | ||
@@ -555,3 +676,3 @@ // parameter may be: | ||
// | ||
// - `w`: wall clock time (adjust for both time zone and DST offset). | ||
// - `w`: wall clock time (adjust for both time zone and DST offset). | ||
// | ||
@@ -567,5 +688,5 @@ // DST adjustment is done using the rule given as third argument. | ||
} else if (type === 'w' || !type) { // Wall Clock Time | ||
offset = getAdjustedOffset(basicOffset, rule); | ||
offset = getAdjustedOffset(basicOffset, rule[6]); | ||
} else { | ||
throw("unknown type " + type); | ||
throw new Error("unknown type " + type); | ||
} | ||
@@ -608,3 +729,3 @@ offset *= 60 * 1000; // to millis | ||
if (!isNaN(rule[4])) { | ||
effectiveDate = new Date(Date.UTC(year, SHORT_MONTHS[rule[3]], rule[4], hms[1], hms[2], hms[3], 0)); | ||
effectiveDate = new Date(Date.UTC(year, SHORT_MONTHS[rule[3]], rule[4], hms[0], hms[1], hms[2], 0)); | ||
} | ||
@@ -618,3 +739,3 @@ //Let's hunt for the date. | ||
// Start at the last day of the month and work backward. | ||
effectiveDate = new Date(Date.UTC(year, SHORT_MONTHS[rule[3]] + 1, 1, hms[1] - 24, hms[2], hms[3], 0)); | ||
effectiveDate = new Date(Date.UTC(year, SHORT_MONTHS[rule[3]] + 1, 1, hms[0] - 24, hms[1], hms[2], 0)); | ||
targetDay = SHORT_DAYS[rule[4].substr(4, 3)]; | ||
@@ -626,3 +747,3 @@ operator = "<="; | ||
//Start at the specified date. | ||
effectiveDate = new Date(Date.UTC(year, SHORT_MONTHS[rule[3]], rule[4].substr(5), hms[1], hms[2], hms[3], 0)); | ||
effectiveDate = new Date(Date.UTC(year, SHORT_MONTHS[rule[3]], rule[4].substr(5), hms[0], hms[1], hms[2], 0)); | ||
targetDay = SHORT_DAYS[rule[4].substr(0, 3)]; | ||
@@ -648,3 +769,3 @@ operator = rule[4].substr(3, 2); | ||
if (prevRule) { | ||
effectiveDate = convertDateToUTC(effectiveDate, hms[4], prevRule); | ||
effectiveDate = convertDateToUTC(effectiveDate, hms[3], prevRule); | ||
} | ||
@@ -679,3 +800,3 @@ return effectiveDate; | ||
var year, rule; | ||
if (a.constructor !== Date) { | ||
if (!(a instanceof Date)) { | ||
year = a[0]; | ||
@@ -689,3 +810,3 @@ rule = a[1]; | ||
} | ||
if (b.constructor !== Date) { | ||
if (!(b instanceof Date)) { | ||
year = b[0]; | ||
@@ -716,7 +837,7 @@ rule = b[1]; | ||
//If there are not enough past DST rules... | ||
if (applicableRules.indexOf(date) < 2) { | ||
if (_arrIndexOf.call(applicableRules, date) < 2) { | ||
applicableRules = applicableRules.concat(findApplicableRules(year-1, _this.rules[ruleset])); | ||
applicableRules.sort(compareDates); | ||
} | ||
var pinpoint = applicableRules.indexOf(date); | ||
var pinpoint = _arrIndexOf.call(applicableRules, date); | ||
if (pinpoint > 1 && compareDates(date, applicableRules[pinpoint-1], applicableRules[pinpoint-2][1]) < 0) { | ||
@@ -735,7 +856,3 @@ //The previous rule does not really apply, take the one before that. | ||
} | ||
function getAdjustedOffset(off, rule) { | ||
return -Math.ceil(rule[6] - off); | ||
} | ||
function getAbbreviation(zone, rule) { | ||
var res; | ||
var base = zone[2]; | ||
@@ -753,14 +870,11 @@ if (base.indexOf('%s') > -1) { | ||
} | ||
res = base.replace('%s', repl); | ||
} | ||
else if (base.indexOf('/') > -1) { | ||
return base.replace('%s', repl); | ||
} else if (base.indexOf('/') > -1) { | ||
//Chose one of two alternative strings. | ||
res = base.split("/", 2)[rule[6] ? 1 : 0]; | ||
} else { | ||
res = base; | ||
return base.split("/", 2)[rule[6] ? 1 : 0]; | ||
} | ||
return res; | ||
return base; | ||
} | ||
this.zoneFileBasePath; | ||
this.zoneFileBasePath = null; | ||
this.zoneFiles = ['africa', 'antarctica', 'asia', 'australasia', 'backward', 'etcetera', 'europe', 'northamerica', 'pacificnew', 'southamerica']; | ||
@@ -779,5 +893,5 @@ this.loadingSchemes = { | ||
var opts = { async: true } | ||
, def = this.defaultZoneFile = this.loadingScheme === this.loadingSchemes.PRELOAD_ALL | ||
, def = this.loadingScheme === this.loadingSchemes.PRELOAD_ALL | ||
? this.zoneFiles | ||
: 'northamerica' | ||
: (this.defaultZoneFile || 'northamerica') | ||
, done = 0 | ||
@@ -923,3 +1037,3 @@ , callbackFn; | ||
if (rule) { | ||
off = getAdjustedOffset(off, rule); | ||
off = getAdjustedOffset(off, rule[6]); | ||
} | ||
@@ -926,0 +1040,0 @@ var abbr = getAbbreviation(z, rule); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
97243
14
1848
210
0