-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrazy_dates.ts
52 lines (45 loc) · 1.74 KB
/
crazy_dates.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
export const parseNorwegianDate = (whencreated: string) => {
const [_date, _time] = whencreated?.split(" ");
if (_date) {
const date = _date?.split(".")?.reverse()?.join("-");
const time = `${_time ?? "12:00:00Z"}`;
return new Date(`${date}T${time}`);
}
return new Date();
};
// The Active Directory stores date/time values as the number of 100-nanosecond intervals that have elapsed since the 0 hour on January 1, 1601…
export const parseAdTime = (n: number) =>
new Date(n / 10000 + new Date(Date.UTC(1601, 0, 1)).getTime());
// parse weird dates like "03/01/0024 01:00:00" to 2024-03-01T01:00:00Z
const weirdRegex =
/^(?<_month>\d{2})\/(?<_day>\d{2})\/00(?<_yy>\d{2})\s(?<_t>\d{2}:\d{2}:\d{2})$/;
export const parseWeirdUsDate = (weird: string) => {
const r = weirdRegex.exec(weird);
if (r) {
const { _month, _day, _yy, _t } = r.groups as Record<string, string>;
const year = 2000 + Number(_yy);
const [month, day] = [_month, _day].map(Number);
const [h, m, s] = _t.split(":").map(Number);
return new Date(Date.UTC(year, month - 1, day, h, m, s));
}
return undefined;
};
export const getAdTime = (
_adtime: number | string,
) => {
const adtime = _adtime === "0" ? 0 : Number(_adtime);
const _expired = adtime > 0 && adtime < 9e18
? parseAdTime(adtime)
: undefined;
return _expired ? _expired : undefined;
};
export const getAdTimeOrUndefinedIfInFuture = (
_adtime: number | string,
) => {
const adtime = getAdTime(_adtime);
return adtime && adtime.getTime() < new Date().getTime() ? adtime : undefined;
};
export const getWeirdUsTimeOrUndefinedIfInFuture = (_w: string) => {
const weird = parseWeirdUsDate(_w);
return weird && weird.getTime() < new Date().getTime() ? weird : undefined;
};