-
Notifications
You must be signed in to change notification settings - Fork 35
/
date.ts
131 lines (112 loc) · 3.83 KB
/
date.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* eslint-disable */
export type _NativeDate = Date;
export const _NativeDateClass = Date;
/**
* Specific padding for number
*
* @param val number
* @param digits minimal number of digits to display
*/
export function pad(val: number, digits: number = 2): string {
const str = `${val}`;
return '0'.repeat(Math.max(0, digits - str.length)) + str;
}
export class CommonDate extends Date {
/**
* Removes timezone information from ISO8601 strings (Received from DAPI)
*/
constructor(...args: any[]) {
if (args) {
if (typeof args[0] === 'string') {
const idxT = args[0].lastIndexOf('T');
// TZD = time zone designator (Z or +hh:mm or -hh:mm)
let idx = args[0].lastIndexOf('Z');
if (idx < 0) { idx = args[0].lastIndexOf('+'); }
if (idx < 0 && idxT > 0) {
const relativeIdx = args[0].substring(idxT).lastIndexOf('-');
idx = relativeIdx > 0 ? relativeIdx + idxT : relativeIdx;
}
if (idx > 0) {
args[0] = args[0].substring(0, idx);
}
const TIME_ZONE_OFFSET = ((new (Date as any)(...args)) as Date).getTimezoneOffset();
if (idxT > 0) {
args[0] += `${TIME_ZONE_OFFSET < 0 ? '+' : '-'}${pad(Math.floor(Math.abs(TIME_ZONE_OFFSET / 60)))}:${pad(Math.abs(TIME_ZONE_OFFSET % 60))}`;
}
} else if (args[0] instanceof CommonDate) {
args[0] = args[0];
}
}
super(...(args as []));
}
/**
* Overrides the JSON conversion to remove any timezone information.
*/
public toJSON(): string {
return `${this.getFullYear()}-${pad(this.getMonth() + 1)}-${pad(this.getDate())}T${pad(this.getHours())}:${pad(this.getMinutes())}:${pad(this.getSeconds())}.${pad(this.getMilliseconds(), 3)}`;
}
}
/**
* A collection of utilities required by the auto-generated code.
*/
export namespace utils {
'use strict';
export class Date extends CommonDate {
/**
* Convert Date to JavaScript DateTime compatible entry
*/
constructor()
constructor(value: number)
constructor(value: string)
constructor(date: _NativeDate)
constructor(dateUtils: Date)
constructor(year: number, month: number, date?: number)
constructor(...args: any[]) {
if (args && typeof args[0] === 'string' && args[0].lastIndexOf('T') < 0) {
args[0] = `${args[0]}T00:00:00Z`;
} else if (args[0] instanceof _NativeDateClass) {
args[0] = `${args[0].getFullYear()}-${pad(args[0].getMonth() + 1)}-${pad(args[0].getDate())}T00:00:00Z`;
}
super(...args);
}
/**
* To ensure that users cannot use a standard Date instead of utils.Date
*/
protected _Date(): void {}
/**
* Overrides the JSON conversion to remove any time information.
*/
public toJSON(): string {
return (`${this.getFullYear()}-${pad(this.getMonth() + 1)}-${pad(this.getDate())}`);
}
/**
* Compare if two dates are equals.
*
* @param {Date} date the date to compare
* @return {boolean} true if the dates are equals.
*/
public equals(date?: Date): boolean {
if (!date) {
return false;
}
return this.getFullYear() === date.getFullYear() && this.getMonth() === date.getMonth() && this.getDate() === date.getDate();
}
}
export class DateTime extends CommonDate {
constructor()
constructor(value: number)
constructor(value: string)
constructor(date: _NativeDate)
constructor(dateTimeUtils: DateTime)
constructor(dateUtils: Date)
constructor(otherDate: any)
constructor(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number)
constructor(...args: any[]) {
super(...args);
}
/**
* To ensure that users cannot use a standard Date instead of utils.DateTime
*/
protected _DateTime(): void {}
}
}