Archived
1
0
Fork 0

implement FullDate#distanceFrom(d0)

fix FullDate timezone bug, use UTC instead
This commit is contained in:
supmiku39 2020-04-09 19:45:32 +09:00
parent 87615616d6
commit 26d531c26e
4 changed files with 46 additions and 32 deletions

View file

@ -13,4 +13,5 @@ export declare class FullDate {
set month(val: number);
set day(val: number);
advance(period: number): FullDate;
distanceFrom(d0: FullDate): number;
}

View file

@ -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;

View file

@ -1,7 +1,12 @@
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 = (()=>{
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);
@ -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;
}
}

View file

@ -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",