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
|
/* LUT for date-number from month name. */
const SHORT_MONTH = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
/**
* Gets a month number from the name
* @param {string} The month name to find the number of.
* @return {number} The number of the monthname.
*/
function getMonthNumber(monthName) {
if (typeof(monthName) !== "string") {
throw new Error(`Cannot get month number of ${typeof(monthName)}...`);
}
if (monthName.length < 3) {
throw new Error(`Cannot get month number string "${monthName}" of length ${monthName.length}...`);
}
monthName = monthName.substring(0,3).toLowerCase();
let monthNumber = SHORT_MONTH.indexOf(monthName);
if (monthNumber === -1) {
throw new Error(`Could not find the month ${monthName}.`);
}
return monthNumber;
}
/**
* Padds a string (or number) with leading zeroes.
* @param {string|number} number The string to be padded.
* @param {number} [2] n The minimum width of the returned string.
* @return {string} Zero-padded string.
*/
function zpad(number, n=2) {
let ret = number.toString();
while (n - ret.length > 0) {
ret = "0" + ret;
}
return ret
}
/**
* Attempts to recognize date-string pattern and convert into ISO861.
* @param {string} dateStr A String representing the time, UTC will be assumed
* unless it is valid ISO861 with a different timezone.
* @return {string} dateStr as ISO861. If no pattern was found (or it
* already is valid ISO861. The string will be returned
* with no changes.
*/
function toISO861UTC(dateStr) {
// yyyy-mm-ddThh:mm:ssZ
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/.test(dateStr)) {
//console.log(`yyyy-mm-ddThh:mm:ssZ <- ${dateStr}`);
return dateStr;
}
// yyyy MMMM dd
if (/^\d{4}\s[a-z,A-Z]+\s\d{2}$/.test(dateStr)) {
//console.log(`yyyy MMM dd <- ${dateStr}`);
let parts = dateStr.split(" ");
dateStr = (`${parts[0]}-${zpad(getMonthNumber(parts[1]))}-${parts[2]}T00:00:00Z`);
return dateStr;
}
// yyyy mm dd hh:mm:ss
if (/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}/.test(dateStr)) {
return dateStr.replace(" ", "T") + "Z";
}
// yyyy mm dd hh:mm:ss.msm
if (/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}.\d{4}/.test(dateStr)) {
return dateStr.replace(" ", "T").split(".")[0] + "Z";
}
console.warn(dateStr);
return dateStr;
}
/**
* Returns a date object from a date-string.
* @param {string} dateStr A String representing the time, UTC will be assumed
* unless it is valid ISO861 with a different timezone.
* @return {Date} The date object representing the date-string.
*/
export function parseDateAsUTC(dateStr) {
dateStr = toISO861UTC(dateStr);
return new Date(dateStr);
}
|