-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeObject.ts
110 lines (97 loc) · 3.38 KB
/
timeObject.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import dateToString, { zeroBefore } from "../helpers/dateToString";
import localeInfo from "./localeInfo";
// watch: deprecate in favor of PlainTime: https://caniuse.com/?search=Temporal.PlainTime
/** Plane time-object without date */
export default class WUPTimeObject {
hours: number;
minutes: number;
static $maxValue = 23 * 60 + 59;
constructor();
constructor(value: number | string);
/** @param isPM set true/false if hours is 12h format */
constructor(hours: number, minutes: number, isPM?: boolean);
/** Extract hh & mm from date */
constructor(value: Date, isUTC: boolean);
constructor(...args: any[]) {
this.hours = 0;
this.minutes = 0;
let isPM: boolean | null | undefined = null;
if (args.length === 1) {
if (typeof args[0] === "string") {
const [h, m] = args[0].split(/\W/);
this.hours = Number.parseInt(h, 10);
this.minutes = Number.parseInt(m, 10);
if (/[pP][mM]$/.test(args[0])) {
isPM = true;
} else if (/[aA][mM]$/.test(args[0])) {
isPM = false;
}
} else {
this.hours = Math.floor(args[0] / 60);
this.minutes = args[0] - this.hours * 60;
}
} else if (args.length >= 2) {
if (args.length === 2 && typeof args[0] === "object") {
const u = args[1] ? "UTC" : "";
this.hours = args[0][`get${u}Hours`]();
this.minutes = args[0][`get${u}Minutes`]();
} else {
this.hours = args[0] as number;
this.minutes = args[1] as number;
// eslint-disable-next-line prefer-destructuring
isPM = args[2];
}
}
const h12Invalid = isPM != null && (this.hours < 1 || this.hours > 12); // with AM/PM only 1...12 is valid
if (this.hours === 12 && isPM === false) {
this.hours = 0;
} else if (this.hours !== 12 && isPM === true) {
this.hours += 12;
}
if (
h12Invalid ||
Number.isNaN(this.hours) ||
Number.isNaN(this.minutes) ||
this.hours < 0 ||
this.hours > 23 ||
this.minutes < 0 ||
this.minutes > 59
) {
const err = RangeError("Out of range") as RangeError & { details: any };
err.details = { parsed: this, args };
console.warn(`${err.message}. Details:`, err.details);
throw err;
}
}
/** Copy hours & minutes to pointed Date
* @returns the same date object */
copyToDate(dt: Date, isUTC: boolean): Date {
const ukey = isUTC ? "UTC" : "";
dt[`set${ukey}Hours`](this.hours, this.minutes, 0, 0); // , dt[`get${ukey}Seconds`](), dt[`get${ukey}Milliseconds`]());
return dt;
}
/** Returns number representation of current object */
valueOf(): number {
return this.hours * 60 + this.minutes;
}
/** Returns whether time is PM or AM */
get isPM(): boolean {
return this.hours > 11;
}
/** Returns string in "hh:mm" format */
toString(sep = ":"): string {
return `${zeroBefore(this.hours, 2)}${sep}${zeroBefore(this.minutes, 2)}`;
}
/** Returns string according to objects/localeInfo.time format */
toLocaleString(): string {
return this.format(localeInfo.time);
}
/** Returns string according to pointed format (hh:mm) */
format(format: string): string {
return dateToString(new Date(1, 1, 1, this.hours, this.minutes), format.replace(/[\D\W]ss/, ""));
}
/** Returns string in "h:m" format */
toJSON(): string {
return `${this.hours}:${this.minutes}`;
}
}