new timezone handling with timezoneJS.date.js

Does not work with DST yet... Maybe rewrite the js date/timehandling in php....

Status: does not work as expected!
This commit is contained in:
2013-02-26 23:32:02 +00:00
parent 5af208a20f
commit 270edc7ee5
42 changed files with 20618 additions and 11 deletions

View File

@@ -0,0 +1,466 @@
var TestUtils = require('./test-utils')
, timezoneJS = TestUtils.getTimezoneJS();
describe('timezoneJS.Date', function () {
it('should have correct format when initialized', function () {
var date = new timezoneJS.Date();
expect(date.toString()).toMatch(/[\d]{4}(-[\d]{2}){2} ([\d]{2}:){2}[\d]{2}/);
});
it('should format string correctly in toISOString', function () {
var date = new timezoneJS.Date();
expect(date.toISOString()).toMatch(/[\d]{4}(-[\d]{2}){2}T([\d]{2}:){2}[\d]{2}.[\d]{3}/);
});
it('should get date correctly from UTC (2011-10-28T12:44:22.172000000)', function () {
var date = new timezoneJS.Date(2011, 9, 28, 12, 44, 22, 172,'Etc/UTC');
expect(date.getTime()).toEqual(1319805862172);
expect(date.toString()).toEqual('2011-10-28 12:44:22');
expect(date.toString('yyyy-MM-dd')).toEqual('2011-10-28');
});
it('should format 2011-10-28T12:44:22.172 UTC to different formats correctly', function () {
var date = new timezoneJS.Date(2011,9,28,12,44,22,172,'Etc/UTC');
expect(date.toString('MMM dd yyyy')).toEqual('Oct 28 2011');
expect(date.toString('MMM dd yyyy HH:mm:ss k')).toEqual('Oct 28 2011 12:44:22 PM');
expect(date.toString('MMM dd yyyy HH:mm:ss k Z')).toEqual('Oct 28 2011 12:44:22 PM UTC');
});
it('should format 2011-10-28T12:44:22.172 UTC to different formats and tz correctly', function () {
var date = new timezoneJS.Date(2011,9,28,12,44,22,172,'Etc/UTC');
expect(date.toString('MMM dd yyyy', 'America/New_York')).toEqual('Oct 28 2011');
expect(date.toString('MMM dd yyyy HH:mm:ss k Z', 'America/New_York')).toEqual('Oct 28 2011 08:44:22 AM EDT');
expect(date.toString('MMM dd yyyy HH:mm:ss k Z', 'Asia/Shanghai')).toEqual('Oct 28 2011 08:44:22 PM CST');
});
it('should format 2011-02-28T12:44:22.172 UTC (before daylight) to different formats and tz correctly', function () {
var date = new timezoneJS.Date(2011,1,28,12,44,22,172,'Etc/UTC');
expect(date.toString('MMM dd yyyy HH:mm:ss k Z', 'America/New_York')).toEqual('Feb 28 2011 07:44:22 AM EST');
expect(date.toString('MMM dd yyyy HH:mm:ss k Z', 'Indian/Cocos')).toEqual('Feb 28 2011 07:14:22 PM CCT');
});
it('should use a default format with the given tz', function () {
var date = new timezoneJS.Date(2012, 7, 30, 10, 56, 0, 0, 'America/Los_Angeles');
expect(date.toString(null, 'Etc/UTC')).toEqual(date.toString('yyyy-MM-dd HH:mm:ss', 'Etc/UTC'));
expect(date.toString(null, 'America/New_York')).toEqual(date.toString('yyyy-MM-dd HH:mm:ss', 'America/New_York'));
});
it('should convert dates from UTC to a timezone correctly', function () {
var date = new timezoneJS.Date(2011,1,28,12,44,22,172,'Etc/UTC');
date.setTimezone('America/Los_Angeles');
expect(date.toString('MMM dd yyyy HH:mm:ss k Z')).toEqual('Feb 28 2011 04:44:22 AM PST');
expect(date.getTime()).toEqual(1298897062172);
expect(date.getHours()).toEqual(4);
});
it('should convert dates from a timezone to UTC correctly', function () {
var date = new timezoneJS.Date(2007, 9, 31, 10, 30, 22, 'America/Los_Angeles');
date.setTimezone('Etc/GMT');
expect(date.getTime()).toEqual(1193851822000);
expect(date.toString('MMM dd yyyy HH:mm:ss k Z')).toEqual('Oct 31 2007 05:30:22 PM UTC');
expect(date.getHours()).toEqual(17);
});
it('should convert dates from one timezone to another correctly', function () {
var dtA = new timezoneJS.Date(2007, 9, 31, 10, 30, 'America/Los_Angeles');
dtA.setTimezone('America/Chicago');
expect(dtA.getTime()).toEqual(1193851800000);
expect(dtA.toString()).toEqual('2007-10-31 12:30:00');
});
it('should convert dates from unix time properly', function () {
var dtA = new timezoneJS.Date(1193851800000);
dtA.setTimezone('America/Chicago');
expect(dtA.getTime()).toEqual(1193851800000);
expect(dtA.toString()).toEqual('2007-10-31 12:30:00');
});
it('should output toISOString correctly', function () {
var dtA = new Date()
, dt = new timezoneJS.Date();
dtA.setTime(dtA.getTime());
expect(dt.getTime()).toEqual(dtA.getTime());
expect(dt.toISOString()).toEqual(dtA.toISOString());
});
it('should output toGMTString correctly', function () {
var dtA = new Date()
, dt = new timezoneJS.Date();
dtA.setTime(dtA.getTime());
expect(dt.getTime()).toEqual(dtA.getTime());
expect(dt.toGMTString()).toEqual(dtA.toGMTString());
});
it('should output toJSON correctly', function () {
var dtA = new Date()
, dt = new timezoneJS.Date();
dtA.setTime(dtA.getTime());
expect(dt.getTime()).toEqual(dtA.getTime());
expect(dt.toJSON()).toEqual(dtA.toJSON());
});
it('should take in millis as constructor', function () {
var dtA = new Date(0)
, dt = new timezoneJS.Date(dtA.getTime());
expect(dt.getTime()).toEqual(dtA.getTime());
expect(dt.toJSON()).toEqual(dtA.toJSON());
});
it('should take in Date object as constructor', function () {
var dtA = new Date(0)
, dt = new timezoneJS.Date(dtA);
expect(dt.getTime()).toEqual(dtA.getTime());
expect(dt.toJSON()).toEqual(dtA.toJSON());
});
it('should take in millis and tz as constructor', function () {
var dtA = new Date(0)
, dt = new timezoneJS.Date(dtA.getTime(), 'Asia/Bangkok');
expect(dt.getTime()).toEqual(0);
});
it('should take in Date object as constructor', function () {
var dtA = new Date(0)
, dt = new timezoneJS.Date(dtA, 'Asia/Bangkok');
expect(dt.getTime()).toEqual(0);
});
it('should take in String and Asia/Bangkok as constructor', function () {
//This is a RFC 3339 UTC string format
var dt = new timezoneJS.Date('2012-01-01T15:00:00.000', 'Asia/Bangkok');
expect(dt.toString()).toEqual('2012-01-01 22:00:00');
expect(dt.toString('yyyy-MM-ddTHH:mm:ss.SSS', 'America/New_York')).toEqual('2012-01-01T10:00:00.000');
expect(dt.toString('yyyy-MM-ddTHH:mm:ss.SSS')).toEqual('2012-01-01T22:00:00.000');
});
it('should take in String and Etc/UTC as constructor', function () {
var dt = new timezoneJS.Date('2012-01-01T15:00:00.000', 'Etc/UTC');
expect(dt.toString('yyyy-MM-ddTHH:mm:ss.SSS', 'America/New_York')).toEqual('2012-01-01T10:00:00.000');
expect(dt.toString('yyyy-MM-ddTHH:mm:ss.SSS')).toEqual('2012-01-01T15:00:00.000');
});
it('should take in String as constructor', function () {
var dtA = new Date()
, dt = new timezoneJS.Date(dtA.toJSON());
expect(dt.toJSON()).toEqual(dtA.toJSON());
});
it('should be able to set hours', function () {
var dtA = new Date(0)
, dt = new timezoneJS.Date(0, 'Etc/UTC');
dt.setHours(6);
expect(dt.getHours()).toEqual(6);
});
it('should be able to set date without altering others', function () {
var dt = new timezoneJS.Date(2012, 2, 2, 5, 0, 0, 0, 'America/Los_Angeles')
, dt2 = new timezoneJS.Date(2011, 4, 15, 23, 0, 0, 0, 'Asia/Bangkok');
var hours = dt.getHours();
dt.setDate(1);
expect(dt.getHours()).toEqual(hours);
hours = dt2.getHours();
dt2.setDate(2);
expect(dt2.getHours()).toEqual(hours);
});
it('should be able to set UTC date without altering others', function () {
var dt = new timezoneJS.Date(2012, 2, 2, 5, 0, 0, 0, 'America/Los_Angeles');
var hours = dt.getUTCHours();
dt.setUTCDate(1);
expect(dt.getUTCHours()).toEqual(hours);
});
it('should adjust daylight saving correctly', function () {
var dt1 = new timezoneJS.Date(2012, 2, 11, 3, 0, 0, 'America/Chicago');
expect(dt1.getTimezoneAbbreviation()).toEqual('CDT');
expect(dt1.getTimezoneOffset()).toEqual(300);
var dt2 = new timezoneJS.Date(2012, 2, 11, 1, 59, 59, 'America/Chicago');
expect(dt2.getTimezoneAbbreviation()).toEqual('CST');
expect(dt2.getTimezoneOffset()).toEqual(360);
});
it('should be able to clone itself', function () {
var dt = new timezoneJS.Date(0, 'America/Chicago')
, dtA = dt.clone();
expect(dt.getTime()).toEqual(dtA.getTime());
expect(dt.toString()).toEqual(dtA.toString());
expect(dt.getTimezoneOffset()).toEqual(dtA.getTimezoneOffset());
expect(dt.getTimezoneAbbreviation()).toEqual(dtA.getTimezoneAbbreviation());
});
it('should convert timezone quickly', function () {
var start = Date.now()
, yearToMillis = 5 * 365 * 24 * 3600 * 1000
, date;
for (var i = 0; i < 5000; i++) {
date = new timezoneJS.Date(start - Math.random() * yearToMillis, 'America/New_York');
date.setTimezone('Europe/Minsk');
}
console.log('Took ' + (Date.now() - start) + 'ms to convert 5000 dates');
});
it('should output 1955-10-30 00:00:00 America/New_York as EDT', function () {
var dt = new timezoneJS.Date(1955, 9, 30, 0, 0, 0, 'America/New_York');
expect(dt.getTimezoneOffset()).toEqual(240);
});
it('should handle Pacific/Apia correctly', function () {
var dt = new timezoneJS.Date(2011, 11, 29, 23, 59, 59, 'Pacific/Apia');
var t = dt.getTime() + 1000;
dt.setTime(t);
expect(dt.toString()).toEqual('2011-12-31 00:00:00');
expect(dt.getTime()).toEqual(t);
});
// 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);
});
});

View File

@@ -0,0 +1,86 @@
var fs = require('fs');
(function () {
var root = this;
var TestUtils = {};
if (typeof exports !== 'undefined') {
TestUtils = exports;
} else {
TestUtils = root.TestUtils = {};
}
var init = function (timezoneJS, options) {
var opts = {
async: false,
loadingScheme: timezoneJS.timezone.loadingSchemes.LAZY_LOAD
};
for (var k in (options || {})) {
opts[k] = options[k];
}
timezoneJS.timezone.transport = function (opts) {
// No success handler, what's the point?
if (opts.async) {
if (typeof opts.success !== 'function') return;
opts.error = opts.error || console.error;
return fs.readFile(opts.url, 'utf8', function (err, data) {
return err ? opts.error(err) : opts.success(data);
});
}
return fs.readFileSync(opts.url, 'utf8');
};
timezoneJS.timezone.loadingScheme = opts.loadingScheme;
timezoneJS.timezone.defaultZoneFile = opts.defaultZoneFile;
if (opts.loadingScheme !== timezoneJS.timezone.loadingSchemes.MANUAL_LOAD) {
//Set up again
timezoneJS.timezone.zoneFileBasePath = 'lib/tz';
timezoneJS.timezone.init(opts);
}
return timezoneJS;
};
TestUtils.getTimezoneJS = function (options) {
//Delete date.js from require cache to force it to reload
for (var k in require.cache) {
if (k.indexOf('date.js') > -1) {
delete require.cache[k];
}
}
return init(require('../src/date'), options);
};
TestUtils.parseISO = function (timestring) {
var pat = '^(?:([+-]?[0-9]{4,})(?:-([0-9]{2})(?:-([0-9]{2}))?)?)?' +
'(?:T(?:([0-9]{2})(?::([0-9]{2})(?::([0-9]{2})(?:\\.' +
'([0-9]{3}))?)?)?)?(Z|[-+][0-9]{2}:[0-9]{2})?)?$';
var match = timestring.match(pat);
if (match) {
var parts = {
year: match[1] || 0,
month: match[2] || 1,
day: match[3] || 1,
hour: match[4] || 0,
minute: match[5] || 0,
second: match[6] || 0,
milli: match[7] || 0,
offset: match[8] || "Z"
};
var utcDate = Date.UTC(parts.year, parts.month-1, parts.day,
parts.hour, parts.minute, parts.second, parts.milli);
if (parts.offset !== "Z") {
match = parts.offset.match('([-+][0-9]{2})(?::([0-9]{2}))?');
if (!match) {
return NaN;
}
var offset = match[1]*60*60*1000+(match[2] || 0)*60*1000;
utcDate -= offset;
}
return new Date(utcDate);
}
return null;
};
}).call(this);

View File

@@ -0,0 +1,37 @@
var TestUtils = require('./test-utils')
, parseISO = TestUtils.parseISO
, date = require('../src/date');
describe('TimezoneJS', function () {
it('should async preload everything correctly', function () {
var i = 0
, timezoneJS
, sampleTz;
runs(function () {
timezoneJS = TestUtils.getTimezoneJS({
loadingScheme: date.timezone.loadingSchemes.PRELOAD_ALL,
async: true,
callback: function () {
//Make sure more than 1 zone is loaded
for (var k in timezoneJS.timezone.loadedZones) {
i++;
}
sampleTz = timezoneJS.timezone.getTzInfo(new Date(), 'Asia/Bangkok');
}
});
});
waitsFor(function () {
return i > 0;
}, 'zones should be loaded', 100);
runs(function () {
expect(timezoneJS.timezone.loadingScheme).toEqual(date.timezone.loadingSchemes.PRELOAD_ALL);
expect(i).toEqual(timezoneJS.timezone.zoneFiles.length);
expect(sampleTz).toBeDefined();
expect(sampleTz.tzAbbr).toEqual('ICT');
});
});
});

View File

@@ -0,0 +1,15 @@
var TestUtils = require('./test-utils')
, parseISO = TestUtils.parseISO
, date = require('../src/date')
, timezoneJS = TestUtils.getTimezoneJS({
defaultZoneFile: 'asia'
});
describe('TimezoneJS', function () {
it('should preload everything correctly', function () {
expect(timezoneJS.timezone.loadingScheme).toEqual(date.timezone.loadingSchemes.LAZY_LOAD);
expect(timezoneJS.timezone.loadedZones.asia).toBe(true);
sampleTz = timezoneJS.timezone.getTzInfo(new Date(), 'Asia/Bangkok');
expect(sampleTz).toBeDefined();
expect(sampleTz.tzAbbr).toEqual('ICT');
});
});

View File

@@ -0,0 +1,25 @@
var TestUtils = require('./test-utils')
, parseISO = TestUtils.parseISO
, date = require('../src/date')
, timezoneJS = TestUtils.getTimezoneJS({
loadingScheme: date.timezone.loadingSchemes.MANUAL_LOAD
});
describe('TimezoneJS', function () {
it('should manually load everything correctly', function () {
var i = 0
, sampleTz;
expect(timezoneJS.timezone.loadingScheme).toEqual(date.timezone.loadingSchemes.MANUAL_LOAD);
//Let's load some stuff
timezoneJS.timezone.loadZoneJSONData('lib/all_cities.json', true);
expect(Object.keys(timezoneJS.timezone.zones).length > 100).toBeTruthy();
sampleTz = timezoneJS.timezone.getTzInfo(new Date(), 'Asia/Bangkok');
expect(sampleTz).toBeDefined();
expect(sampleTz.tzAbbr).toEqual('ICT');
dt = new timezoneJS.Date();
dt.setTimezone('America/Chicago');
expect(dt.getTimezoneAbbreviation()).toMatch(/C(S|D)T/);
dt.setTimezone('America/New_York');
expect(dt.getTimezoneAbbreviation()).toMatch(/E(S|D)T/);
});
});

View File

@@ -0,0 +1,23 @@
var TestUtils = require('./test-utils')
, parseISO = TestUtils.parseISO
, date = require('../src/date')
, timezoneJS = TestUtils.getTimezoneJS({
loadingScheme: date.timezone.loadingSchemes.PRELOAD_ALL
});
describe('TimezoneJS', function () {
it('should preload everything correctly', function () {
var i = 0
, sampleTz;
expect(timezoneJS.timezone.loadingScheme).toEqual(date.timezone.loadingSchemes.PRELOAD_ALL);
//Make sure more than 1 zone is loaded
for (var k in timezoneJS.timezone.loadedZones) {
i++;
}
expect(i).toEqual(timezoneJS.timezone.zoneFiles.length);
i = 0;
sampleTz = timezoneJS.timezone.getTzInfo(new Date(), 'Asia/Bangkok');
expect(sampleTz).toBeDefined();
expect(sampleTz.tzAbbr).toEqual('ICT');
expect(new timezoneJS.Date('America/New_York').getTimezoneOffset() > 0).toBe(true);
});
});

View File

@@ -0,0 +1,176 @@
var TestUtils = require('./test-utils')
, parseISO = TestUtils.parseISO
, timezoneJS = TestUtils.getTimezoneJS();
describe('TimezoneJS', function () {
it('should get America/Chicago DST time correctly', function () {
var testDstLeap = function (arr) {
var expectedArr = [360, 300, 300, 360];
var dt;
var actual;
var expected;
for (var i = 0; i < arr.length; i++) {
dt = timezoneJS.timezone.getTzInfo(parseISO(arr[i]), 'America/Chicago');
actual = dt.tzOffset;
expected = expectedArr[i];
expect(actual).toEqual(expected);
}
};
testDstLeap(['2004-04-04', '2004-04-05', '2004-10-31', '2004-11-01']);
testDstLeap(['2005-04-03', '2005-04-04', '2005-10-30', '2005-10-31']);
testDstLeap(['2006-04-02', '2006-04-03', '2006-10-29', '2006-10-30']);
// 2007 -- new DST rules start here
testDstLeap(['2007-03-11', '2007-03-12', '2007-11-04', '2007-11-05']);
testDstLeap(['2008-03-09', '2008-03-10', '2008-11-02', '2008-11-03']);
testDstLeap(['2009-03-08', '2009-03-09', '2009-11-01', '2009-11-02']);
testDstLeap(['2010-03-14', '2010-03-15', '2010-11-07', '2010-11-08']);
testDstLeap(['2011-03-13', '2011-03-14', '2011-11-06', '2011-11-07']);
});
it('should get tzOffset of America/Sao_Paulo correctly', function () {
// Source: http://www.timeanddate.com/worldclock/clockchange.html?n=233
// Standard: GMT-3 from Feb 16 - Nov 1
// Daylight: GMT-2 from Nov 2 - Feb 16
var dt;
// 2008
//dt = timezoneJS.timezone.getTzInfo(parseISO('2008-02-16-02:00'), 'America/Sao_Paulo');
//expect(120, dt.tzOffset);
dt = timezoneJS.timezone.getTzInfo(parseISO('2008-02-17'), 'America/Sao_Paulo');
expect(dt.tzOffset).toEqual(180);
dt = timezoneJS.timezone.getTzInfo(parseISO('2008-10-11'), 'America/Sao_Paulo');
expect(dt.tzOffset).toEqual(180);
dt = timezoneJS.timezone.getTzInfo(parseISO('2008-10-19'), 'America/Sao_Paulo');
expect(dt.tzOffset).toEqual(120);
});
it('should get New_York time correctly', function () {
// Source: http://www.timeanddate.com/worldclock/city.html?n=179
// Changes every year!
var dt;
// 2006
dt = timezoneJS.timezone.getTzInfo(parseISO('2006-04-02T01:59:59'), 'America/New_York');
expect(dt.tzOffset).toEqual(300);
dt = timezoneJS.timezone.getTzInfo(parseISO('2006-04-02T03:00:01'), 'America/New_York');
expect(dt.tzOffset).toEqual(240);
dt = timezoneJS.timezone.getTzInfo(parseISO('2006-10-29T00:59:59'), 'America/New_York');
expect(dt.tzOffset).toEqual(240);
dt = timezoneJS.timezone.getTzInfo(parseISO('2006-10-29T03:00:01'), 'America/New_York');
expect(dt.tzOffset).toEqual(300);
// 2009
dt = timezoneJS.timezone.getTzInfo(parseISO('2009-03-08T01:59:59'), 'America/New_York');
expect(dt.tzOffset).toEqual(300);
dt = timezoneJS.timezone.getTzInfo(parseISO('2009-03-08T03:00:01'), 'America/New_York');
expect(dt.tzOffset).toEqual(240);
dt = timezoneJS.timezone.getTzInfo(parseISO('2009-11-01T00:59:59'), 'America/New_York');
expect(dt.tzOffset).toEqual(240);
dt = timezoneJS.timezone.getTzInfo(parseISO('2009-11-01T03:00:01'), 'America/New_York');
expect(dt.tzOffset).toEqual(300);
});
it('should get Jerusalem time correctly', function () {
// Source: http://www.timeanddate.com/worldclock/city.html?n=110
// Changes every year!
var dt;
// 2008
dt = timezoneJS.timezone.getTzInfo(parseISO('2008-03-28T01:59:59'), 'Asia/Jerusalem');
expect(dt.tzOffset).toEqual(-120);
dt = timezoneJS.timezone.getTzInfo(parseISO('2008-03-28T03:00:01'), 'Asia/Jerusalem');
expect(dt.tzOffset).toEqual(-180);
dt = timezoneJS.timezone.getTzInfo(parseISO('2008-10-05T00:59:59'), 'Asia/Jerusalem');
expect(dt.tzOffset).toEqual(-180);
dt = timezoneJS.timezone.getTzInfo(parseISO('2008-10-05T03:00:01'), 'Asia/Jerusalem');
expect(dt.tzOffset).toEqual(-120);
// 2009
dt = timezoneJS.timezone.getTzInfo(parseISO('2009-03-27T01:59:59'), 'Asia/Jerusalem');
expect(dt.tzOffset).toEqual(-120);
dt = timezoneJS.timezone.getTzInfo(parseISO('2009-03-27T03:00:01'), 'Asia/Jerusalem');
expect(dt.tzOffset).toEqual(-180);
dt = timezoneJS.timezone.getTzInfo(parseISO('2009-09-27T00:59:59'), 'Asia/Jerusalem');
expect(dt.tzOffset).toEqual(-180);
dt = timezoneJS.timezone.getTzInfo(parseISO('2009-09-27T03:00:01'), 'Asia/Jerusalem');
expect(dt.tzOffset).toEqual(-120);
});
it('should get abbreviation of central EU time correctly', function () {
var dt = timezoneJS.timezone.getTzInfo(parseISO('2010-01-01'), 'Europe/Berlin'); // winter time (CET) for sure
expect(dt.tzAbbr).toEqual('CET');
});
it('should get abbr for central EU summer time correctly', function () {
var dt = timezoneJS.timezone.getTzInfo(parseISO('2010-07-01'), 'Europe/Berlin'); // summer time (CEST) for sure
expect(dt.tzAbbr, 'CEST');
});
it('should get abbr for British Standard time correctly', function () {
var dt = timezoneJS.timezone.getTzInfo(parseISO('2010-01-01'), 'Europe/London'); // winter time (GMT) for sure
expect(dt.tzAbbr, 'GMT');
});
it('should get abbr for British summer time correctly', function () {
var dt = timezoneJS.timezone.getTzInfo(parseISO('2010-07-01'), 'Europe/London'); // summer time (BST) for sure
expect(dt.tzAbbr, 'BST');
});
it('should get abbr CET from tz info of 2010-03-28T01:59:59', function () {
var dt = timezoneJS.timezone.getTzInfo(parseISO('2010-03-28T01:59:59'), 'Europe/Berlin'); // CET, from local time
expect(dt.tzAbbr).toEqual('CET');
});
it('should get abbr CEST from tz info of 2010-03-08T03:00:00', function () {
var dt = timezoneJS.timezone.getTzInfo(parseISO('2010-03-28T03:00:00'), 'Europe/Berlin'); // CEST, from local time
expect(dt.tzAbbr, 'CEST');
});
it('should get abbr CET from tz info of 2010-03-08T00:59:59 UTC', function () {
var dt = timezoneJS.timezone.getTzInfo(parseISO('2010-03-28T00:59:59'), 'Europe/Berlin', true); // CEST, from local time
expect(dt.tzAbbr, 'CET');
});
it('should get abbr CEST from tz info of 2010-03-08T01:00:00 UTC', function () {
var dt = timezoneJS.timezone.getTzInfo(parseISO('2010-03-28T01:00:00'), 'Europe/Berlin', true); // CEST, from local time
expect(dt.tzAbbr, 'CEST');
});
it('should get abbr CST from 2010-03-14T01:59:59 Chicago', function () {
var dt = timezoneJS.timezone.getTzInfo(parseISO('2010-03-14T01:59:59'), 'America/Chicago'); // CST, from local time
expect(dt.tzAbbr).toEqual('CST');
});
it('should get abbr CDT from 2010-03-14T03:00:00 Chicago', function () {
var dt = timezoneJS.timezone.getTzInfo(parseISO('2010-03-14T03:00:00'), 'America/Chicago'); // CST, from local time
expect(dt.tzAbbr).toEqual('CDT');
});
it('should get abbr CST from 2010-03-14T07:59:59 UTC', function () {
var dt = timezoneJS.timezone.getTzInfo(parseISO('2010-03-14T07:59:59'), 'America/Chicago', true); // CST, from local time
expect(dt.tzAbbr).toEqual('CST');
});
it('should get abbr CDT from 2010-03-14T08:00:00 Chicago', function () {
var dt = timezoneJS.timezone.getTzInfo(parseISO('2010-03-14T08:00:00'), 'America/Chicago', true); // CST, from local time
expect(dt.tzAbbr).toEqual('CDT');
});
//This is for issue #1 in github
it('should not get null in getAllZones', function () {
var zones = timezoneJS.timezone.getAllZones();
for (var i = 0; i < zones; i++) {
expect(zones[i]).not.toBe(null);
}
});
it('should get tzInfo quickly', function () {
var time = Date.now();
for (var i = 0; i < 5000; i++) {
timezoneJS.timezone.getTzInfo(new Date(), 'America/Chicago');
}
console.log('Took ' + (Date.now() - time) + 'ms to get 5000 same tzInfo');
});
it('should throw error with invalid zone', function () {
var testFn = function () {
timezoneJS.timezone.getTzInfo(new Date(), 'asd')
}
expect(testFn).toThrow(new Error('Timezone "asd" is either incorrect, or not loaded in the timezone registry.'));
});
});