Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chronoshift

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chronoshift - npm Package Compare versions

Comparing version 0.3.7 to 0.4.1

script/publish

48

build/chronoshift.d.ts

@@ -27,5 +27,2 @@ /// <reference path="../typings/immutable-class.d.ts" />

static fromJS(spec: string): Timezone;
/**
* Constructs a timezone form the string representation by checking that it is defined
*/
constructor(timezone: string);

@@ -44,3 +41,3 @@ valueOf(): string;

}
interface MoveFn {
interface ShiftFn {
(dt: Date, tz: Timezone, step: number): Date;

@@ -51,3 +48,3 @@ }

}
interface TimeMover {
interface TimeShifter {
canonicalLength: number;

@@ -57,13 +54,15 @@ siblings?: number;

round?: RoundFn;
move: MoveFn;
shift: ShiftFn;
ceil?: AlignFn;
move?: ShiftFn;
}
var second: TimeMover;
var minute: TimeMover;
var hour: TimeMover;
var day: TimeMover;
var week: TimeMover;
var month: TimeMover;
var year: TimeMover;
var movers: Lookup<TimeMover>;
var second: TimeShifter;
var minute: TimeShifter;
var hour: TimeShifter;
var day: TimeShifter;
var week: TimeShifter;
var month: TimeShifter;
var year: TimeShifter;
var shifters: Lookup<TimeShifter>;
var movers: Lookup<TimeShifter>;
}

@@ -87,5 +86,2 @@ declare module Chronoshift {

static isDuration(candidate: any): boolean;
/**
* Constructs an ISO duration like P1DT3H from a string
*/
constructor(spans: DurationValue);

@@ -102,21 +98,13 @@ constructor(start: Date, end: Date, timezone: Timezone);

isFloorable(): boolean;
/**
* Floors the date according to this duration.
* @param date The date to floor
* @param timezone The timezone within which to floor
*/
floor(date: Date, timezone: Timezone): Date;
/**
* Moves the given date by 'step' times of the duration
* Negative step value will move back in time.
* @param date The date to move
* @param timezone The timezone within which to make the move
* @param step The number of times to step by the duration
*/
shift(date: Date, timezone: Timezone, step?: number): Date;
move(date: Date, timezone: Timezone, step?: number): Date;
getCanonicalLength(): number;
canonicalLength(): number;
getDescription(): string;
}
}
declare module Chronoshift {
function parseSQLDate(type: string, v: string): Date;
function parseISODate(date: string, timezone?: Timezone): Date;
}

@@ -123,0 +111,0 @@ declare var chronoshift: typeof Chronoshift;

@@ -1,2 +0,1 @@

/// <reference path="../typings/immutable-class.d.ts" />
"use strict";

@@ -16,10 +15,4 @@ var Chronoshift;

(function (Chronoshift) {
/**
* Represents timezones
*/
var check;
var Timezone = (function () {
/**
* Constructs a timezone form the string representation by checking that it is defined
*/
function Timezone(timezone) {

@@ -30,3 +23,3 @@ if (typeof timezone !== 'string') {

if (timezone !== 'Etc/UTC') {
Chronoshift.WallTime.UTCToWallTime(new Date(0), timezone); // This will throw an error if timezone is not a real timezone
Chronoshift.WallTime.UTCToWallTime(new Date(0), timezone);
}

@@ -61,3 +54,3 @@ this.timezone = timezone;

return Timezone;
})();
}());
Chronoshift.Timezone = Timezone;

@@ -72,17 +65,17 @@ check = Timezone;

}
function timeMoverFiller(tm) {
var floor = tm.floor, move = tm.move;
function timeShifterFiller(tm) {
var floor = tm.floor, shift = tm.shift;
tm.ceil = function (dt, tz) {
var floored = floor(dt, tz);
if (floored.valueOf() === dt.valueOf())
return dt; // Just like ceil(3) is 3 and not 4
return move(floored, tz, 1);
return dt;
return shift(floored, tz, 1);
};
tm.move = tm.shift;
return tm;
}
Chronoshift.second = timeMoverFiller({
Chronoshift.second = timeShifterFiller({
canonicalLength: 1000,
siblings: 60,
floor: function (dt, tz) {
// Seconds do not actually need a timezone because all timezones align on seconds... for now...
dt = new Date(dt.valueOf());

@@ -99,3 +92,3 @@ dt.setUTCMilliseconds(0);

},
move: function (dt, tz, step) {
shift: function (dt, tz, step) {
dt = new Date(dt.valueOf());

@@ -106,7 +99,6 @@ dt.setUTCSeconds(dt.getUTCSeconds() + step);

});
Chronoshift.minute = timeMoverFiller({
Chronoshift.minute = timeShifterFiller({
canonicalLength: 60000,
siblings: 60,
floor: function (dt, tz) {
// Minutes do not actually need a timezone because all timezones align on minutes... for now...
dt = new Date(dt.valueOf());

@@ -123,3 +115,3 @@ dt.setUTCSeconds(0, 0);

},
move: function (dt, tz, step) {
shift: function (dt, tz, step) {
dt = new Date(dt.valueOf());

@@ -141,3 +133,3 @@ dt.setUTCMinutes(dt.getUTCMinutes() + step);

}
Chronoshift.hour = timeMoverFiller({
Chronoshift.hour = timeShifterFiller({
canonicalLength: 3600000,

@@ -171,5 +163,5 @@ siblings: 24,

},
move: hourMove
shift: hourMove
});
Chronoshift.day = timeMoverFiller({
Chronoshift.day = timeShifterFiller({
canonicalLength: 24 * 3600000,

@@ -187,3 +179,3 @@ floor: function (dt, tz) {

},
move: function (dt, tz, step) {
shift: function (dt, tz, step) {
if (tz.isUTC()) {

@@ -200,3 +192,3 @@ dt = new Date(dt.valueOf());

});
Chronoshift.week = timeMoverFiller({
Chronoshift.week = timeShifterFiller({
canonicalLength: 7 * 24 * 3600000,

@@ -215,3 +207,3 @@ floor: function (dt, tz) {

},
move: function (dt, tz, step) {
shift: function (dt, tz, step) {
if (tz.isUTC()) {

@@ -228,3 +220,3 @@ dt = new Date(dt.valueOf());

});
function monthMove(dt, tz, step) {
function monthShift(dt, tz, step) {
if (tz.isUTC()) {

@@ -240,3 +232,3 @@ dt = new Date(dt.valueOf());

}
Chronoshift.month = timeMoverFiller({
Chronoshift.month = timeShifterFiller({
canonicalLength: 30 * 24 * 3600000,

@@ -267,9 +259,9 @@ siblings: 12,

if (cur !== adj)
return monthMove(dt, tz, adj - cur);
return monthShift(dt, tz, adj - cur);
}
return dt;
},
move: monthMove
shift: monthShift
});
function yearMove(dt, tz, step) {
function yearShift(dt, tz, step) {
if (tz.isUTC()) {

@@ -285,3 +277,3 @@ dt = new Date(dt.valueOf());

}
Chronoshift.year = timeMoverFiller({
Chronoshift.year = timeShifterFiller({
canonicalLength: 365 * 24 * 3600000,

@@ -312,9 +304,9 @@ siblings: 1000,

if (cur !== adj)
return yearMove(dt, tz, adj - cur);
return yearShift(dt, tz, adj - cur);
}
return dt;
},
move: yearMove
shift: yearShift
});
Chronoshift.movers = {
Chronoshift.shifters = {
second: Chronoshift.second,

@@ -328,2 +320,3 @@ minute: Chronoshift.minute,

};
Chronoshift.movers = Chronoshift.shifters;
})(Chronoshift || (Chronoshift = {}));

@@ -334,6 +327,4 @@ var Chronoshift;

var spansWithoutWeek = ["year", "month", "day", "hour", "minute", "second"];
var spanMultiplicity = {};
var periodWeekRegExp = /^P(\d+)W$/;
var periodRegExp = /^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/;
// P (year ) (month ) (day ) T(hour ) (minute ) (second )
function getSpansFromString(durationStr) {

@@ -371,5 +362,4 @@ var spans = {};

var spanCount = 0;
// Shortcut
var length = end.valueOf() - iterator.valueOf();
var canonicalLength = Chronoshift.movers[span].canonicalLength;
var canonicalLength = Chronoshift.shifters[span].canonicalLength;
if (length < canonicalLength / 4)

@@ -380,4 +370,3 @@ continue;

if (numberToFit > 0) {
// try to skip by numberToFit
iteratorMove = Chronoshift.movers[span].move(iterator, timezone, numberToFit);
iteratorMove = Chronoshift.shifters[span].shift(iterator, timezone, numberToFit);
if (iteratorMove <= end) {

@@ -389,3 +378,3 @@ spanCount += numberToFit;

while (true) {
iteratorMove = Chronoshift.movers[span].move(iterator, timezone, 1);
iteratorMove = Chronoshift.shifters[span].shift(iterator, timezone, 1);
if (iteratorMove <= end) {

@@ -415,5 +404,2 @@ iterator = iteratorMove;

}
/**
* Represents an ISO duration like P1DT3H
*/
var check;

@@ -451,3 +437,3 @@ var Duration = (function () {

var span = spansWithWeek[i];
var spanLength = Chronoshift.movers[span].canonicalLength;
var spanLength = Chronoshift.shifters[span].canonicalLength;
var count = Math.floor(length / spanLength);

@@ -519,3 +505,3 @@ length -= spanLength * count;

return true;
var siblings = Chronoshift.movers[singleSpan].siblings;
var siblings = Chronoshift.shifters[singleSpan].siblings;
if (!siblings)

@@ -525,7 +511,2 @@ return false;

};
/**
* Floors the date according to this duration.
* @param date The date to floor
* @param timezone The timezone within which to floor
*/
Duration.prototype.floor = function (date, timezone) {

@@ -536,3 +517,3 @@ var singleSpan = this.singleSpan;

var span = this.spans[singleSpan];
var mover = Chronoshift.movers[singleSpan];
var mover = Chronoshift.shifters[singleSpan];
var dt = mover.floor(date, timezone);

@@ -548,41 +529,34 @@ if (span !== 1) {

};
/**
* Moves the given date by 'step' times of the duration
* Negative step value will move back in time.
* @param date The date to move
* @param timezone The timezone within which to make the move
* @param step The number of times to step by the duration
*/
Duration.prototype.move = function (date, timezone, step) {
Duration.prototype.shift = function (date, timezone, step) {
if (step === void 0) { step = 1; }
var spans = this.spans;
for (var _i = 0; _i < spansWithWeek.length; _i++) {
var span = spansWithWeek[_i];
for (var _i = 0, spansWithWeek_1 = spansWithWeek; _i < spansWithWeek_1.length; _i++) {
var span = spansWithWeek_1[_i];
var value = spans[span];
if (value)
date = Chronoshift.movers[span].move(date, timezone, step * value);
date = Chronoshift.shifters[span].shift(date, timezone, step * value);
}
return date;
};
Duration.prototype.move = function (date, 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);
};
Duration.prototype.getCanonicalLength = function () {
var spans = this.spans;
var length = 0;
for (var _i = 0; _i < spansWithWeek.length; _i++) {
var span = spansWithWeek[_i];
for (var _i = 0, spansWithWeek_2 = spansWithWeek; _i < spansWithWeek_2.length; _i++) {
var span = spansWithWeek_2[_i];
var value = spans[span];
if (value)
length += value * Chronoshift.movers[span].canonicalLength;
length += value * Chronoshift.shifters[span].canonicalLength;
}
return length;
};
Duration.prototype.canonicalLength = function () {
// This method is deprecated
console.warn("The method 'canonicalLength()' is deprecated. Please use 'getCanonicalLength()' instead.");
return this.getCanonicalLength();
};
Duration.prototype.getDescription = function () {
var spans = this.spans;
var description = [];
for (var _i = 0; _i < spansWithWeek.length; _i++) {
var span = spansWithWeek[_i];
for (var _i = 0, spansWithWeek_3 = spansWithWeek; _i < spansWithWeek_3.length; _i++) {
var span = spansWithWeek_3[_i];
var value = spans[span];

@@ -601,8 +575,122 @@ if (value) {

return Duration;
})();
}());
Chronoshift.Duration = Duration;
check = Duration;
})(Chronoshift || (Chronoshift = {}));
var Chronoshift;
(function (Chronoshift) {
function parseYear(v) {
if (v.length === 2) {
var vn = parseInt(v, 10);
return (vn < 70 ? 2000 : 1900) + vn;
}
else if (v.length === 4) {
return parseInt(v, 10);
}
else {
throw new Error('Invalid year in date');
}
}
function parseMonth(v) {
var vn = parseInt(v, 10);
if (vn <= 0 || 12 < vn)
throw new Error('Invalid month in date');
return vn - 1;
}
function parseDay(v) {
var vn = parseInt(v, 10);
if (vn <= 0 || 31 < vn)
throw new Error('Invalid day in date');
return vn;
}
function parseHour(v) {
var vn = parseInt(v, 10);
if (vn < 0 || 24 < vn)
throw new Error('Invalid hour in date');
return vn;
}
function parseMinute(v) {
var vn = parseInt(v, 10);
if (vn < 0 || 60 < vn)
throw new Error('Invalid minute in date');
return vn;
}
function parseSecond(v) {
var vn = parseInt(v, 10);
if (vn < 0 || 60 < vn)
throw new Error('Invalid second in date');
return vn;
}
function parseMillisecond(v) {
if (!v)
return 0;
return parseInt(v.substr(0, 3), 10);
}
function parseSQLDate(type, v) {
if (type === 't')
throw new Error('time literals are not supported');
var m;
var d;
if (type === 'ts') {
if (m = v.match(/^(\d{2}(?:\d{2})?)(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/)) {
d = Date.UTC(parseYear(m[1]), parseMonth(m[2]), parseDay(m[3]), parseHour(m[4]), parseMinute(m[5]), parseSecond(m[6]));
}
else if (m = v.match(/^(\d{2}(?:\d{2})?)[~!@#$%^&*()_+=:.\-\/](\d{1,2})[~!@#$%^&*()_+=:.\-\/](\d{1,2})[T ](\d{1,2})[~!@#$%^&*()_+=:.\-\/](\d{1,2})[~!@#$%^&*()_+=:.\-\/](\d{1,2})(?:\.(\d{1,6}))?$/)) {
d = Date.UTC(parseYear(m[1]), parseMonth(m[2]), parseDay(m[3]), parseHour(m[4]), parseMinute(m[5]), parseSecond(m[6]), parseMillisecond(m[7]));
}
else {
throw new Error('Invalid timestamp');
}
}
else {
if (m = v.match(/^(\d{2}(?:\d{2})?)(\d{2})(\d{2})$/)) {
d = Date.UTC(parseYear(m[1]), parseMonth(m[2]), parseDay(m[3]));
}
else if (m = v.match(/^(\d{2}(?:\d{2})?)[~!@#$%^&*()_+=:.\-\/](\d{1,2})[~!@#$%^&*()_+=:.\-\/](\d{1,2})$/)) {
d = Date.UTC(parseYear(m[1]), parseMonth(m[2]), parseDay(m[3]));
}
else {
throw new Error('Invalid date');
}
}
return new Date(d);
}
Chronoshift.parseSQLDate = parseSQLDate;
var numericKeys = [1, 4, 5, 6, 10, 11];
function parseISODate(date, timezone) {
if (timezone === void 0) { timezone = Chronoshift.Timezone.UTC; }
var struct, minutesOffset = 0;
if ((struct = /^(\d{4}|[+\-]\d{6})(?:-?(\d{2})(?:-?(\d{2}))?)?(?:[ T]?(\d{2})(?::?(\d{2})(?::?(\d{2})(?:[,\.](\d{1,}))?)?)?)?(?:(Z)|([+\-])(\d{2})(?::?(\d{2}))?)?$/.exec(date))) {
for (var i = 0, k; (k = numericKeys[i]); ++i) {
struct[k] = +struct[k] || 0;
}
struct[2] = (+struct[2] || 1) - 1;
struct[3] = +struct[3] || 1;
struct[7] = struct[7] ? +(struct[7] + "00").substr(0, 3) : 0;
if ((struct[8] === undefined || struct[8] === '') && (struct[9] === undefined || struct[9] === '') && !Chronoshift.Timezone.UTC.equals(timezone)) {
if (timezone === null) {
return new Date(struct[1], struct[2], struct[3], struct[4], struct[5], struct[6], struct[7]);
}
else {
return Chronoshift.WallTime.WallTimeToUTC(timezone.toString(), struct[1], struct[2], struct[3], struct[4], struct[5], struct[6], struct[7]);
}
}
else {
if (struct[8] !== 'Z' && struct[9] !== undefined) {
minutesOffset = struct[10] * 60 + struct[11];
if (struct[9] === '+') {
minutesOffset = 0 - minutesOffset;
}
}
return new Date(Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]));
}
}
else {
return null;
}
}
Chronoshift.parseISODate = parseISODate;
})(Chronoshift || (Chronoshift = {}));
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Chronoshift;
}
{
"name": "chronoshift",
"version": "0.3.7",
"version": "0.4.1",
"description": "A tiny library for shifting time with timezones",

@@ -10,3 +10,5 @@ "keywords": [

"time",
"range"
"range",
"IS08601",
"8601"
],

@@ -36,18 +38,18 @@ "author": {

"dependencies": {
"immutable-class": "^0.3.2"
"immutable-class": "0.4.3"
},
"devDependencies": {
"blanket": "^1.1.6",
"browserify": "~8.1.1",
"chai": "~2.3.0",
"event-stream": "^3.2.1",
"gulp": "^3.8.10",
"gulp-mocha": "^2.0.0",
"gulp-typescript": "^2.4.1",
"mocha": "~2.1.x",
"pegjs": "~0.8.0",
"run-sequence": "^1.0.2",
"tslint": "2.2.0-beta",
"typescript": "^1.5.3"
"blanket": "1.2.3",
"browserify": "13.0.0",
"chai": "3.5.0",
"event-stream": "3.3.2",
"gulp": "3.9.1",
"gulp-mocha": "2.2.0",
"gulp-typescript": "2.12.1",
"mocha": "2.4.5",
"pegjs": "0.9.0",
"run-sequence": "1.1.5",
"tslint": "3.6.0",
"typescript": "1.8.9"
}
}

@@ -7,3 +7,3 @@ # Chronoshift [![Dependency Status](https://david-dm.org/implyio/chronoshift.svg?theme=shields.io)](https://david-dm.org/implyio/chronoshift) [![devDependency Status](https://david-dm.org/implyio/chronoshift/dev-status.svg?theme=shields.io)](https://david-dm.org/implyio/chronoshift#info=devDependencies) [![Build Status](https://travis-ci.org/implyio/chronoshift.svg?branch=master)](https://travis-ci.org/implyio/chronoshift)

In node simply run: ```npm install chronoshift```
In node simply run: `npm install chronoshift`

@@ -31,6 +31,6 @@ In the browser you should use the browserified package/chronoshift.js

Use ```chronoshift[period][fn](date, timezone, moveAmount)``` where
Use `chronoshift[period][fn](date, timezone, moveAmount)` where
- ```period``` is one of ```['year', 'month', 'day', 'hour', 'minute', 'second', 'millisecond']```
- ```fn``` is one of ```['floor, ceil, move']```
- `period` is one of `['year', 'month', 'day', 'hour', 'minute', 'second', 'millisecond']`
- `fn` is one of `['floor, ceil, move']`

@@ -48,5 +48,5 @@ ```javascript

Construct a new duration with a [ISO Duration string](http://en.wikipedia.org/wiki/ISO_8601#Durations). Then you can call ```floor```, ```ceil``` and ```move``` on it.
Construct a new duration with a [ISO Duration string](http://en.wikipedia.org/wiki/ISO_8601#Durations). Then you can call `floor`, `ceil` and `move` on it.
Note that ```floor``` and ```ceil``` only work for 'simple' durations like "P1x" and "PT1x".
Note that `floor` and `ceil` only work for 'simple' durations like "P1x" and "PT1x".

@@ -105,1 +105,5 @@ ```javascript

```
## Questions & Support
Please file bugs and feature requests by opening and issue on GitHub and direct all questions to our [user groups](https://groups.google.com/forum/#!forum/imply-user-group).

@@ -5,6 +5,2 @@ module Chronoshift {

var spanMultiplicity = {
};
export interface DurationValue {

@@ -59,3 +55,3 @@ year?: number;

var length = end.valueOf() - iterator.valueOf();
var canonicalLength: number = movers[span].canonicalLength;
var canonicalLength: number = shifters[span].canonicalLength;
if (length < canonicalLength / 4) continue;

@@ -66,3 +62,3 @@ var numberToFit = Math.min(0, Math.floor(length / canonicalLength) - 1);

// try to skip by numberToFit
iteratorMove = movers[span].move(iterator, timezone, numberToFit);
iteratorMove = shifters[span].shift(iterator, timezone, numberToFit);
if (iteratorMove <= end) {

@@ -75,3 +71,3 @@ spanCount += numberToFit;

while (true) {
iteratorMove = movers[span].move(iterator, timezone, 1);
iteratorMove = shifters[span].shift(iterator, timezone, 1);
if (iteratorMove <= end) {

@@ -121,3 +117,3 @@ iterator = iteratorMove;

var span = spansWithWeek[i];
var spanLength = movers[span].canonicalLength;
var spanLength = shifters[span].canonicalLength;
var count = Math.floor(length / spanLength);

@@ -226,3 +222,3 @@

if (span === 1) return true;
var { siblings } = movers[singleSpan];
var { siblings } = shifters[singleSpan];
if (!siblings) return false;

@@ -241,3 +237,3 @@ return siblings % span === 0;

var span = this.spans[singleSpan];
var mover = movers[singleSpan];
var mover = shifters[singleSpan];
var dt = mover.floor(date, timezone);

@@ -259,7 +255,7 @@ if (span !== 1) {

*/
public move(date: Date, timezone: Timezone, step: number = 1) {
public shift(date: Date, timezone: Timezone, step: number = 1) {
var spans = this.spans;
for (let span of spansWithWeek) {
var value = spans[span];
if (value) date = movers[span].move(date, timezone, step * value);
if (value) date = shifters[span].shift(date, timezone, step * value);
}

@@ -269,2 +265,7 @@ return date;

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);
}
public getCanonicalLength(): number {

@@ -275,3 +276,3 @@ var spans = this.spans;

var value = spans[span];
if (value) length += value * movers[span].canonicalLength;
if (value) length += value * shifters[span].canonicalLength;
}

@@ -281,8 +282,2 @@ return length;

public canonicalLength(): number {
// This method is deprecated
console.warn("The method 'canonicalLength()' is deprecated. Please use 'getCanonicalLength()' instead.");
return this.getCanonicalLength();
}
public getDescription(): string {

@@ -289,0 +284,0 @@ var spans = this.spans;

/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />
/// <reference path="../build/chronoshift.d.ts" />
var chai = require("chai");
var expect = chai.expect;
"use strict";
var chai_1 = require("chai");
var ImmutableClassTesterModule = require("../node_modules/immutable-class/build/tester");

@@ -27,11 +28,11 @@ var testImmutableClass = ImmutableClassTesterModule.testImmutableClass;

it("throws error if invalid duration", function () {
expect(function () { return Duration.fromJS(""); }).to.throw(Error, "Can not parse duration ''");
expect(function () { return Duration.fromJS("P00"); }).to.throw(Error, "Can not parse duration 'P00'");
expect(function () { return Duration.fromJS("P"); }).to.throw(Error, "Duration can not be empty");
expect(function () { return Duration.fromJS("P0YT0H"); }).to.throw(Error, "Duration can not be empty");
expect(function () { return Duration.fromJS("P0W").move(new Date(), tz); }).to.throw(Error, "Duration can not be empty");
expect(function () { return Duration.fromJS("P0Y0MT0H0M0S").move(new Date(), tz); }).to.throw(Error, "Duration can not be empty");
chai_1.expect(function () { return Duration.fromJS(""); }).to.throw(Error, "Can not parse duration ''");
chai_1.expect(function () { return Duration.fromJS("P00"); }).to.throw(Error, "Can not parse duration 'P00'");
chai_1.expect(function () { return Duration.fromJS("P"); }).to.throw(Error, "Duration can not be empty");
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");
});
it("throws error if fromJS is not given a string", function () {
expect(function () { return Duration.fromJS((new Date())); }).to.throw(Error, "Duration JS must be a string");
chai_1.expect(function () { return Duration.fromJS((new Date())); }).to.throw(Error, "Duration JS must be a string");
});

@@ -43,12 +44,12 @@ });

durationStr = "P3Y";
expect(Duration.fromJS(durationStr).toString()).to.equal(durationStr);
chai_1.expect(Duration.fromJS(durationStr).toString()).to.equal(durationStr);
durationStr = "P2W";
expect(Duration.fromJS(durationStr).toString()).to.equal(durationStr);
chai_1.expect(Duration.fromJS(durationStr).toString()).to.equal(durationStr);
durationStr = "PT5H";
expect(Duration.fromJS(durationStr).toString()).to.equal(durationStr);
chai_1.expect(Duration.fromJS(durationStr).toString()).to.equal(durationStr);
durationStr = "P3DT15H";
expect(Duration.fromJS(durationStr).toString()).to.equal(durationStr);
chai_1.expect(Duration.fromJS(durationStr).toString()).to.equal(durationStr);
});
it("eliminates 0", function () {
expect(Duration.fromJS("P0DT15H").toString()).to.equal("PT15H");
chai_1.expect(Duration.fromJS("P0DT15H").toString()).to.equal("PT15H");
});

@@ -58,3 +59,3 @@ });

it("works", function () {
expect(Duration.fromCanonicalLength(86400000).toJS()).to.eql("P1D");
chai_1.expect(Duration.fromCanonicalLength(86400000).toJS()).to.eql("P1D");
});

@@ -64,8 +65,8 @@ });

it("parses days over DST", function () {
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");
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).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");
});
it("parses complex case", function () {
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");
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).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");
});

@@ -75,23 +76,23 @@ });

it("throws error if complex duration", function () {
expect(function () { return Duration.fromJS("P1Y2D").floor(new Date(), tz); }).to.throw(Error, "Can not floor on a complex duration");
expect(function () { return Duration.fromJS("P3DT15H").floor(new Date(), tz); }).to.throw(Error, "Can not floor on a complex duration");
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); }).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");
});
it("works for year", function () {
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"));
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"));
});
it("works for T2M", function () {
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"));
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 2H", function () {
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"));
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 1W", function () {
var p1w = Duration.fromJS("P1W");
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))
.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))
chai_1.expect(p1w.floor(new Date("2013-10-03T01:02:03.456-07:00"), tz))
.to.deep.equal(new Date("2013-09-30T00:00:00.000-07:00"));

@@ -101,23 +102,23 @@ });

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-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)).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"));
});
it("works for 4Y", function () {
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"));
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"));
});
});
describe("#move", function () {
describe("#shift", function () {
it("works for weeks", function () {
var p1w, p2w;
p1w = Duration.fromJS("P1W");
expect(p1w.move(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)).to.deep.equal(new Date("2012-11-05T00:00:00-08:00"));
p1w = Duration.fromJS("P1W");
expect(p1w.move(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, 2)).to.deep.equal(new Date("2012-11-12T00:00:00-08:00"));
p2w = Duration.fromJS("P2W");
expect(p2w.move(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)).to.deep.equal(new Date("2012-11-12T05:16:17-08:00"));
});
it("works for general complex case", function () {
var pComplex = Duration.fromJS("P1Y2M3DT4H5M6S");
expect(pComplex.move(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)).to.deep.equal(new Date("2013-03-04T04:05:06-08:00"));
});

@@ -129,9 +130,9 @@ });

durationStr = "P3Y";
expect(Duration.fromJS(durationStr).getCanonicalLength()).to.equal(94608000000);
chai_1.expect(Duration.fromJS(durationStr).getCanonicalLength()).to.equal(94608000000);
durationStr = "P2W";
expect(Duration.fromJS(durationStr).getCanonicalLength()).to.equal(1209600000);
chai_1.expect(Duration.fromJS(durationStr).getCanonicalLength()).to.equal(1209600000);
durationStr = "PT5H";
expect(Duration.fromJS(durationStr).getCanonicalLength()).to.equal(18000000);
chai_1.expect(Duration.fromJS(durationStr).getCanonicalLength()).to.equal(18000000);
durationStr = "P3DT15H";
expect(Duration.fromJS(durationStr).getCanonicalLength()).to.equal(313200000);
chai_1.expect(Duration.fromJS(durationStr).getCanonicalLength()).to.equal(313200000);
});

@@ -143,3 +144,3 @@ });

var d2 = Duration.fromJS("P1D");
expect(d1.add(d2).toJS()).to.eql("P2D");
chai_1.expect(d1.add(d2).toJS()).to.eql("P2D");
});

@@ -149,3 +150,3 @@ it("works with heterogeneous spans", function () {

var d2 = Duration.fromJS("P1Y");
expect(d1.add(d2).toJS()).to.eql("P1Y1D");
chai_1.expect(d1.add(d2).toJS()).to.eql("P1Y1D");
});

@@ -155,6 +156,6 @@ it("works with weeks", function () {

var d2 = Duration.fromJS("P2W");
expect(d1.add(d2).toJS()).to.eql("P3W");
chai_1.expect(d1.add(d2).toJS()).to.eql("P3W");
d1 = Duration.fromJS("P6D");
d2 = Duration.fromJS("P1D");
expect(d1.add(d2).toJS()).to.eql("P1W");
chai_1.expect(d1.add(d2).toJS()).to.eql("P1W");
});

@@ -166,3 +167,3 @@ });

var d2 = Duration.fromJS("PT1H");
expect(d1.subtract(d2).toJS()).to.eql("P1DT1H");
chai_1.expect(d1.subtract(d2).toJS()).to.eql("P1DT1H");
});

@@ -172,3 +173,3 @@ it("works with a less simple duration", function () {

var d2 = Duration.fromJS("PT1H");
expect(d1.subtract(d2).toJS()).to.eql("PT23H");
chai_1.expect(d1.subtract(d2).toJS()).to.eql("PT23H");
});

@@ -178,3 +179,3 @@ it("works with weeks", function () {

var d2 = Duration.fromJS("P1D");
expect(d1.subtract(d2).toJS()).to.eql("P6D");
chai_1.expect(d1.subtract(d2).toJS()).to.eql("P6D");
});

@@ -184,3 +185,3 @@ it("throws an error if result is going to be negative", function () {

var d2 = Duration.fromJS("P2D");
expect(function () { return d1.subtract(d2); }).to.throw();
chai_1.expect(function () { return d1.subtract(d2); }).to.throw();
});

@@ -192,13 +193,13 @@ });

durationStr = "P1D";
expect(Duration.fromJS(durationStr).getDescription()).to.equal('day');
chai_1.expect(Duration.fromJS(durationStr).getDescription()).to.equal('day');
durationStr = "P3Y";
expect(Duration.fromJS(durationStr).getDescription()).to.equal('3 years');
chai_1.expect(Duration.fromJS(durationStr).getDescription()).to.equal('3 years');
durationStr = "P2W";
expect(Duration.fromJS(durationStr).getDescription()).to.equal('2 weeks');
chai_1.expect(Duration.fromJS(durationStr).getDescription()).to.equal('2 weeks');
durationStr = "PT5H";
expect(Duration.fromJS(durationStr).getDescription()).to.equal('5 hours');
chai_1.expect(Duration.fromJS(durationStr).getDescription()).to.equal('5 hours');
durationStr = "P3DT15H";
expect(Duration.fromJS(durationStr).getDescription()).to.equal('3 days, 15 hours');
chai_1.expect(Duration.fromJS(durationStr).getDescription()).to.equal('3 days, 15 hours');
});
});
});
/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />
/// <reference path="../build/chronoshift.d.ts" />
import { expect } from "chai";
declare function require(file: string): any;
import chai = require("chai");
import expect = chai.expect;
import ImmutableClassTesterModule = require("../node_modules/immutable-class/build/tester");

@@ -44,5 +44,5 @@ import testImmutableClass = ImmutableClassTesterModule.testImmutableClass;

expect(() => Duration.fromJS("P0W").move(new Date(), tz)).to.throw(Error, "Duration can not be empty");
expect(() => Duration.fromJS("P0W").shift(new Date(), tz)).to.throw(Error, "Duration can not be empty");
expect(() => Duration.fromJS("P0Y0MT0H0M0S").move(new Date(), tz)).to.throw(Error, "Duration can not be empty");
expect(() => Duration.fromJS("P0Y0MT0H0M0S").shift(new Date(), tz)).to.throw(Error, "Duration can not be empty");
});

@@ -159,13 +159,13 @@

describe("#move", () => {
describe("#shift", () => {
it("works for weeks", () => {
var p1w, p2w;
p1w = Duration.fromJS("P1W");
expect(p1w.move(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)).to.deep.equal(new Date("2012-11-05T00:00:00-08:00"));
p1w = Duration.fromJS("P1W");
expect(p1w.move(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, 2)).to.deep.equal(new Date("2012-11-12T00:00:00-08:00"));
p2w = Duration.fromJS("P2W");
expect(p2w.move(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)).to.deep.equal(new Date("2012-11-12T05:16:17-08:00"));
});

@@ -175,3 +175,3 @@

var pComplex = Duration.fromJS("P1Y2M3DT4H5M6S");
expect(pComplex.move(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)).to.deep.equal(new Date("2013-03-04T04:05:06-08:00"));
});

@@ -178,0 +178,0 @@ });

/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />
/// <reference path="../build/chronoshift.d.ts" />
"use strict";
var chai = require("chai");
var expect = chai.expect;
var chai_1 = require("chai");
var chronoshift = require("../build/chronoshift");

@@ -10,10 +10,10 @@ var Timezone = chronoshift.Timezone;

it("can find Timezone", function () {
expect(Timezone.fromJS("America/Los_Angeles").toJS()).to.equal("America/Los_Angeles");
chai_1.expect(Timezone.fromJS("America/Los_Angeles").toJS()).to.equal("America/Los_Angeles");
});
describe("isDate", function () {
it("works", function () {
expect(chronoshift.isDate(new Date)).to.equal(true);
expect(chronoshift.isDate([])).to.equal(false);
chai_1.expect(chronoshift.isDate(new Date)).to.equal(true);
chai_1.expect(chronoshift.isDate([])).to.equal(false);
});
});
});
/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />
/// <reference path="../build/chronoshift.d.ts" />
"use strict";
import { expect } from "chai";
declare function require(file: string): any;
import chai = require("chai");
import expect = chai.expect;
import ImmutableClassTesterModule = require("../node_modules/immutable-class/build/tester");

@@ -11,0 +10,0 @@

/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />
/// <reference path="../build/chronoshift.d.ts" />
"use strict";
var chai = require("chai");
var expect = chai.expect;
var chai_1 = require("chai");
var ImmutableClassTesterModule = require("../node_modules/immutable-class/build/tester");

@@ -24,5 +24,5 @@ var testImmutableClass = ImmutableClassTesterModule.testImmutableClass;

it("throws error if invalid timezone", function () {
expect(function () { return new Timezone(""); }).to.throw(Error, "Unable to find time zone named <blank>");
expect(function () { return new Timezone("Blah/UTC"); }).to.throw(Error, "Unable to find time zone named Blah/UTC");
expect(function () { return new Timezone("America/Lost_Angeles"); }).to.throw(Error, "Unable to find time zone named America/Lost_Angeles");
chai_1.expect(function () { return new Timezone(""); }).to.throw(Error, "Unable to find time zone named <blank>");
chai_1.expect(function () { return new Timezone("Blah/UTC"); }).to.throw(Error, "Unable to find time zone named Blah/UTC");
chai_1.expect(function () { return new Timezone("America/Lost_Angeles"); }).to.throw(Error, "Unable to find time zone named America/Lost_Angeles");
});

@@ -33,10 +33,10 @@ });

var timezoneStr = "America/Los_Angeles";
expect(new Timezone(timezoneStr).toString()).to.equal(timezoneStr);
chai_1.expect(new Timezone(timezoneStr).toString()).to.equal(timezoneStr);
});
it("gives back the correct string for UTC", function () {
var timezoneStr = "Etc/UTC";
expect(new Timezone(timezoneStr).toString()).to.equal(timezoneStr);
chai_1.expect(new Timezone(timezoneStr).toString()).to.equal(timezoneStr);
});
it("gives back the correct string for inbuilt UTC", function () {
expect(Timezone.UTC.toString()).to.equal("Etc/UTC");
chai_1.expect(Timezone.UTC.toString()).to.equal("Etc/UTC");
});

@@ -47,5 +47,5 @@ });

var timezoneStr = "America/Los_Angeles";
expect(Timezone.isTimezone(new Timezone(timezoneStr))).to.equal(true);
chai_1.expect(Timezone.isTimezone(new Timezone(timezoneStr))).to.equal(true);
});
});
});
/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />
/// <reference path="../build/chronoshift.d.ts" />
"use strict";
import { expect } from "chai";
declare function require(file: string): any;
import chai = require("chai");
import expect = chai.expect;
import ImmutableClassTesterModule = require("../node_modules/immutable-class/build/tester");

@@ -11,0 +10,0 @@ import testImmutableClass = ImmutableClassTesterModule.testImmutableClass;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc