Skip to content

Commit

Permalink
fake getTimezoneOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
paed01 committed Aug 29, 2021
1 parent d9c3dc7 commit d5a2ae2
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 39 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"camelcase": 0,
"comma-spacing": 2,
"consistent-return": 0,
"constructor-super": 0,
"curly": [2, "multi-line"],
"dot-notation": [2, {
"allowKeywords": true
Expand Down
22 changes: 18 additions & 4 deletions dist/chronokinesis.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ var chronokinesis = (function (exports) {
* MIT License.
*/
var NativeDate = Date;
var nativeGetTimezoneOffset = NativeDate.prototype.getTimezoneOffset;
var freezedAt = null;
var traveledTo = null;
var started = null;
Expand All @@ -146,9 +147,21 @@ var chronokinesis = (function (exports) {
}

var length = args.length;
if (!length && freezedAt) return new NativeDate(freezedAt);
if (!length && traveledTo) return new NativeDate(time());
return instantiate(NativeDate, args);

if (!length) {
if (freezedAt) args = [freezedAt];else if (traveledTo) args = [time()];
}

var dt = instantiate(NativeDate, args);

dt.getTimezoneOffset = function getTimezoneOffset() {
var curr = nativeGetTimezoneOffset.call(this);
if (!iana) return curr;
var tz = timezone(iana).getTime(this);
return Math.round((tz - this.getTime()) / 60000) + curr;
};

return dt;
}

FakeDate.UTC = NativeDate.UTC;
Expand Down Expand Up @@ -233,6 +246,7 @@ var chronokinesis = (function (exports) {
defrost: defrost,
reset: reset,
isKeepingTime: isKeepingTime,
getTime: getTime,
freeze: freezeInTimezone,
travel: travelInTimezone
};
Expand Down Expand Up @@ -324,7 +338,7 @@ var chronokinesis = (function (exports) {
_iterator.f();
}

return NativeDate.UTC(year, month, day, hour, minute, second);
return NativeDate.UTC(year, month, day, hour, minute, second, dt.getMilliseconds());
}
var chronokinesis_1 = chronokinesis.freeze;
var chronokinesis_2 = chronokinesis.defrost;
Expand Down
22 changes: 18 additions & 4 deletions dist/index.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function _createForOfIteratorHelper(o, allowArrayLike) {
* MIT License.
*/
var NativeDate = Date;
var nativeGetTimezoneOffset = NativeDate.prototype.getTimezoneOffset;
var freezedAt = null;
var traveledTo = null;
var started = null;
Expand All @@ -143,9 +144,21 @@ function FakeDate() {
}

var length = args.length;
if (!length && freezedAt) return new NativeDate(freezedAt);
if (!length && traveledTo) return new NativeDate(time());
return instantiate(NativeDate, args);

if (!length) {
if (freezedAt) args = [freezedAt];else if (traveledTo) args = [time()];
}

var dt = instantiate(NativeDate, args);

dt.getTimezoneOffset = function getTimezoneOffset() {
var curr = nativeGetTimezoneOffset.call(this);
if (!iana) return curr;
var tz = timezone(iana).getTime(this);
return Math.round((tz - this.getTime()) / 60000) + curr;
};

return dt;
}

FakeDate.UTC = NativeDate.UTC;
Expand Down Expand Up @@ -230,6 +243,7 @@ function timezone(timeZone) {
defrost: defrost,
reset: reset,
isKeepingTime: isKeepingTime,
getTime: getTime,
freeze: freezeInTimezone,
travel: travelInTimezone
};
Expand Down Expand Up @@ -321,7 +335,7 @@ function toUTC(formatter, dt) {
_iterator.f();
}

return NativeDate.UTC(year, month, day, hour, minute, second);
return NativeDate.UTC(year, month, day, hour, minute, second, dt.getMilliseconds());
}
var chronokinesis_1 = chronokinesis.freeze;
var chronokinesis_2 = chronokinesis.defrost;
Expand Down
25 changes: 20 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'use strict';

const NativeDate = Date;
const nativeGetTimezoneOffset = NativeDate.prototype.getTimezoneOffset;

let freezedAt = null;
let traveledTo = null;
Expand All @@ -15,11 +16,22 @@ let iana = null;

function FakeDate(...args) {
const length = args.length;
if (!length) {
if (freezedAt) args = [freezedAt];
else if (traveledTo) args = [time()];
}

const dt = instantiate(NativeDate, args);

if (!length && freezedAt) return new NativeDate(freezedAt);
if (!length && traveledTo) return new NativeDate(time());
dt.getTimezoneOffset = function getTimezoneOffset() {
const curr = nativeGetTimezoneOffset.call(this);
if (!iana) return curr;

const tz = timezone(iana).getTime(this);
return Math.round((tz - this.getTime()) / 60000) + curr;
};

return instantiate(NativeDate, args);
return dt;
}

FakeDate.UTC = NativeDate.UTC;
Expand Down Expand Up @@ -100,6 +112,7 @@ function timezone(timeZone) {
defrost,
reset,
isKeepingTime,
getTime,
freeze: freezeInTimezone,
travel: travelInTimezone,
};
Expand All @@ -119,9 +132,10 @@ function timezone(timeZone) {
function getTime(...args) {
const realDate = instantiate(Date, args);
const tz = new NativeDate(toUTC(formatter, realDate));
if (!args.length) return tz.getTime();

if (!args.length) return tz.getTime();
const currentTz = new NativeDate(toUTC(current, realDate));

return realDate.getTime() + currentTz.getTime() - tz.getTime();
}
}
Expand All @@ -146,6 +160,7 @@ function instantiate(type, args) {

function toUTC(formatter, dt) {
let year, month, day, hour, minute, second;

for (const {type, value} of formatter.formatToParts(dt)) {
switch (type) {
case 'year':
Expand All @@ -169,5 +184,5 @@ function toUTC(formatter, dt) {
}
}

return NativeDate.UTC(year, month, day, hour, minute, second);
return NativeDate.UTC(year, month, day, hour, minute, second, dt.getMilliseconds());
}
79 changes: 53 additions & 26 deletions test/timezone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ describe('chronokinesis', () => {
it('without arguments freezes moves now to timezone', () => {
const utc = Date.now();

const tzLA = ck.timezone('America/Los_Angeles');
tzLA.freeze();
expect(diffHrs(utc, new Date()), 'LA', new Date().toISOString() + ' -> ' + new Date(utc).toISOString()).to.equal(-7);

ck.reset();
const tzShanghai = ck.timezone('Asia/Shanghai');
tzShanghai.freeze();
expect(diffHrs(utc, new Date()), 'Shanghai').to.equal(8);

ck.reset();
const tzFinland = ck.timezone('Europe/Helsinki');
tzFinland.freeze();
expect(diffHrs(utc, new Date()), 'Finland').to.be.within(2, 3);
Expand All @@ -60,16 +70,6 @@ describe('chronokinesis', () => {
const tzSweden = ck.timezone('Europe/Stockholm');
tzSweden.freeze();
expect(diffHrs(utc, new Date()), 'Sweden').to.be.within(1, 2);

ck.reset();
const tzLA = ck.timezone('America/Los_Angeles');
tzLA.freeze();
expect(diffHrs(utc, new Date()), 'LA').to.equal(-7);

ck.reset();
const tzShanghai = ck.timezone('Asia/Shanghai');
tzShanghai.freeze();
expect(diffHrs(utc, new Date()), 'Shanghai').to.equal(7);
});

it('if already frozen moves now to timezone', () => {
Expand All @@ -81,7 +81,7 @@ describe('chronokinesis', () => {

const tzSweden = ck.timezone('Europe/Stockholm');
tzSweden.freeze();
expect(diffHrs(utc, new Date()), 'Sweden').to.equal(4);
expect(diffHrs(utc, new Date()), 'Sweden').to.equal(5);
});

it('returns date adjusted to timezone', () => {
Expand Down Expand Up @@ -156,9 +156,9 @@ describe('chronokinesis', () => {
const utc = Date.now();
const tz = ck.timezone('Asia/Shanghai');
tz.freeze();
expect(diffHrs(utc, new Date()), '1st').to.equal(7);
expect(diffHrs(utc, new Date()), '1st').to.equal(8);
tz.freeze();
expect(diffHrs(utc, new Date()), '2nd').to.equal(7);
expect(diffHrs(utc, new Date()), '2nd').to.equal(8);
});
});

Expand Down Expand Up @@ -245,8 +245,8 @@ describe('chronokinesis', () => {
const dt = Date.UTC(2021, 2, 20, 0, 1, 0, 123);

const tz = ck.timezone('America/Los_Angeles');
tz.travel(dt);
expect(diffHrs(dt, new Date())).to.equal(8);
tz.travel(2021, 2, 20, 0, 1, 0, 123);
expect(diffHrs(dt, new Date())).to.equal(7);

const before = Date.now();

Expand All @@ -256,7 +256,7 @@ describe('chronokinesis', () => {

expect(Date.now() - before).to.be.above(50);

expect(diffHrs(dt, new Date())).to.equal(8);
expect(diffHrs(dt, new Date())).to.equal(7);
});

it('works in combination with timezone freeze', async () => {
Expand All @@ -275,38 +275,65 @@ describe('chronokinesis', () => {
tz.freeze();

expect(Date.now() - before).to.be.within(50, 200);
expect(diffHrs(utc, new Date())).to.equal(-7);
expect(diffHrs(utc, new Date())).to.equal(-8);
});

it('works in combination with freeze', async () => {
it('works in combination with freeze', () => {
const dt = Date.UTC(2021, 2, 20, 0, 1, 0, 123);
ck.freeze(dt);

const tz = ck.timezone('Asia/Shanghai');
tz.travel(dt);
expect(diffHrs(dt, new Date())).to.equal(-7);
tz.travel();
expect(diffHrs(dt, new Date())).to.equal(8);
});

it('works in combination with travel', async () => {
it('works in combination with travel', () => {
const dt = Date.UTC(2021, 2, 20, 0, 1, 0, 123);
ck.travel(dt);

const tz = ck.timezone('Asia/Shanghai');
tz.travel();
expect(diffHrs(dt, new Date())).to.equal(7);
expect(diffHrs(dt, new Date())).to.equal(8);
});

it('travel after travel without arguments is ignored', async () => {
it('travel after travel without arguments is ignored', () => {
const utc = Date.now();
const tz = ck.timezone('Asia/Shanghai');
tz.travel();
expect(diffHrs(utc, new Date()), '1st').to.equal(7);
expect(diffHrs(utc, new Date()), '1st').to.equal(8);
tz.travel();
expect(diffHrs(utc, new Date()), '2nd').to.equal(7);
expect(diffHrs(utc, new Date()), '2nd').to.equal(8);
});
});

describe('#getTimezoneOffset', () => {
beforeEach(ck.reset);

it('returns offset for current time zone', () => {
ck.freeze();
expect(new Date().getTimezoneOffset() - new NativeDate().getTimezoneOffset()).to.equal(0);
});

it('returns offset for faked time zone', () => {
ck.timezone('Asia/Shanghai').freeze();
const date = new Date();

expect(date.getTimezoneOffset()).to.equal(-480);

ck.timezone('America/Los_Angeles').travel();
expect(new Date().getTimezoneOffset()).to.equal(420);
});

it('returns offset for faked time zone with daylight saving', () => {
const tzSweden = ck.timezone('Europe/Stockholm');
tzSweden.freeze(2021, 7, 26, 17, 0);
expect(new Date().getTimezoneOffset()).to.equal(-120);
tzSweden.freeze(2021, 2, 26, 17, 0);
expect(new Date().getTimezoneOffset()).to.equal(-60);
});
});
});

function diffHrs(dt, faked) {
return ~~((faked.getTime() - new NativeDate(dt).getTime()) / 3600000);
return Math.round((new NativeDate(faked).getTime() - new NativeDate(dt).getTime()) / 3600000);
}

0 comments on commit d5a2ae2

Please sign in to comment.