implement FullDate#distanceFrom(d0)
fix FullDate timezone bug, use UTC instead
This commit is contained in:
parent
87615616d6
commit
26d531c26e
4 changed files with 46 additions and 32 deletions
1
dist/utils/FullDate.d.ts
vendored
1
dist/utils/FullDate.d.ts
vendored
|
@ -13,4 +13,5 @@ export declare class FullDate {
|
||||||
set month(val: number);
|
set month(val: number);
|
||||||
set day(val: number);
|
set day(val: number);
|
||||||
advance(period: number): FullDate;
|
advance(period: number): FullDate;
|
||||||
|
distanceFrom(d0: FullDate): number;
|
||||||
}
|
}
|
||||||
|
|
38
dist/utils/FullDate.js
vendored
38
dist/utils/FullDate.js
vendored
|
@ -1,13 +1,19 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
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 () {
|
var FullDate = /** @class */ (function () {
|
||||||
function FullDate() {
|
function FullDate() {
|
||||||
var argv = [];
|
var argv = [];
|
||||||
for (var _i = 0; _i < arguments.length; _i++) {
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||||||
argv[_i] = arguments[_i];
|
argv[_i] = arguments[_i];
|
||||||
}
|
}
|
||||||
this._date = (function () {
|
this._date = removeTime((function () {
|
||||||
var _a;
|
var _a, _b, _c;
|
||||||
|
if (argv.length == 0)
|
||||||
|
return new Date();
|
||||||
if (argv.length == 1) {
|
if (argv.length == 1) {
|
||||||
var arg = argv[0];
|
var arg = argv[0];
|
||||||
if (arg instanceof FullDate)
|
if (arg instanceof FullDate)
|
||||||
|
@ -21,22 +27,19 @@ var FullDate = /** @class */ (function () {
|
||||||
}
|
}
|
||||||
return new Date(arg);
|
return new Date(arg);
|
||||||
}
|
}
|
||||||
else if (argv.length == 3) {
|
return new Date(argv[0], ((_b = argv[1]) !== null && _b !== void 0 ? _b : 1) - 1, (_c = argv[2]) !== null && _c !== void 0 ? _c : 1);
|
||||||
return new Date(argv[0], argv[1] - 1, argv[2]);
|
})());
|
||||||
}
|
|
||||||
return new Date();
|
|
||||||
})();
|
|
||||||
}
|
}
|
||||||
FullDate.prototype.toString = function () {
|
FullDate.prototype.toString = function () {
|
||||||
var d = this._date;
|
var d = this._date;
|
||||||
var f = function (s) { return ('0' + s).slice(-2); };
|
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 () {
|
FullDate.prototype.toJSON = function () {
|
||||||
return this.toString();
|
return this.toString();
|
||||||
};
|
};
|
||||||
FullDate.prototype.valueOf = function () {
|
FullDate.prototype.valueOf = function () {
|
||||||
return new Date(this._date).setHours(0, 0, 0, 0);
|
return this._date.valueOf();
|
||||||
};
|
};
|
||||||
Object.defineProperty(FullDate.prototype, "date", {
|
Object.defineProperty(FullDate.prototype, "date", {
|
||||||
// getter
|
// getter
|
||||||
|
@ -48,38 +51,38 @@ var FullDate = /** @class */ (function () {
|
||||||
});
|
});
|
||||||
Object.defineProperty(FullDate.prototype, "year", {
|
Object.defineProperty(FullDate.prototype, "year", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._date.getFullYear();
|
return this._date.getUTCFullYear();
|
||||||
},
|
},
|
||||||
// setter
|
// setter
|
||||||
set: function (val) {
|
set: function (val) {
|
||||||
this._date.setFullYear(val);
|
this._date.setUTCFullYear(val);
|
||||||
},
|
},
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
configurable: true
|
configurable: true
|
||||||
});
|
});
|
||||||
Object.defineProperty(FullDate.prototype, "month", {
|
Object.defineProperty(FullDate.prototype, "month", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._date.getMonth() + 1;
|
return this._date.getUTCMonth() + 1;
|
||||||
},
|
},
|
||||||
set: function (val) {
|
set: function (val) {
|
||||||
this._date.setMonth(val - 1);
|
this._date.setUTCMonth(val - 1);
|
||||||
},
|
},
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
configurable: true
|
configurable: true
|
||||||
});
|
});
|
||||||
Object.defineProperty(FullDate.prototype, "day", {
|
Object.defineProperty(FullDate.prototype, "day", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._date.getDate();
|
return this._date.getUTCDate();
|
||||||
},
|
},
|
||||||
set: function (val) {
|
set: function (val) {
|
||||||
this._date.setDate(val);
|
this._date.setUTCDate(val);
|
||||||
},
|
},
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
configurable: true
|
configurable: true
|
||||||
});
|
});
|
||||||
Object.defineProperty(FullDate.prototype, "dayOfWeek", {
|
Object.defineProperty(FullDate.prototype, "dayOfWeek", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._date.getDay();
|
return this._date.getUTCDay();
|
||||||
},
|
},
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
configurable: true
|
configurable: true
|
||||||
|
@ -88,6 +91,9 @@ var FullDate = /** @class */ (function () {
|
||||||
FullDate.prototype.advance = function (period) {
|
FullDate.prototype.advance = function (period) {
|
||||||
return new FullDate(this._date.valueOf() + period * 86400e3);
|
return new FullDate(this._date.valueOf() + period * 86400e3);
|
||||||
};
|
};
|
||||||
|
FullDate.prototype.distanceFrom = function (d0) {
|
||||||
|
return (this.valueOf() - d0.valueOf()) / 86400e3;
|
||||||
|
};
|
||||||
return FullDate;
|
return FullDate;
|
||||||
}());
|
}());
|
||||||
exports.FullDate = FullDate;
|
exports.FullDate = FullDate;
|
||||||
|
|
|
@ -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 {
|
export class FullDate {
|
||||||
private _date: Date;
|
private _date: Date;
|
||||||
constructor(...argv: any) {
|
constructor(...argv: any) {
|
||||||
this._date = (()=>{
|
this._date = removeTime((() => {
|
||||||
if (argv.length==1) {
|
if (argv.length == 0) return new Date();
|
||||||
|
if (argv.length == 1) {
|
||||||
const arg = argv[0];
|
const arg = argv[0];
|
||||||
if (arg instanceof FullDate) return new Date(+arg);
|
if (arg instanceof FullDate) return new Date(+arg);
|
||||||
if (arg instanceof Date) return 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]);
|
if (tokens) return new Date(+tokens[0], +tokens[1]-1, +tokens[2]);
|
||||||
}
|
}
|
||||||
return new Date(arg);
|
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 {
|
toString(): string {
|
||||||
const d = this._date;
|
const d = this._date;
|
||||||
const f = (s: any) => ('0'+s).slice(-2);
|
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 {
|
toJSON(): string {
|
||||||
return this.toString();
|
return this.toString();
|
||||||
}
|
}
|
||||||
valueOf(): number {
|
valueOf(): number {
|
||||||
return new Date(this._date).setHours(0, 0, 0, 0);
|
return this._date.valueOf();
|
||||||
}
|
}
|
||||||
// getter
|
// getter
|
||||||
get date(): Date {
|
get date(): Date {
|
||||||
return new Date(this._date);
|
return new Date(this._date);
|
||||||
}
|
}
|
||||||
get year(): number {
|
get year(): number {
|
||||||
return this._date.getFullYear();
|
return this._date.getUTCFullYear();
|
||||||
}
|
}
|
||||||
get month(): number {
|
get month(): number {
|
||||||
return this._date.getMonth()+1;
|
return this._date.getUTCMonth()+1;
|
||||||
}
|
}
|
||||||
get day(): number {
|
get day(): number {
|
||||||
return this._date.getDate();
|
return this._date.getUTCDate();
|
||||||
}
|
}
|
||||||
get dayOfWeek(): number {
|
get dayOfWeek(): number {
|
||||||
return this._date.getDay();
|
return this._date.getUTCDay();
|
||||||
}
|
}
|
||||||
// setter
|
// setter
|
||||||
set year(val: number) {
|
set year(val: number) {
|
||||||
this._date.setFullYear(val);
|
this._date.setUTCFullYear(val);
|
||||||
}
|
}
|
||||||
set month(val: number) {
|
set month(val: number) {
|
||||||
this._date.setMonth(val-1);
|
this._date.setUTCMonth(val-1);
|
||||||
}
|
}
|
||||||
set day(val: number) {
|
set day(val: number) {
|
||||||
this._date.setDate(val);
|
this._date.setUTCDate(val);
|
||||||
}
|
}
|
||||||
// func
|
// func
|
||||||
advance(period: number): FullDate {
|
advance(period: number): FullDate {
|
||||||
return new FullDate(this._date.valueOf()+period*86400e3);
|
return new FullDate(this._date.valueOf()+period*86400e3);
|
||||||
}
|
}
|
||||||
|
distanceFrom(d0: FullDate): number {
|
||||||
|
return (this.valueOf()-d0.valueOf())/86400e3;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "api-codegen-ts",
|
"name": "api-codegen-ts",
|
||||||
"version": "1.1.0",
|
"version": "1.1.1",
|
||||||
"description": "OpenAPI code generator for TypeScript",
|
"description": "OpenAPI code generator for TypeScript",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
|
Reference in a new issue