From 26d531c26ee70a7794e59ba2d65cea080781b727 Mon Sep 17 00:00:00 2001 From: supmiku39 Date: Thu, 9 Apr 2020 19:45:32 +0900 Subject: [PATCH] implement FullDate#distanceFrom(d0) fix FullDate timezone bug, use UTC instead --- dist/utils/FullDate.d.ts | 1 + dist/utils/FullDate.js | 38 ++++++++++++++++++++++---------------- lib/utils/FullDate.ts | 37 ++++++++++++++++++++++--------------- package.json | 2 +- 4 files changed, 46 insertions(+), 32 deletions(-) diff --git a/dist/utils/FullDate.d.ts b/dist/utils/FullDate.d.ts index 914e2b1..ef710bd 100644 --- a/dist/utils/FullDate.d.ts +++ b/dist/utils/FullDate.d.ts @@ -13,4 +13,5 @@ export declare class FullDate { set month(val: number); set day(val: number); advance(period: number): FullDate; + distanceFrom(d0: FullDate): number; } diff --git a/dist/utils/FullDate.js b/dist/utils/FullDate.js index 27396d3..e184655 100644 --- a/dist/utils/FullDate.js +++ b/dist/utils/FullDate.js @@ -1,13 +1,19 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); +function removeTime(date) { + var dt = date.getTimezoneOffset() * 60e3; + return new Date(new Date(date.valueOf() - dt).setUTCHours(0, 0, 0, 0)); +} var FullDate = /** @class */ (function () { function FullDate() { var argv = []; for (var _i = 0; _i < arguments.length; _i++) { argv[_i] = arguments[_i]; } - this._date = (function () { - var _a; + this._date = removeTime((function () { + var _a, _b, _c; + if (argv.length == 0) + return new Date(); if (argv.length == 1) { var arg = argv[0]; if (arg instanceof FullDate) @@ -21,22 +27,19 @@ var FullDate = /** @class */ (function () { } return new Date(arg); } - else if (argv.length == 3) { - return new Date(argv[0], argv[1] - 1, argv[2]); - } - return new Date(); - })(); + return new Date(argv[0], ((_b = argv[1]) !== null && _b !== void 0 ? _b : 1) - 1, (_c = argv[2]) !== null && _c !== void 0 ? _c : 1); + })()); } FullDate.prototype.toString = function () { var d = this._date; var f = function (s) { return ('0' + s).slice(-2); }; - return d.getFullYear() + "-" + f(d.getMonth() + 1) + "-" + f(d.getDate()); + return d.getUTCFullYear() + "-" + f(d.getUTCMonth() + 1) + "-" + f(d.getUTCDate()); }; FullDate.prototype.toJSON = function () { return this.toString(); }; FullDate.prototype.valueOf = function () { - return new Date(this._date).setHours(0, 0, 0, 0); + return this._date.valueOf(); }; Object.defineProperty(FullDate.prototype, "date", { // getter @@ -48,38 +51,38 @@ var FullDate = /** @class */ (function () { }); Object.defineProperty(FullDate.prototype, "year", { get: function () { - return this._date.getFullYear(); + return this._date.getUTCFullYear(); }, // setter set: function (val) { - this._date.setFullYear(val); + this._date.setUTCFullYear(val); }, enumerable: true, configurable: true }); Object.defineProperty(FullDate.prototype, "month", { get: function () { - return this._date.getMonth() + 1; + return this._date.getUTCMonth() + 1; }, set: function (val) { - this._date.setMonth(val - 1); + this._date.setUTCMonth(val - 1); }, enumerable: true, configurable: true }); Object.defineProperty(FullDate.prototype, "day", { get: function () { - return this._date.getDate(); + return this._date.getUTCDate(); }, set: function (val) { - this._date.setDate(val); + this._date.setUTCDate(val); }, enumerable: true, configurable: true }); Object.defineProperty(FullDate.prototype, "dayOfWeek", { get: function () { - return this._date.getDay(); + return this._date.getUTCDay(); }, enumerable: true, configurable: true @@ -88,6 +91,9 @@ var FullDate = /** @class */ (function () { FullDate.prototype.advance = function (period) { return new FullDate(this._date.valueOf() + period * 86400e3); }; + FullDate.prototype.distanceFrom = function (d0) { + return (this.valueOf() - d0.valueOf()) / 86400e3; + }; return FullDate; }()); exports.FullDate = FullDate; diff --git a/lib/utils/FullDate.ts b/lib/utils/FullDate.ts index f8165e5..f669776 100644 --- a/lib/utils/FullDate.ts +++ b/lib/utils/FullDate.ts @@ -1,8 +1,13 @@ +function removeTime(date: Date): Date { + const dt = date.getTimezoneOffset()*60e3; + return new Date(new Date(date.valueOf()-dt).setUTCHours(0, 0, 0, 0)); +} export class FullDate { private _date: Date; constructor(...argv: any) { - this._date = (()=>{ - if (argv.length==1) { + this._date = removeTime((() => { + if (argv.length == 0) return new Date(); + if (argv.length == 1) { const arg = argv[0]; if (arg instanceof FullDate) return new Date(+arg); if (arg instanceof Date) return arg; @@ -11,51 +16,53 @@ export class FullDate { if (tokens) return new Date(+tokens[0], +tokens[1]-1, +tokens[2]); } return new Date(arg); - } else if (argv.length==3) { - return new Date(argv[0], argv[1]-1, argv[2]); } - return new Date(); - })(); + return new Date(argv[0], (argv[1] ?? 1)-1, argv[2] ?? 1); + })()); } toString(): string { const d = this._date; const f = (s: any) => ('0'+s).slice(-2); - return `${d.getFullYear()}-${f(d.getMonth()+1)}-${f(d.getDate())}`; + return `${d.getUTCFullYear()}-${ + f(d.getUTCMonth()+1)}-${f(d.getUTCDate())}`; } toJSON(): string { return this.toString(); } valueOf(): number { - return new Date(this._date).setHours(0, 0, 0, 0); + return this._date.valueOf(); } // getter get date(): Date { return new Date(this._date); } get year(): number { - return this._date.getFullYear(); + return this._date.getUTCFullYear(); } get month(): number { - return this._date.getMonth()+1; + return this._date.getUTCMonth()+1; } get day(): number { - return this._date.getDate(); + return this._date.getUTCDate(); } get dayOfWeek(): number { - return this._date.getDay(); + return this._date.getUTCDay(); } // setter set year(val: number) { - this._date.setFullYear(val); + this._date.setUTCFullYear(val); } set month(val: number) { - this._date.setMonth(val-1); + this._date.setUTCMonth(val-1); } set day(val: number) { - this._date.setDate(val); + this._date.setUTCDate(val); } // func advance(period: number): FullDate { return new FullDate(this._date.valueOf()+period*86400e3); } + distanceFrom(d0: FullDate): number { + return (this.valueOf()-d0.valueOf())/86400e3; + } } diff --git a/package.json b/package.json index 97884c8..3a1f277 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "api-codegen-ts", - "version": "1.1.0", + "version": "1.1.1", "description": "OpenAPI code generator for TypeScript", "main": "dist/index.js", "types": "dist/index.d.ts",