browser-tz
Advanced tools
Comparing version 0.1.1 to 0.1.2
79
index.js
var moment = require("moment-timezone/moment-timezone.js") | ||
var isISOString = new RegExp( | ||
"(\\d\\d\\d\\d)-" + // year | ||
"(\\d\\d)-" + // month | ||
"(\\d\\d)T" + // day | ||
"(\\d\\d):" + // hour | ||
"(\\d\\d):" + // minute | ||
"(\\d\\d)" + // second | ||
"\\.?(\\d\\d\\d)?" + // optional milliseconds | ||
"([Z(-\\d\\d:\\d\\d)])?" // optional timezone information | ||
) | ||
var SECOND = 1000 | ||
var MINUTE = 60 * SECOND | ||
var HOUR = 60 * MINUTE | ||
var DAY = 24 * HOUR | ||
// var WEEK = 7 * DAY | ||
var add = require("./add.js") | ||
var IsoString = require("./iso-string.js") | ||
var format = require("./format.js") | ||
@@ -26,61 +14,8 @@ module.exports = BrowserTimezone | ||
return { | ||
time: time | ||
IsoString: IsoString, | ||
addWeek: add.bind(null, "week"), | ||
addHour: add.bind(null, "hour"), | ||
format: format | ||
} | ||
} | ||
function time(date, timezone) { | ||
if (typeof date === "string") { | ||
date = { iso: date, timezone: timezone } | ||
} | ||
var isoParts = date.iso.match(isISOString) | ||
if (!isoParts) { | ||
return "BAD DATE" | ||
} | ||
if (!date.timezone) { | ||
return date.iso | ||
} | ||
if (!moment.tz.__zones[date.timezone]) { | ||
return "BAD DATE" | ||
} | ||
// var isoYear = Number(isoParts[1]) | ||
// var isoMonth = Number(isoParts[2]) | ||
var isoDay = Number(isoParts[3]) | ||
var isoHour = Number(isoParts[4]) | ||
var isoMinute = Number(isoParts[5]) | ||
// var isoSecond = Number(isoParts[6]) | ||
// var isoMillisecond = isoParts[7] ? Number(isoParts[7]) : 0 | ||
var isLocal = !isoParts[8] | ||
var momentDate = moment(date.iso).tz(date.timezone) | ||
if (isLocal) { | ||
// var isoOffset = (isoDay * DAY) + | ||
// (isoHour * HOUR) + | ||
// (isoMinute * MINUTE) | ||
// var momentOffset = (momentDate.date() * DAY) + | ||
// (momentDate.hour() * HOUR) + | ||
// (momentDate.minute() * MINUTE) | ||
// var diff = isoOffset - momentOffset | ||
// momentDate.add("millisecond", diff) | ||
momentDate.date(isoDay) | ||
// go back an extra hour to jump over the Timezone gap | ||
// this make ambigious local times always favor the | ||
// earliest time. so when 1am happens twice because 2am | ||
// switches back to 1am it picks the first 1am | ||
momentDate.hour(Math.max(isoHour - 1, 0)) | ||
// changing hour across a TZ gap is buggy. So we | ||
// set the hour twice, once to cross the timezone gap | ||
// and the second one to get the hour right | ||
momentDate.hour(isoHour) | ||
momentDate.minute(isoMinute) | ||
} | ||
return momentDate.format("YYYY-MM-DDTHH:mm:ss.SSSZ") | ||
} |
{ | ||
"name": "browser-tz", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Timezone specific manipulation of datetime strings", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
var test = require("tape") | ||
var timezoneData = require("moment-timezone/moment-timezone.json") | ||
if (!Function.prototype.bind) { | ||
Function.prototype.bind = function (context) { | ||
var args = [].slice.call(arguments, 1) | ||
var fn = this | ||
return function () { | ||
return fn.apply(null, args.concat([].slice.call(arguments))) | ||
} | ||
} | ||
} | ||
var BrowserTimezone = require("../index") | ||
var tz = BrowserTimezone(timezoneData) | ||
var tz = require("./tz.js") | ||
@@ -13,3 +23,5 @@ test("BrowserTimezone is a function", function (assert) { | ||
test("tz has correct methods", function (assert) { | ||
assert.equal(typeof tz.time, "function") | ||
assert.equal(typeof tz.IsoString, "function") | ||
assert.equal(typeof tz.addWeek, "function") | ||
assert.equal(typeof tz.format, "function") | ||
@@ -19,98 +31,4 @@ assert.end() | ||
test("tz.time", function (assert) { | ||
assert.equal(tz.time("garbage"), "BAD DATE") | ||
assert.equal(tz.time("2013-08-10T00:00:00"), "2013-08-10T00:00:00") | ||
assert.equal(tz.time("2013-08-10T00:00:00Z"), "2013-08-10T00:00:00Z") | ||
assert.equal(tz.time("2013-08-10T00:00:00-05:00"), | ||
"2013-08-10T00:00:00-05:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-08-10T00:00:00", | ||
timezone: "jomomma" | ||
}), "BAD DATE") | ||
assert.equal(tz.time({ | ||
iso: "2013-08-10T00:00:00Z", | ||
timezone: "jomomma" | ||
}), "BAD DATE") | ||
assert.equal(tz.time({ | ||
iso: "2013-08-10T00:00:00", | ||
timezone: "America/Toronto" | ||
}), "2013-08-10T00:00:00.000-04:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-08-10T00:00:00Z", | ||
timezone: "America/Toronto" | ||
}), "2013-08-09T20:00:00.000-04:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-11-02T06:00:00Z", | ||
timezone: "America/Toronto" | ||
}), "2013-11-02T02:00:00.000-04:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-11-03T06:00:00Z", | ||
timezone: "America/Toronto" | ||
}), "2013-11-03T01:00:00.000-05:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-11-03T06:01:00Z", | ||
timezone: "America/Toronto" | ||
}), "2013-11-03T01:01:00.000-05:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-11-03T05:59:00Z", | ||
timezone: "America/Toronto" | ||
}), "2013-11-03T01:59:00.000-04:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-11-03T05:00:00Z", | ||
timezone: "America/Toronto" | ||
}), "2013-11-03T01:00:00.000-04:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-03-10T05:00:00Z", | ||
timezone: "America/Toronto" | ||
}), "2013-03-10T00:00:00.000-05:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-03-10T06:00:00Z", | ||
timezone: "America/Toronto" | ||
}), "2013-03-10T01:00:00.000-05:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-03-10T06:59:00Z", | ||
timezone: "America/Toronto" | ||
}), "2013-03-10T01:59:00.000-05:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-03-10T07:00:00Z", | ||
timezone: "America/Toronto" | ||
}), "2013-03-10T03:00:00.000-04:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-03-10T07:01:00Z", | ||
timezone: "America/Toronto" | ||
}), "2013-03-10T03:01:00.000-04:00") | ||
assert.end() | ||
}) | ||
test("tz.time ambigious", function (assert) { | ||
assert.equal(tz.time({ | ||
iso: "2013-11-03T00:00:00", | ||
timezone: "America/Toronto" | ||
}), "2013-11-03T00:00:00.000-04:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-11-03T00:59:00", | ||
timezone: "America/Toronto" | ||
}), "2013-11-03T00:59:00.000-04:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-11-03T02:00:00", | ||
timezone: "America/Toronto" | ||
}), "2013-11-03T02:00:00.000-05:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-11-03T02:01:00", | ||
timezone: "America/Toronto" | ||
}), "2013-11-03T02:01:00.000-05:00") | ||
// These are ambigious | ||
assert.equal(tz.time({ | ||
iso: "2013-11-03T01:00:00", | ||
timezone: "America/Toronto" | ||
}), "2013-11-03T01:00:00.000-04:00") | ||
assert.equal(tz.time({ | ||
iso: "2013-11-03T01:30:00", | ||
timezone: "America/Toronto" | ||
}), "2013-11-03T01:30:00.000-04:00") | ||
assert.end() | ||
}) | ||
require("./time.js") | ||
require("./add.js") | ||
require("./format.js") |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
18536
19
352
1