1
-
2
1
export class UnparsedObject {
3
- _data:any;
4
- constructor(data:any) {
2
+ _data: any;
3
+ constructor(data: any) {
5
4
this._data = data;
6
5
}
7
6
}
@@ -13,73 +12,36 @@ export type AttributeTypeMap = {
13
12
required?: boolean;
14
13
format?: string;
15
14
};
16
- }
15
+ };
17
16
18
- export const isBrowser: boolean = typeof window !== "undefined" && typeof window.document !== "undefined";
17
+ export const isBrowser: boolean =
18
+ typeof window !== "undefined" && typeof window.document !== "undefined";
19
19
20
- export const isNode: boolean = typeof process !== "undefined" && process.release && process.release.name === 'node';
20
+ export const isNode: boolean =
21
+ typeof process !== "undefined" &&
22
+ process.release &&
23
+ process.release.name === "node";
21
24
22
25
export class DDate extends Date {
23
- rfc3339TzOffset : string | undefined;
26
+ originalDate : string | undefined;
24
27
}
25
28
26
- const RFC3339Re: RegExp = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})\.?(\d+)?(?:(?:([+-]\d{2}):?(\d{2}))|Z)?$/;
29
+ const RFC3339Re =
30
+ /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})\.?(\d+)?(?:(?:([+-]\d{2}):?(\d{2}))|Z)?$/;
27
31
export function dateFromRFC3339String(date: string): DDate {
28
32
const m = RFC3339Re.exec(date);
29
33
if (m) {
30
- const _date = new DDate(date)
31
- if( m[8] === undefined && m[9] === undefined){
32
- _date.rfc3339TzOffset = 'Z'
33
- } else {
34
- _date.rfc3339TzOffset = `${m[8]}:${m[9]}`
35
- }
36
-
37
- return _date
34
+ const _date = new DDate(date);
35
+ _date.originalDate = date;
36
+ return _date;
38
37
} else {
39
- throw new Error(' unexpected date format: ' + date)
38
+ throw new Error(" unexpected date format: " + date);
40
39
}
41
40
}
42
41
43
42
export function dateToRFC3339String(date: Date | DDate): string {
44
- const offSetArr = getRFC3339TimezoneOffset(date).split(":")
45
- const tzHour = offSetArr.length == 1 ? 0 : +offSetArr[0];
46
- const tzMin = offSetArr.length == 1 ? 0 : +offSetArr[1];
47
-
48
- const year = date.getFullYear() ;
49
- const month = date.getMonth();
50
- const day = date.getUTCDate();
51
- const hour = date.getUTCHours() + tzHour;
52
- const minute = date.getUTCMinutes() + tzMin;
53
- const second = date.getUTCSeconds();
54
-
55
- let msec = date.getUTCMilliseconds().toString();
56
- msec = +msec === 0 ? "" : `.${pad(+msec, 3)}`
57
-
58
- return year + "-" +
59
- pad(month + 1) + "-" +
60
- pad(day) + "T" +
61
- pad(hour) + ":" +
62
- pad(minute) + ":" +
63
- pad(second) +
64
- msec +
65
- offSetArr.join(":");
66
- }
67
-
68
- // Helpers
69
- function pad(num: number, len: number = 2): string {
70
- let paddedNum = num.toString()
71
- if (paddedNum.length < len) {
72
- paddedNum = "0".repeat(len - paddedNum.length) + paddedNum
73
- } else if (paddedNum.length > len) {
74
- paddedNum = paddedNum.slice(0, len)
75
- }
76
-
77
- return paddedNum
78
- }
79
-
80
- function getRFC3339TimezoneOffset(date: Date | DDate): string {
81
- if (date instanceof DDate && date.rfc3339TzOffset) {
82
- return date.rfc3339TzOffset;
83
- }
84
- return "Z";
43
+ if (date instanceof DDate && date.originalDate) {
44
+ return date.originalDate;
45
+ }
46
+ return date.toISOString().split('.')[0] + "Z";
85
47
}
0 commit comments