chronoshift
Advanced tools
Comparing version 0.4.2 to 0.4.3
@@ -95,3 +95,5 @@ /// <reference path="../typings/immutable-class.d.ts" /> | ||
shift(date: Date, timezone: Timezone, step?: number): Date; | ||
move(date: Date, timezone: Timezone, step?: number): Date; | ||
materialize(start: Date, end: Date, timezone: Timezone, step?: number): Date[]; | ||
isAligned(date: Date, timezone: Timezone): boolean; | ||
dividesBy(smaller: Duration): boolean; | ||
getCanonicalLength(): number; | ||
@@ -98,0 +100,0 @@ getDescription(): string; |
@@ -62,2 +62,5 @@ "use strict"; | ||
} | ||
function floorTo(n, roundTo) { | ||
return Math.floor(n / roundTo) * roundTo; | ||
} | ||
function timeShifterFiller(tm) { | ||
@@ -84,3 +87,3 @@ var floor = tm.floor, shift = tm.shift; | ||
var cur = dt.getUTCSeconds(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) | ||
@@ -106,3 +109,3 @@ dt.setUTCSeconds(adj); | ||
var cur = dt.getUTCMinutes(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) | ||
@@ -146,3 +149,3 @@ dt.setUTCMinutes(adj); | ||
var cur = dt.getUTCHours(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) | ||
@@ -152,4 +155,5 @@ dt.setUTCHours(adj); | ||
else { | ||
var cur = dt.getHours(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var wt = Chronoshift.WallTime.UTCToWallTime(dt, tz.toString()); | ||
var cur = wt.getHours(); | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) | ||
@@ -242,3 +246,3 @@ return hourMove(dt, tz, adj - cur); | ||
var cur = dt.getUTCMonth(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) | ||
@@ -249,3 +253,3 @@ dt.setUTCMonth(adj); | ||
var cur = dt.getMonth(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) | ||
@@ -287,3 +291,3 @@ return monthShift(dt, tz, adj - cur); | ||
var cur = dt.getUTCFullYear(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) | ||
@@ -294,3 +298,3 @@ dt.setUTCFullYear(adj); | ||
var cur = dt.getFullYear(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) | ||
@@ -459,6 +463,6 @@ return yearShift(dt, tz, adj - cur); | ||
Duration.prototype.subtract = function (duration) { | ||
if (this.getCanonicalLength() - duration.getCanonicalLength() < 0) { | ||
var newCanonicalDuration = this.getCanonicalLength() - duration.getCanonicalLength(); | ||
if (newCanonicalDuration < 0) | ||
throw new Error("A duration can not be negative."); | ||
} | ||
return Duration.fromCanonicalLength(this.getCanonicalLength() - duration.getCanonicalLength()); | ||
return Duration.fromCanonicalLength(newCanonicalDuration); | ||
}; | ||
@@ -523,7 +527,20 @@ Duration.prototype.valueOf = function () { | ||
}; | ||
Duration.prototype.move = function (date, timezone, step) { | ||
Duration.prototype.materialize = function (start, end, timezone, step) { | ||
if (step === void 0) { step = 1; } | ||
console.warn("The method 'move()' is deprecated. Please use 'shift()' instead."); | ||
return this.shift(date, timezone, step); | ||
var values = []; | ||
var iter = this.floor(start, timezone); | ||
while (iter <= end) { | ||
values.push(iter); | ||
iter = this.shift(iter, timezone, step); | ||
} | ||
return values; | ||
}; | ||
Duration.prototype.isAligned = function (date, timezone) { | ||
return this.floor(date, timezone).valueOf() === date.valueOf(); | ||
}; | ||
Duration.prototype.dividesBy = function (smaller) { | ||
var myCanonicalLength = this.getCanonicalLength(); | ||
var smallerCanonicalLength = smaller.getCanonicalLength(); | ||
return myCanonicalLength % smallerCanonicalLength === 0 && this.isFloorable() && smaller.isFloorable(); | ||
}; | ||
Duration.prototype.getCanonicalLength = function () { | ||
@@ -530,0 +547,0 @@ var spans = this.spans; |
{ | ||
"name": "chronoshift", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "A tiny library for shifting time with timezones", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -180,9 +180,5 @@ module Chronoshift { | ||
public subtract(duration: Duration): Duration { | ||
if (this.getCanonicalLength() - duration.getCanonicalLength() < 0) { | ||
throw new Error("A duration can not be negative."); | ||
} | ||
return Duration.fromCanonicalLength( | ||
this.getCanonicalLength() - duration.getCanonicalLength() | ||
); | ||
var newCanonicalDuration = this.getCanonicalLength() - duration.getCanonicalLength(); | ||
if (newCanonicalDuration < 0) throw new Error("A duration can not be negative."); | ||
return Duration.fromCanonicalLength(newCanonicalDuration); | ||
} | ||
@@ -249,3 +245,3 @@ | ||
*/ | ||
public shift(date: Date, timezone: Timezone, step: number = 1) { | ||
public shift(date: Date, timezone: Timezone, step: number = 1): Date { | ||
var spans = this.spans; | ||
@@ -259,7 +255,38 @@ for (let span of spansWithWeek) { | ||
public move(date: Date, timezone: Timezone, step: number = 1) { | ||
console.warn("The method 'move()' is deprecated. Please use 'shift()' instead."); | ||
return this.shift(date, timezone, step); | ||
/** | ||
* Materializes all the values of this duration form start to end | ||
* @param start The date to start on | ||
* @param end The date to start on | ||
* @param timezone The timezone within which to materialize | ||
* @param step The number of times to step by the duration | ||
*/ | ||
public materialize(start: Date, end: Date, timezone: Timezone, step: number = 1): Date[] { | ||
var values: Date[] = []; | ||
var iter = this.floor(start, timezone); | ||
while (iter <= end) { | ||
values.push(iter); | ||
iter = this.shift(iter, timezone, step); | ||
} | ||
return values; | ||
} | ||
/** | ||
* Checks to see if date is aligned to this duration within the timezone (floors to itself) | ||
* @param date The date to check | ||
* @param timezone The timezone within which to make the check | ||
*/ | ||
public isAligned(date: Date, timezone: Timezone): boolean { | ||
return this.floor(date, timezone).valueOf() === date.valueOf(); | ||
} | ||
/** | ||
* Check to see if this duration can be divided by the given duration | ||
* @param smaller The smaller duration to divide by | ||
*/ | ||
public dividesBy(smaller: Duration): boolean { | ||
var myCanonicalLength = this.getCanonicalLength(); | ||
var smallerCanonicalLength = smaller.getCanonicalLength(); | ||
return myCanonicalLength % smallerCanonicalLength === 0 && this.isFloorable() && smaller.isFloorable(); | ||
} | ||
public getCanonicalLength(): number { | ||
@@ -266,0 +293,0 @@ var spans = this.spans; |
@@ -30,2 +30,6 @@ module Chronoshift { | ||
function floorTo(n: number, roundTo: number): number { | ||
return Math.floor(n / roundTo) * roundTo; | ||
} | ||
function timeShifterFiller(tm: TimeShifter): TimeShifter { | ||
@@ -53,3 +57,3 @@ var { floor, shift } = tm; | ||
var cur = dt.getUTCSeconds(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) dt.setUTCSeconds(adj); | ||
@@ -76,3 +80,3 @@ return dt; | ||
var cur = dt.getUTCMinutes(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) dt.setUTCMinutes(adj); | ||
@@ -119,7 +123,8 @@ return dt; | ||
var cur = dt.getUTCHours(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) dt.setUTCHours(adj); | ||
} else { | ||
var cur = dt.getHours(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
let wt = WallTime.UTCToWallTime(dt, tz.toString()); | ||
var cur = wt.getHours(); | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) return hourMove(dt, tz, adj - cur); | ||
@@ -225,7 +230,7 @@ } | ||
var cur = dt.getUTCMonth(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) dt.setUTCMonth(adj); | ||
} else { | ||
var cur = dt.getMonth(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) return monthShift(dt, tz, adj - cur); | ||
@@ -270,7 +275,7 @@ } | ||
var cur = dt.getUTCFullYear(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) dt.setUTCFullYear(adj); | ||
} else { | ||
var cur = dt.getFullYear(); | ||
var adj = Math.floor(cur / roundTo) * roundTo; | ||
var adj = floorTo(cur, roundTo); | ||
if (cur !== adj) return yearShift(dt, tz, adj - cur); | ||
@@ -277,0 +282,0 @@ } |
@@ -16,3 +16,4 @@ /// <reference path="../typings/mocha/mocha.d.ts" /> | ||
describe("Duration", function () { | ||
var tz = Timezone.fromJS("America/Los_Angeles"); | ||
var TZ_LA = Timezone.fromJS("America/Los_Angeles"); | ||
var TZ_JUNEAU = Timezone.fromJS("America/Juneau"); | ||
it("is an immutable class", function () { | ||
@@ -33,4 +34,4 @@ testImmutableClass(Duration, [ | ||
chai_1.expect(function () { return Duration.fromJS("P0YT0H"); }).to.throw(Error, "Duration can not be empty"); | ||
chai_1.expect(function () { return Duration.fromJS("P0W").shift(new Date(), tz); }).to.throw(Error, "Duration can not be empty"); | ||
chai_1.expect(function () { return Duration.fromJS("P0Y0MT0H0M0S").shift(new Date(), tz); }).to.throw(Error, "Duration can not be empty"); | ||
chai_1.expect(function () { return Duration.fromJS("P0W").shift(new Date(), TZ_LA); }).to.throw(Error, "Duration can not be empty"); | ||
chai_1.expect(function () { return Duration.fromJS("P0Y0MT0H0M0S").shift(new Date(), TZ_LA); }).to.throw(Error, "Duration can not be empty"); | ||
}); | ||
@@ -64,43 +65,63 @@ it("throws error if fromJS is not given a string", function () { | ||
it("parses days over DST", function () { | ||
chai_1.expect(new Duration(new Date("2012-10-29T00:00:00-07:00"), new Date("2012-11-05T00:00:00-08:00"), tz).toString()).to.equal("P7D"); | ||
chai_1.expect(new Duration(new Date("2012-10-29T00:00:00-07:00"), new Date("2012-11-12T00:00:00-08:00"), tz).toString()).to.equal("P14D"); | ||
chai_1.expect(new Duration(new Date("2012-10-29T00:00:00-07:00"), new Date("2012-11-05T00:00:00-08:00"), TZ_LA).toString()).to.equal("P7D"); | ||
chai_1.expect(new Duration(new Date("2012-10-29T00:00:00-07:00"), new Date("2012-11-12T00:00:00-08:00"), TZ_LA).toString()).to.equal("P14D"); | ||
}); | ||
it("parses complex case", function () { | ||
chai_1.expect(new Duration(new Date("2012-10-29T00:00:00-07:00"), new Date(new Date("2012-11-05T00:00:00-08:00").valueOf() - 1000), tz).toString()).to.equal("P6DT23H59M59S"); | ||
chai_1.expect(new Duration(new Date("2012-01-01T00:00:00-08:00"), new Date("2013-03-04T04:05:06-08:00"), tz).toString()).to.equal("P1Y2M3DT4H5M6S"); | ||
chai_1.expect(new Duration(new Date("2012-10-29T00:00:00-07:00"), new Date(new Date("2012-11-05T00:00:00-08:00").valueOf() - 1000), TZ_LA).toString()).to.equal("P6DT23H59M59S"); | ||
chai_1.expect(new Duration(new Date("2012-01-01T00:00:00-08:00"), new Date("2013-03-04T04:05:06-08:00"), TZ_LA).toString()).to.equal("P1Y2M3DT4H5M6S"); | ||
}); | ||
}); | ||
describe("#isFloorable", function () { | ||
it("works on floorable things", function () { | ||
var vs = 'P1Y P5Y P10Y P100Y P1M P2M P3M P4M P1D'.split(' '); | ||
for (var _i = 0, vs_1 = vs; _i < vs_1.length; _i++) { | ||
var v = vs_1[_i]; | ||
chai_1.expect(Duration.fromJS(v).isFloorable(), v).to.equal(true); | ||
} | ||
}); | ||
it("works on not floorable things", function () { | ||
var vs = 'P1Y1M P5M P2D P3D'.split(' '); | ||
for (var _i = 0, vs_2 = vs; _i < vs_2.length; _i++) { | ||
var v = vs_2[_i]; | ||
chai_1.expect(Duration.fromJS(v).isFloorable(), v).to.equal(false); | ||
} | ||
}); | ||
}); | ||
describe("#floor", function () { | ||
it("throws error if complex duration", function () { | ||
chai_1.expect(function () { return Duration.fromJS("P1Y2D").floor(new Date(), tz); }).to.throw(Error, "Can not floor on a complex duration"); | ||
chai_1.expect(function () { return Duration.fromJS("P3DT15H").floor(new Date(), tz); }).to.throw(Error, "Can not floor on a complex duration"); | ||
chai_1.expect(function () { return Duration.fromJS("PT5H").floor(new Date(), tz); }).to.throw(Error, "Can not floor on a hour duration that is not a multiple of 5"); | ||
chai_1.expect(function () { return Duration.fromJS("P1Y2D").floor(new Date(), TZ_LA); }).to.throw(Error, "Can not floor on a complex duration"); | ||
chai_1.expect(function () { return Duration.fromJS("P3DT15H").floor(new Date(), TZ_LA); }).to.throw(Error, "Can not floor on a complex duration"); | ||
chai_1.expect(function () { return Duration.fromJS("PT5H").floor(new Date(), TZ_LA); }).to.throw(Error, "Can not floor on a hour duration that is not a multiple of 5"); | ||
}); | ||
it("works for year", function () { | ||
var p1y = Duration.fromJS("P1Y"); | ||
chai_1.expect(p1y.floor(new Date("2013-09-29T01:02:03.456-07:00"), tz)).to.deep.equal(new Date("2013-01-01T00:00:00.000-08:00")); | ||
chai_1.expect(p1y.floor(new Date("2013-09-29T01:02:03.456-07:00"), TZ_LA)).to.deep.equal(new Date("2013-01-01T00:00:00.000-08:00")); | ||
}); | ||
it("works for T2M", function () { | ||
var p2h = Duration.fromJS("PT2M"); | ||
chai_1.expect(p2h.floor(new Date("2013-09-29T03:03:03.456-07:00"), tz)).to.deep.equal(new Date("2013-09-29T03:02:00.000-07:00")); | ||
it("works for PT2M", function () { | ||
var pt2h = Duration.fromJS("PT2M"); | ||
chai_1.expect(pt2h.floor(new Date("2013-09-29T03:03:03.456-07:00"), TZ_LA)).to.deep.equal(new Date("2013-09-29T03:02:00.000-07:00")); | ||
}); | ||
it("works for 2H", function () { | ||
var p2h = Duration.fromJS("PT2H"); | ||
chai_1.expect(p2h.floor(new Date("2013-09-29T03:02:03.456-07:00"), tz)).to.deep.equal(new Date("2013-09-29T02:00:00.000-07:00")); | ||
it("works for P2H", function () { | ||
var pt2h = Duration.fromJS("PT2H"); | ||
chai_1.expect(pt2h.floor(new Date("2013-09-29T03:02:03.456-07:00"), TZ_LA)).to.deep.equal(new Date("2013-09-29T02:00:00.000-07:00")); | ||
}); | ||
it("works for 1W", function () { | ||
it("works for PT12H", function () { | ||
var pt12h = Duration.fromJS("PT12H"); | ||
chai_1.expect(pt12h.floor(new Date("2015-09-12T13:05:00-08:00"), TZ_JUNEAU)).to.deep.equal(new Date("2015-09-12T12:00:00-08:00")); | ||
}); | ||
it("works for P1W", function () { | ||
var p1w = Duration.fromJS("P1W"); | ||
chai_1.expect(p1w.floor(new Date("2013-09-29T01:02:03.456-07:00"), tz)) | ||
chai_1.expect(p1w.floor(new Date("2013-09-29T01:02:03.456-07:00"), TZ_LA)) | ||
.to.deep.equal(new Date("2013-09-23T07:00:00.000Z")); | ||
chai_1.expect(p1w.floor(new Date("2013-10-03T01:02:03.456-07:00"), tz)) | ||
chai_1.expect(p1w.floor(new Date("2013-10-03T01:02:03.456-07:00"), TZ_LA)) | ||
.to.deep.equal(new Date("2013-09-30T00:00:00.000-07:00")); | ||
}); | ||
it("works for 3M", function () { | ||
it("works for P3M", function () { | ||
var p3m = Duration.fromJS("P3M"); | ||
chai_1.expect(p3m.floor(new Date("2013-09-29T03:02:03.456-07:00"), tz)).to.deep.equal(new Date("2013-07-01T00:00:00.000-07:00")); | ||
chai_1.expect(p3m.floor(new Date("2013-02-29T03:02:03.456-07:00"), tz)).to.deep.equal(new Date("2013-01-01T00:00:00.000-08:00")); | ||
chai_1.expect(p3m.floor(new Date("2013-09-29T03:02:03.456-07:00"), TZ_LA)).to.deep.equal(new Date("2013-07-01T00:00:00.000-07:00")); | ||
chai_1.expect(p3m.floor(new Date("2013-02-29T03:02:03.456-07:00"), TZ_LA)).to.deep.equal(new Date("2013-01-01T00:00:00.000-08:00")); | ||
}); | ||
it("works for 4Y", function () { | ||
it("works for P4Y", function () { | ||
var p4y = Duration.fromJS("P4Y"); | ||
chai_1.expect(p4y.floor(new Date("2013-09-29T03:02:03.456-07:00"), tz)).to.deep.equal(new Date("2012-01-01T00:00:00.000-08:00")); | ||
chai_1.expect(p4y.floor(new Date("2013-09-29T03:02:03.456-07:00"), TZ_LA)).to.deep.equal(new Date("2012-01-01T00:00:00.000-08:00")); | ||
}); | ||
@@ -112,13 +133,55 @@ }); | ||
p1w = Duration.fromJS("P1W"); | ||
chai_1.expect(p1w.shift(new Date("2012-10-29T00:00:00-07:00"), tz)).to.deep.equal(new Date("2012-11-05T00:00:00-08:00")); | ||
chai_1.expect(p1w.shift(new Date("2012-10-29T00:00:00-07:00"), TZ_LA)).to.deep.equal(new Date("2012-11-05T00:00:00-08:00")); | ||
p1w = Duration.fromJS("P1W"); | ||
chai_1.expect(p1w.shift(new Date("2012-10-29T00:00:00-07:00"), tz, 2)).to.deep.equal(new Date("2012-11-12T00:00:00-08:00")); | ||
chai_1.expect(p1w.shift(new Date("2012-10-29T00:00:00-07:00"), TZ_LA, 2)).to.deep.equal(new Date("2012-11-12T00:00:00-08:00")); | ||
p2w = Duration.fromJS("P2W"); | ||
chai_1.expect(p2w.shift(new Date("2012-10-29T05:16:17-07:00"), tz)).to.deep.equal(new Date("2012-11-12T05:16:17-08:00")); | ||
chai_1.expect(p2w.shift(new Date("2012-10-29T05:16:17-07:00"), TZ_LA)).to.deep.equal(new Date("2012-11-12T05:16:17-08:00")); | ||
}); | ||
it("works for general complex case", function () { | ||
var pComplex = Duration.fromJS("P1Y2M3DT4H5M6S"); | ||
chai_1.expect(pComplex.shift(new Date("2012-01-01T00:00:00-08:00"), tz)).to.deep.equal(new Date("2013-03-04T04:05:06-08:00")); | ||
chai_1.expect(pComplex.shift(new Date("2012-01-01T00:00:00-08:00"), TZ_LA)).to.deep.equal(new Date("2013-03-04T04:05:06-08:00")); | ||
}); | ||
}); | ||
describe("#materialize", function () { | ||
it("works for weeks", function () { | ||
var p1w = Duration.fromJS("P1W"); | ||
chai_1.expect(p1w.materialize(new Date("2012-10-29T00:00:00-07:00"), new Date("2012-12-01T00:00:00-08:00"), TZ_LA)).to.deep.equal([ | ||
new Date('2012-10-29T07:00:00.000Z'), | ||
new Date('2012-11-05T08:00:00.000Z'), | ||
new Date('2012-11-12T08:00:00.000Z'), | ||
new Date('2012-11-19T08:00:00.000Z'), | ||
new Date('2012-11-26T08:00:00.000Z') | ||
]); | ||
chai_1.expect(p1w.materialize(new Date("2012-10-29T00:00:00-07:00"), new Date("2012-12-01T00:00:00-08:00"), TZ_LA, 2)).to.deep.equal([ | ||
new Date('2012-10-29T07:00:00.000Z'), | ||
new Date('2012-11-12T08:00:00.000Z'), | ||
new Date('2012-11-26T08:00:00.000Z') | ||
]); | ||
}); | ||
}); | ||
describe("#isAligned", function () { | ||
it("works for weeks", function () { | ||
var p1w = Duration.fromJS("P1W"); | ||
chai_1.expect(p1w.isAligned(new Date("2012-10-29T00:00:00-07:00"), TZ_LA)).to.equal(true); | ||
chai_1.expect(p1w.isAligned(new Date("2012-10-29T00:00:00-07:00"), Timezone.UTC)).to.equal(false); | ||
}); | ||
}); | ||
describe("#dividesBy", function () { | ||
it("works for true", function () { | ||
var vs = 'P5Y/P1Y P1D/P1D P1M/P1D P1W/P1D P1D/PT6H PT3H/PT1H'.split(' '); | ||
for (var _i = 0, vs_3 = vs; _i < vs_3.length; _i++) { | ||
var v = vs_3[_i]; | ||
var p = v.split('/'); | ||
chai_1.expect(Duration.fromJS(p[0]).dividesBy(Duration.fromJS(p[1])), v).to.equal(true); | ||
} | ||
}); | ||
it("works for false", function () { | ||
var vs = 'P1D/P1M PT5H/PT1H'.split(' '); | ||
for (var _i = 0, vs_4 = vs; _i < vs_4.length; _i++) { | ||
var v = vs_4[_i]; | ||
var p = v.split('/'); | ||
chai_1.expect(Duration.fromJS(p[0]).dividesBy(Duration.fromJS(p[1])), v).to.equal(false); | ||
} | ||
}); | ||
}); | ||
describe("#getCanonicalLength", function () { | ||
@@ -125,0 +188,0 @@ it("gives back the correct canonical length", function () { |
@@ -22,3 +22,4 @@ /// <reference path="../typings/mocha/mocha.d.ts" /> | ||
describe("Duration", () => { | ||
var tz = Timezone.fromJS("America/Los_Angeles"); | ||
var TZ_LA = Timezone.fromJS("America/Los_Angeles"); | ||
var TZ_JUNEAU = Timezone.fromJS("America/Juneau"); | ||
@@ -45,5 +46,5 @@ it("is an immutable class", () => { | ||
expect(() => Duration.fromJS("P0W").shift(new Date(), tz)).to.throw(Error, "Duration can not be empty"); | ||
expect(() => Duration.fromJS("P0W").shift(new Date(), TZ_LA)).to.throw(Error, "Duration can not be empty"); | ||
expect(() => Duration.fromJS("P0Y0MT0H0M0S").shift(new Date(), tz)).to.throw(Error, "Duration can not be empty"); | ||
expect(() => Duration.fromJS("P0Y0MT0H0M0S").shift(new Date(), TZ_LA)).to.throw(Error, "Duration can not be empty"); | ||
}); | ||
@@ -88,3 +89,3 @@ | ||
new Date("2012-11-05T00:00:00-08:00"), | ||
tz | ||
TZ_LA | ||
).toString()).to.equal("P7D"); | ||
@@ -95,3 +96,3 @@ | ||
new Date("2012-11-12T00:00:00-08:00"), | ||
tz | ||
TZ_LA | ||
).toString()).to.equal("P14D"); | ||
@@ -104,3 +105,3 @@ }); | ||
new Date(new Date("2012-11-05T00:00:00-08:00").valueOf() - 1000), | ||
tz | ||
TZ_LA | ||
).toString()).to.equal("P6DT23H59M59S"); | ||
@@ -111,3 +112,3 @@ | ||
new Date("2013-03-04T04:05:06-08:00"), | ||
tz | ||
TZ_LA | ||
).toString()).to.equal("P1Y2M3DT4H5M6S"); | ||
@@ -117,9 +118,25 @@ }); | ||
describe("#isFloorable", () => { | ||
it("works on floorable things", () => { | ||
var vs = 'P1Y P5Y P10Y P100Y P1M P2M P3M P4M P1D'.split(' '); | ||
for (var v of vs) { | ||
expect(Duration.fromJS(v).isFloorable(), v).to.equal(true); | ||
} | ||
}); | ||
it("works on not floorable things", () => { | ||
var vs = 'P1Y1M P5M P2D P3D'.split(' '); | ||
for (var v of vs) { | ||
expect(Duration.fromJS(v).isFloorable(), v).to.equal(false); | ||
} | ||
}); | ||
}); | ||
describe("#floor", () => { | ||
it("throws error if complex duration", () => { | ||
expect(() => Duration.fromJS("P1Y2D").floor(new Date(), tz)).to.throw(Error, "Can not floor on a complex duration"); | ||
expect(() => Duration.fromJS("P1Y2D").floor(new Date(), TZ_LA)).to.throw(Error, "Can not floor on a complex duration"); | ||
expect(() => Duration.fromJS("P3DT15H").floor(new Date(), tz)).to.throw(Error, "Can not floor on a complex duration"); | ||
expect(() => Duration.fromJS("P3DT15H").floor(new Date(), TZ_LA)).to.throw(Error, "Can not floor on a complex duration"); | ||
expect(() => Duration.fromJS("PT5H").floor(new Date(), tz)).to.throw(Error, "Can not floor on a hour duration that is not a multiple of 5"); | ||
expect(() => Duration.fromJS("PT5H").floor(new Date(), TZ_LA)).to.throw(Error, "Can not floor on a hour duration that is not a multiple of 5"); | ||
}); | ||
@@ -129,36 +146,42 @@ | ||
var p1y = Duration.fromJS("P1Y"); | ||
expect(p1y.floor(new Date("2013-09-29T01:02:03.456-07:00"), tz)).to.deep.equal(new Date("2013-01-01T00:00:00.000-08:00")); | ||
expect(p1y.floor(new Date("2013-09-29T01:02:03.456-07:00"), TZ_LA)).to.deep.equal(new Date("2013-01-01T00:00:00.000-08:00")); | ||
}); | ||
it("works for T2M", () => { | ||
var p2h = Duration.fromJS("PT2M"); | ||
expect(p2h.floor(new Date("2013-09-29T03:03:03.456-07:00"), tz)).to.deep.equal(new Date("2013-09-29T03:02:00.000-07:00")); | ||
it("works for PT2M", () => { | ||
var pt2h = Duration.fromJS("PT2M"); | ||
expect(pt2h.floor(new Date("2013-09-29T03:03:03.456-07:00"), TZ_LA)).to.deep.equal(new Date("2013-09-29T03:02:00.000-07:00")); | ||
}); | ||
it("works for 2H", () => { | ||
var p2h = Duration.fromJS("PT2H"); | ||
expect(p2h.floor(new Date("2013-09-29T03:02:03.456-07:00"), tz)).to.deep.equal(new Date("2013-09-29T02:00:00.000-07:00")); | ||
it("works for P2H", () => { | ||
var pt2h = Duration.fromJS("PT2H"); | ||
expect(pt2h.floor(new Date("2013-09-29T03:02:03.456-07:00"), TZ_LA)).to.deep.equal(new Date("2013-09-29T02:00:00.000-07:00")); | ||
}); | ||
it("works for 1W", () => { | ||
it("works for PT12H", () => { | ||
var pt12h = Duration.fromJS("PT12H"); | ||
expect(pt12h.floor(new Date("2015-09-12T13:05:00-08:00"), TZ_JUNEAU)).to.deep.equal(new Date("2015-09-12T12:00:00-08:00")); | ||
}); | ||
it("works for P1W", () => { | ||
var p1w = Duration.fromJS("P1W"); | ||
expect(p1w.floor(new Date("2013-09-29T01:02:03.456-07:00"), tz)) | ||
expect(p1w.floor(new Date("2013-09-29T01:02:03.456-07:00"), TZ_LA)) | ||
.to.deep.equal(new Date("2013-09-23T07:00:00.000Z")); | ||
expect(p1w.floor(new Date("2013-10-03T01:02:03.456-07:00"), tz)) | ||
expect(p1w.floor(new Date("2013-10-03T01:02:03.456-07:00"), TZ_LA)) | ||
.to.deep.equal(new Date("2013-09-30T00:00:00.000-07:00")); | ||
}); | ||
it("works for 3M", () => { | ||
it("works for P3M", () => { | ||
var p3m = Duration.fromJS("P3M"); | ||
expect(p3m.floor(new Date("2013-09-29T03:02:03.456-07:00"), tz)).to.deep.equal(new Date("2013-07-01T00:00:00.000-07:00")); | ||
expect(p3m.floor(new Date("2013-09-29T03:02:03.456-07:00"), TZ_LA)).to.deep.equal(new Date("2013-07-01T00:00:00.000-07:00")); | ||
expect(p3m.floor(new Date("2013-02-29T03:02:03.456-07:00"), tz)).to.deep.equal(new Date("2013-01-01T00:00:00.000-08:00")); | ||
expect(p3m.floor(new Date("2013-02-29T03:02:03.456-07:00"), TZ_LA)).to.deep.equal(new Date("2013-01-01T00:00:00.000-08:00")); | ||
}); | ||
it("works for 4Y", () => { | ||
it("works for P4Y", () => { | ||
var p4y = Duration.fromJS("P4Y"); | ||
expect(p4y.floor(new Date("2013-09-29T03:02:03.456-07:00"), tz)).to.deep.equal(new Date("2012-01-01T00:00:00.000-08:00")); | ||
expect(p4y.floor(new Date("2013-09-29T03:02:03.456-07:00"), TZ_LA)).to.deep.equal(new Date("2012-01-01T00:00:00.000-08:00")); | ||
}); | ||
}); | ||
@@ -170,9 +193,9 @@ | ||
p1w = Duration.fromJS("P1W"); | ||
expect(p1w.shift(new Date("2012-10-29T00:00:00-07:00"), tz)).to.deep.equal(new Date("2012-11-05T00:00:00-08:00")); | ||
expect(p1w.shift(new Date("2012-10-29T00:00:00-07:00"), TZ_LA)).to.deep.equal(new Date("2012-11-05T00:00:00-08:00")); | ||
p1w = Duration.fromJS("P1W"); | ||
expect(p1w.shift(new Date("2012-10-29T00:00:00-07:00"), tz, 2)).to.deep.equal(new Date("2012-11-12T00:00:00-08:00")); | ||
expect(p1w.shift(new Date("2012-10-29T00:00:00-07:00"), TZ_LA, 2)).to.deep.equal(new Date("2012-11-12T00:00:00-08:00")); | ||
p2w = Duration.fromJS("P2W"); | ||
expect(p2w.shift(new Date("2012-10-29T05:16:17-07:00"), tz)).to.deep.equal(new Date("2012-11-12T05:16:17-08:00")); | ||
expect(p2w.shift(new Date("2012-10-29T05:16:17-07:00"), TZ_LA)).to.deep.equal(new Date("2012-11-12T05:16:17-08:00")); | ||
}); | ||
@@ -182,6 +205,55 @@ | ||
var pComplex = Duration.fromJS("P1Y2M3DT4H5M6S"); | ||
expect(pComplex.shift(new Date("2012-01-01T00:00:00-08:00"), tz)).to.deep.equal(new Date("2013-03-04T04:05:06-08:00")); | ||
expect(pComplex.shift(new Date("2012-01-01T00:00:00-08:00"), TZ_LA)).to.deep.equal(new Date("2013-03-04T04:05:06-08:00")); | ||
}); | ||
}); | ||
describe("#materialize", () => { | ||
it("works for weeks", () => { | ||
var p1w = Duration.fromJS("P1W"); | ||
expect(p1w.materialize(new Date("2012-10-29T00:00:00-07:00"), new Date("2012-12-01T00:00:00-08:00"), TZ_LA)).to.deep.equal([ | ||
new Date('2012-10-29T07:00:00.000Z'), | ||
new Date('2012-11-05T08:00:00.000Z'), | ||
new Date('2012-11-12T08:00:00.000Z'), | ||
new Date('2012-11-19T08:00:00.000Z'), | ||
new Date('2012-11-26T08:00:00.000Z') | ||
]); | ||
expect(p1w.materialize(new Date("2012-10-29T00:00:00-07:00"), new Date("2012-12-01T00:00:00-08:00"), TZ_LA, 2)).to.deep.equal([ | ||
new Date('2012-10-29T07:00:00.000Z'), | ||
new Date('2012-11-12T08:00:00.000Z'), | ||
new Date('2012-11-26T08:00:00.000Z') | ||
]); | ||
}); | ||
}); | ||
describe("#isAligned", () => { | ||
it("works for weeks", () => { | ||
var p1w = Duration.fromJS("P1W"); | ||
expect(p1w.isAligned(new Date("2012-10-29T00:00:00-07:00"), TZ_LA)).to.equal(true); | ||
expect(p1w.isAligned(new Date("2012-10-29T00:00:00-07:00"), Timezone.UTC)).to.equal(false); | ||
}); | ||
}); | ||
describe("#dividesBy", () => { | ||
it("works for true", () => { | ||
var vs = 'P5Y/P1Y P1D/P1D P1M/P1D P1W/P1D P1D/PT6H PT3H/PT1H'.split(' '); | ||
for (var v of vs) { | ||
var p = v.split('/'); | ||
expect(Duration.fromJS(p[0]).dividesBy(Duration.fromJS(p[1])), v).to.equal(true); | ||
} | ||
}); | ||
it("works for false", () => { | ||
var vs = 'P1D/P1M PT5H/PT1H'.split(' '); | ||
for (var v of vs) { | ||
var p = v.split('/'); | ||
expect(Duration.fromJS(p[0]).dividesBy(Duration.fromJS(p[1])), v).to.equal(false); | ||
} | ||
}); | ||
}); | ||
describe("#getCanonicalLength", () => { | ||
@@ -188,0 +260,0 @@ it("gives back the correct canonical length", () => { |
Sorry, the diff of this file is too big to display
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
875697
9386
45