summaryrefslogtreecommitdiff
path: root/alarm/node_modules/jsdom/lib/jsdom/living/helpers/dates-and-times.js
blob: 15d920b4dd6687e42b4f23c3d2d4a95617a8794f (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"use strict";

function isLeapYear(year) {
  return year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0);
}

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#number-of-days-in-month-month-of-year-year
const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
function numberOfDaysInMonthOfYear(month, year) {
  if (month === 2 && isLeapYear(year)) {
    return 29;
  }
  return daysInMonth[month - 1];
}

const monthRe = /^([0-9]{4,})-([0-9]{2})$/;

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-month-string
function parseMonthString(str) {
  const matches = monthRe.exec(str);
  if (!matches) {
    return null;
  }
  const year = Number(matches[1]);
  if (year <= 0) {
    return null;
  }
  const month = Number(matches[2]);
  if (month < 1 || month > 12) {
    return null;
  }
  return { year, month };
}

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-month-string
function isValidMonthString(str) {
  return parseMonthString(str) !== null;
}
function serializeMonth({ year, month }) {
  const yearStr = `${year}`.padStart(4, "0");
  const monthStr = `${month}`.padStart(2, "0");
  return `${yearStr}-${monthStr}`;
}

const dateRe = /^([0-9]{4,})-([0-9]{2})-([0-9]{2})$/;

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-date-string
function parseDateString(str) {
  const matches = dateRe.exec(str);
  if (!matches) {
    return null;
  }
  const year = Number(matches[1]);
  if (year <= 0) {
    return null;
  }
  const month = Number(matches[2]);
  if (month < 1 || month > 12) {
    return null;
  }
  const day = Number(matches[3]);
  if (day < 1 || day > numberOfDaysInMonthOfYear(month, year)) {
    return null;
  }
  return { year, month, day };
}

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
function isValidDateString(str) {
  return parseDateString(str) !== null;
}
function serializeDate(date) {
  const dayStr = `${date.day}`.padStart(2, "0");
  return `${serializeMonth(date)}-${dayStr}`;
}

const yearlessDateRe = /^(?:--)?([0-9]{2})-([0-9]{2})$/;

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-yearless-date-string
function parseYearlessDateString(str) {
  const matches = yearlessDateRe.exec(str);
  if (!matches) {
    return null;
  }
  const month = Number(matches[1]);
  if (month < 1 || month > 12) {
    return null;
  }
  const day = Number(matches[2]);
  if (day < 1 || day > numberOfDaysInMonthOfYear(month, 4)) {
    return null;
  }
  return { month, day };
}

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-yearless-date-string
function isValidYearlessDateString(str) {
  return parseYearlessDateString(str) !== null;
}
function serializeYearlessDate({ month, day }) {
  const monthStr = `${month}`.padStart(2, "0");
  const dayStr = `${day}`.padStart(2, "0");
  return `${monthStr}-${dayStr}`;
}

const timeRe = /^([0-9]{2}):([0-9]{2})(?::([0-9]{2}(?:\.([0-9]{1,3}))?))?$/;

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-time-string
function parseTimeString(str) {
  const matches = timeRe.exec(str);
  if (!matches) {
    return null;
  }
  const hour = Number(matches[1]);
  if (hour < 0 || hour > 23) {
    return null;
  }
  const minute = Number(matches[2]);
  if (minute < 0 || minute > 59) {
    return null;
  }
  const second = matches[3] !== undefined ? Math.trunc(Number(matches[3])) : 0;
  if (second < 0 || second >= 60) {
    return null;
  }
  const millisecond = matches[4] !== undefined ? Number(matches[4]) : 0;
  return { hour, minute, second, millisecond };
}

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-time-string
function isValidTimeString(str) {
  return parseTimeString(str) !== null;
}

function serializeTime({ hour, minute, second, millisecond }) {
  const hourStr = `${hour}`.padStart(2, "0");
  const minuteStr = `${minute}`.padStart(2, "0");
  if (second === 0 && millisecond === 0) {
    return `${hourStr}:${minuteStr}`;
  }
  const secondStr = `${second}`.padStart(2, "0");
  const millisecondStr = `${millisecond}`.padStart(3, "0");
  return `${hourStr}:${minuteStr}:${secondStr}.${millisecondStr}`;
}

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-local-date-and-time-string
function parseLocalDateAndTimeString(str, normalized = false) {
  let separatorIdx = str.indexOf("T");
  if (separatorIdx < 0 && !normalized) {
    separatorIdx = str.indexOf(" ");
  }
  if (separatorIdx < 0) {
    return null;
  }
  const date = parseDateString(str.slice(0, separatorIdx));
  if (date === null) {
    return null;
  }
  const time = parseTimeString(str.slice(separatorIdx + 1));
  if (time === null) {
    return null;
  }
  return { date, time };
}

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-local-date-and-time-string
function isValidLocalDateAndTimeString(str) {
  return parseLocalDateAndTimeString(str) !== null;
}

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-normalised-local-date-and-time-string
function isValidNormalizedLocalDateAndTimeString(str) {
  return parseLocalDateAndTimeString(str, true) !== null;
}
function serializeNormalizedDateAndTime({ date, time }) {
  return `${serializeDate(date)}T${serializeTime(time)}`;
}

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#week-number-of-the-last-day
// https://stackoverflow.com/a/18538272/1937836
function weekNumberOfLastDay(year) {
  const jan1 = new Date(year, 0);
  return jan1.getDay() === 4 || (isLeapYear(year) && jan1.getDay() === 3) ? 53 : 52;
}

const weekRe = /^([0-9]{4,5})-W([0-9]{2})$/;

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-week-string
function parseWeekString(str) {
  const matches = weekRe.exec(str);
  if (!matches) {
    return null;
  }
  const year = Number(matches[1]);
  if (year <= 0) {
    return null;
  }
  const week = Number(matches[2]);
  if (week < 1 || week > weekNumberOfLastDay(year)) {
    return null;
  }
  return { year, week };
}

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-week-string
function isValidWeekString(str) {
  return parseWeekString(str) !== null;
}
function serializeWeek({ year, week }) {
  const yearStr = `${year}`.padStart(4, "0");
  const weekStr = `${week}`.padStart(2, "0");
  return `${yearStr}-W${weekStr}`;
}

// https://stackoverflow.com/a/6117889
function parseDateAsWeek(originalDate) {
  const dayInSeconds = 86400000;
  // Copy date so don't modify original
  const date = new Date(Date.UTC(originalDate.getUTCFullYear(), originalDate.getUTCMonth(), originalDate.getUTCDate()));
  // Set to nearest Thursday: current date + 4 - current day number
  // Make Sunday's day number 7
  date.setUTCDate(date.getUTCDate() + 4 - (date.getUTCDay() || 7));
  // Get first day of year
  const yearStart = new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
  // Calculate full weeks to nearest Thursday
  const week = Math.ceil((((date - yearStart) / dayInSeconds) + 1) / 7);

  return { year: date.getUTCFullYear(), week };
}

function isDate(obj) {
  try {
    Date.prototype.valueOf.call(obj);
    return true;
  } catch {
    return false;
  }
}

module.exports = {
  isDate,
  numberOfDaysInMonthOfYear,

  parseMonthString,
  isValidMonthString,
  serializeMonth,

  parseDateString,
  isValidDateString,
  serializeDate,

  parseYearlessDateString,
  isValidYearlessDateString,
  serializeYearlessDate,

  parseTimeString,
  isValidTimeString,
  serializeTime,

  parseLocalDateAndTimeString,
  isValidLocalDateAndTimeString,
  isValidNormalizedLocalDateAndTimeString,
  serializeNormalizedDateAndTime,

  parseDateAsWeek,
  weekNumberOfLastDay,
  parseWeekString,
  isValidWeekString,
  serializeWeek
};