browser-tz
Advanced tools
Comparing version 0.1.6 to 0.1.7
12
add.js
@@ -74,2 +74,8 @@ var moment = require("moment-timezone/moment-timezone.js") | ||
datetime.setUTCDate(datetime.getUTCDate() + 7 * amount) | ||
// The logic for `addMonth` should not add two months | ||
// When you add a month to Jan 31 in JavaScript you get | ||
// March 2nd. This is manually fixed by ensuring we set | ||
// it to the real month and then setting the date back and | ||
// clamping the date of the month by the maximum number | ||
// of days in the month | ||
} else if (type === "month") { | ||
@@ -84,2 +90,4 @@ var targetMonth = datetime.getUTCMonth() + amount | ||
datetime.setUTCDate(Math.min(day, daysInMonth)) | ||
} else if (type === "year") { | ||
datetime.setUTCFullYear(datetime.getUTCFullYear() + amount) | ||
} | ||
@@ -97,3 +105,5 @@ | ||
// to the timezone. This avoids some bugs with moment as well | ||
if (type === "week" || type === "day" || type === "month") { | ||
if (type === "week" || type === "day" || | ||
type === "month" || type === "year" | ||
) { | ||
return addLocalTimezone(type, date, amount) | ||
@@ -100,0 +110,0 @@ } |
@@ -23,11 +23,4 @@ var moment = require("moment-timezone/moment-timezone.js") | ||
addMonth: add.bind(null, "month"), | ||
addYear: NotImplemented("addYear") | ||
// addYear: add.bind(null, "year") | ||
addYear: add.bind(null, "year") | ||
} | ||
} | ||
function NotImplemented(name) { | ||
return function () { | ||
throw new Error("browser-tz: not implemented " + name) | ||
} | ||
} |
{ | ||
"name": "browser-tz", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "Timezone specific manipulation of datetime strings", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -265,2 +265,24 @@ var test = require("tape") | ||
test("addYear", function (assert) { | ||
assertBadDates(assert, "addYear") | ||
assert.equal(tz.addYear("2013-11-03T05:00:00.000Z", 1), | ||
"2014-11-03T05:00:00.000Z") | ||
assert.equal(tz.addYear("2013-11-03T05:00:00.000-05:00", 1), | ||
"2014-11-03T05:00:00.000-05:00") | ||
assert.equal(tz.addYear("2013-11-03T05:00:00.000", 1), | ||
"2014-11-03T05:00:00.000") | ||
assertDSTBoundary(assert, "year", { | ||
iso: "2013-11-03T06:00:00Z", | ||
timezone: "America/Toronto" | ||
}) | ||
assertDSTBoundary(assert, "year", { | ||
iso: "2013-03-10T07:00:00.000Z", | ||
timezone: "America/Toronto" | ||
}) | ||
assert.end() | ||
}) | ||
function assertBadDates(assert, operation) { | ||
@@ -282,24 +304,6 @@ assert.equal(tz[operation]("JUNK", 1), "BAD DATE") | ||
var operation, timeChange | ||
var options = getDSTBoundaryOptions(type) | ||
var operation = options.operation | ||
var timeChange = options.timeChange | ||
if (type === "milliSecond") { | ||
operation = timeChange = { fn: "addMillisecond", amount: 100 } | ||
} else if (type === "second") { | ||
operation = { fn: "addSecond", amount: 1 } | ||
timeChange = { fn: "addMillisecond", amount: 1000 } | ||
} else if (type === "minute") { | ||
operation = { fn: "addMinute", amount: 1 } | ||
timeChange = { fn: "addSecond", amount: 60 } | ||
} else if (type === "hour") { | ||
operation = { fn: "addHour", amount: 1 } | ||
timeChange = { fn: "addMinute", amount: 60 } | ||
} else if (type === "day") { | ||
operation = timeChange = { fn: "addDay", amount: 2 } | ||
} else if (type === "week") { | ||
operation = { fn: "addWeek", amount: 2 } | ||
timeChange = { fn: "addDay", amount: 14 } | ||
} else if (type === "month") { | ||
operation = timeChange = { fn: "addMonth", amount: 2 } | ||
} | ||
var gmtTimes = { | ||
@@ -316,3 +320,5 @@ "-1.5": tz[timeChange.fn](iso, timeChange.amount * -1.5), | ||
var expectedTimes | ||
if (type === "week" || type === "day" || type === "month") { | ||
if (type === "week" || type === "day" || | ||
type === "month" || type === "year" | ||
) { | ||
var beforeTime = { | ||
@@ -394,2 +400,30 @@ iso: tz.IsoString({ | ||
} | ||
function getDSTBoundaryOptions(type) { | ||
var operation, timeChange | ||
if (type === "milliSecond") { | ||
operation = timeChange = { fn: "addMillisecond", amount: 100 } | ||
} else if (type === "second") { | ||
operation = { fn: "addSecond", amount: 1 } | ||
timeChange = { fn: "addMillisecond", amount: 1000 } | ||
} else if (type === "minute") { | ||
operation = { fn: "addMinute", amount: 1 } | ||
timeChange = { fn: "addSecond", amount: 60 } | ||
} else if (type === "hour") { | ||
operation = { fn: "addHour", amount: 1 } | ||
timeChange = { fn: "addMinute", amount: 60 } | ||
} else if (type === "day") { | ||
operation = timeChange = { fn: "addDay", amount: 2 } | ||
} else if (type === "week") { | ||
operation = { fn: "addWeek", amount: 2 } | ||
timeChange = { fn: "addDay", amount: 14 } | ||
} else if (type === "month") { | ||
operation = timeChange = { fn: "addMonth", amount: 2 } | ||
} else if (type === "year") { | ||
operation = timeChange = { fn: "addYear", amount: 2 } | ||
} | ||
return { operation: operation, timeChange: timeChange } | ||
} | ||
} |
42823
858