Skip to content

Commit

Permalink
update(module/date): update main.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaodong2008 committed Dec 26, 2023
1 parent 4123b5b commit 7bdd7bc
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/date/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ class FastjsDate extends FastjsBaseModule<FastjsDate> {
public _date: number;
public _createAt: number = Date.now();
public timezoneDiff: number = new Date().getTimezoneOffset() * 60 * 1000;
public isUTC: boolean;

constructor(format: string, date: fDate)
constructor(format: string, date: fDate, toLocal?: boolean)
constructor(format: string, date: number | string, isUTC?: boolean)
constructor(
public format: string = "Y-M-D h:m:s",
date: number | string | fDate = Date.now(),
public isUTC: boolean = false
sw: boolean = false
) {
super();

if (typeof date === "object") {
this._date = date.t;
this._date = sw ? date.t - date.z : date.t;
this.timezoneDiff = date.z;
this.isUTC = date.u;
} else this._date = (typeof date === "string") ? this.parseFormatString(format, date) : date;
this.isUTC = date.u && !sw;
} else {
this.isUTC = sw
this._date = (typeof date === "string") ? this.parseFormatString(format, date) : date;
}
}

convertUTC(from: "utc" | "local" | "default" = "default", timezoneOffset: number = this.timezoneDiff): FastjsDate {
Expand Down Expand Up @@ -67,25 +71,27 @@ class FastjsDate extends FastjsBaseModule<FastjsDate> {
"*export(**toUTC: boolean**): fDate",
"*this.isUTC: true",
"FastjsDate.export",
"super:", this
"super: ", this
], ["fastjs.warn"]);
throw _dev.error("fastjs/date/FastjsDate", "Exporting UTC date to UTC", [
"export(toUTC: boolean): fDate",
"FastjsDate.export",
]);
}
let t = this._date;
if (toUTC && !this.isUTC) t += this.timezoneDiff;
return {
t: this.toNumber(),
t: t,
z: this.timezoneDiff,
u: this.isUTC
u: this.isUTC || toUTC
}
}

toNumber(): number {
return this._date;
}

toNumberLocal(): number {
toActiveNumber(): number {
const timeLeft = Date.now() - this._createAt;
return this._date + timeLeft;
}
Expand All @@ -107,7 +113,7 @@ class FastjsDate extends FastjsBaseModule<FastjsDate> {
return result;
}

toStringLocal(newFormat?: string): string {
toActiveString(newFormat?: string): string {
return new FastjsDate(newFormat || this.format, this.toNumberLocal()).toString();
}

Expand Down Expand Up @@ -139,7 +145,7 @@ class FastjsDate extends FastjsBaseModule<FastjsDate> {
"***formatString: " + formatString,
"***dateString: " + dateString,
"private parseFormatString(formatString: string, dateString: string): number",
"super:", this
"super: ", this
], ["fastjs.wrong"]);
throw _dev.error("fastjs/date/FastjsDate", "Invalid format string, token cannot be adjacent", [
"private parseFormatString(formatString: string, dateString: string): number",
Expand Down Expand Up @@ -203,7 +209,7 @@ class FastjsDate extends FastjsBaseModule<FastjsDate> {
"***formatString: " + formatString,
"***dateString: " + dateString,
"private parseFormatString(formatString: string, dateString: string): number",
"super:", this
"super: ", this
], ["fastjs.wrong"]);
throw _dev.error("fastjs/date/FastjsDate", "Invalid format string, using 12 hours format but missing AM/PM token", [
"private parseFormatString(formatString: string, dateString: string): number",
Expand Down

0 comments on commit 7bdd7bc

Please sign in to comment.