-
Notifications
You must be signed in to change notification settings - Fork 176
/
functions.date.js
406 lines (315 loc) · 9.05 KB
/
functions.date.js
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
* Date Functions available inside Mavo expressions
*/
(function($, val, _, $u = _.util) {
var s = {seconds: 1, minutes: 60};
s.hours = s.minutes * 60;
s.days = s.hours * 24;
s.weeks = s.days * 7;
s.months = s.days * 30.4368;
s.years = s.weeks * 52;
var numeric = {
year: d => d.getFullYear(),
month: d => d.getMonth() + 1,
day: d => d.getDate(),
weekday: d => d.getDay() || 7,
hour: d => d.getHours(),
minute: d => d.getMinutes(),
second: d => d.getSeconds(),
ms: d => d.getMilliseconds()
};
function isPrecision (precision) {
if (!precision) {
return false;
}
if (precision == "ms") {
return true;
}
let singular = precision.replace(/s$/, "");
let plural = precision.replace(/s?$/, "s");
return singular in s || plural in s;
}
function parsePrecision(precision) {
precision = precision?.trim() || "";
let keys = Object.keys(s).reverse();
let ret = {};
do {
p = keys.shift();
ret[p] = true;
} while(!RegExp(p + "?").test(precision) && keys.length > 0);
if (precision == "ms") {
ret.ms = true;
}
return ret;
}
$.extend(_, {
get $now() {
return new Date();
},
$startup: new Date(), // Like $now, but doesn't update
get $today() {
return _.date(new Date());
},
year: $.extend(function() {
return $u.dateComponent("year", ...arguments);
}, {multiValued: true}),
month: $.extend(function() {
return $u.dateComponent("month", ...arguments);
}, {multiValued: true}),
week: () => s.weeks * 1000,
day: $.extend(function() {
return $u.dateComponent("day", ...arguments);
}, {multiValued: true}),
weekday: $.extend(function() {
return $u.dateComponent("weekday", ...arguments);
}, {multiValued: true}),
hour: $.extend(function() {
return $u.dateComponent("hour", ...arguments);
}, {multiValued: true}),
minute: $.extend(function() {
return $u.dateComponent("minute", ...arguments);
}, {multiValued: true}),
second: $.extend(function() {
return $u.dateComponent("second", ...arguments);
}, {multiValued: true}),
ms: $.extend(function() {
return $u.dateComponent("ms", ...arguments);
}, {multiValued: true}),
// Return an ISO date & time string
datetime: $.extend((date, time, precision) => {
date = $u.date(date);
if (!date) {
return "";
}
let separateTime;
if (time !== undefined) {
if (isPrecision(time)) {
[time, precision] = [, time];
}
else {
separateTime = true;
}
}
precision ??= "minutes";
let parts = parsePrecision(precision);
let ret = _.date(date, precision);
if (!parts.hours) {
return ret; // No time
}
if (separateTime) {
// If time is provided separately, and it's empty, we just return a date
ret += Mavo.value(time) ? `T${ _.time(time, precision) }` : "";
}
else {
ret += `T${_.time(date, precision)}`;
}
return ret;
}, {multiValued: true}),
// Return an ISO date
date: $.extend((date, precision = "days") => {
date = $u.date(date);
if (!date) {
return "";
}
let parts = parsePrecision(precision);
let ret = [];
if (parts.years) {
ret.push(_.year(date));
}
if (parts.months) {
ret.push(_.month(date, "00"));
}
if (parts.days) {
ret.push(_.day(date, "00"));
}
return ret.join("-");
}, {multiValued: true}),
// Return an ISO time
time: $.extend((date, precision = "minutes") => {
date = $u.date(date);
if (!date) {
return "";
}
let parts = parsePrecision(precision);
let ret = "";
if (parts.hours) {
ret += _.hour(date, "00") + ":" + (parts.minutes? _.minute(date, "00") : "00");
if (parts.seconds) {
ret += ":" + _.second(date, "00");
if (parts.ms) {
ret += "." + _.ms(date, "000");
}
}
}
return ret;
}, {multiValued: true}),
readable_datetime: $.extend((date, ...options) => {
options = options.map(o => typeof o === "string" || o instanceof String? {precision: o} : o);
options = Object.assign({}, ...options);
let parts = parsePrecision(options.precision);
let monthFormat = options.month || parts.days? "shortname" : "long";
let ret = [];
if (parts.days) {
ret.push(_.day(date));
}
if (parts.months) {
ret.push(_.month(date, monthFormat));
}
if (parts.years) {
ret.push(_.year(date));
}
if (parts.hours) {
ret.push(_.time(date, options.precision));
}
return ret.join(" ");
}, {multiValued: true}),
localTimezone: -(new Date()).getTimezoneOffset(),
});
_.msTo = (what, ms) => Math.floor(Math.abs(ms) / (s[what] * 1000)) || 0;
for (let unit in s) {
_[unit] = $.extend(function(ms) {
if (arguments.length === 0) {
return s[unit] * 1000;
}
return _.msTo(unit, ms);
}, {multiValued: true});
}
_.duration = $.extend(function (ms, terms) {
// TODO unify code for specific unit with code for auto units to reduce repetition
// TODO allow multiple units, e.g. ["days", "hours"]
// TODO allow combining term # and units, e.g. start: days, terms: 2
let negativeMultiplier = ms < 0 ? -1 : 1; // a multiplier to convert result to negative if needed
ms = Math.abs(ms); // negative works same way as positive does, just adding negative sign in the front
if (terms && isNaN(terms)) {
// Specific term specified
let unitSingular = terms != "ms" ? terms.replace(/s?$/, "") : terms;
let unitPlural = terms.replace(/s?$/, "s");
if (!(unitPlural in s)) {
throw new TypeError(`Unknown duration unit ${terms}. Please use one of ${ Object.keys(s).join(", ") }`);
}
let n = Math.floor(ms / s[unitPlural] / 1000);
let unitProperPlurality = n === 1 && unitPlural !== "ms" ? unitSingular : unitPlural;
return negativeMultiplier * n + " " + _.phrase.call(this, unitProperPlurality);
}
else if (ms == 0 || terms === undefined) {
terms = 1;
}
let timeLeft = ms;
let ret = [];
if (ms == 0) {
ret = ["0 ms"];
}
else {
let units = [...Object.keys(s).reverse(), "ms"];
for (let i=0, unit; unit = units[i]; i++) {
// get largest value of time unit for the remaining
// time to account for
let unitMs = unit in s? s[unit] * 1000 : 1; // number of ms in 1 unit
let unitValue = Math.floor(timeLeft / unitMs); // quotient
timeLeft = timeLeft % unitMs; // remainder
if (unitValue > 0 && ret.length < terms) {
let unitProperPlurality = unitValue === 1 && unit !== "ms" ? unit.slice(0, -1) : unit;
ret.push(negativeMultiplier * unitValue + " " + _.phrase.call(this, unitProperPlurality));
}
else if (ret.length > 0) {
// Discard any further terms to avoid non-continous terms like e.g. "1 month, 10 ms"
break;
}
}
}
return arguments.length === 1 ? ret[0] : ret;
}, {
needsContext: true,
multiValued: true
});
$.extend(_.util, {
fixDateString: function(date) {
date = date.trim();
var hasDate = /^\d{4}-\d{2}(-\d{2})?/.test(date);
var hasTime = date.indexOf(":") > -1;
if (!hasDate && !hasTime) {
return null;
}
// Fix up time format
if (!hasDate) {
// No date, add today’s
date = _.$today + " " + date;
}
else {
// Only year-month, add day
date = date.replace(/^(\d{4}-\d{2})(?!-\d{2})/, "$1-01");
}
if (!hasTime) {
// Add a time if one doesn't exist
date += "T00:00:00";
}
else {
// Make sure time starts with T, due to Safari bug
date = date.replace(/\-(\d{2})\s+(?=\d{2}:)/, "-$1T");
}
// Remove all whitespace
date = date.replace(/\s+/g, "");
return date;
},
dateComponent: function(component, date, format, locale = Mavo.locale) {
if (arguments.length === 1 && component + "s" in s) {
return _[component + "s"]();
}
var dateO = $u.date(date);
if (component === "year") {
// Why +""? We don't want years to be formatted like 2,017!
// Why the .match()? For incomplete dates, see #226
date = date && date.match? date : date + "";
var ret = dateO? dateO.getFullYear() + "" : (date.match(/\b[1-9]\d\d\b|\d+/) || [])[0];
}
if (!ret && !dateO) {
return "";
}
var ret = ret || numeric[component](dateO);
if (format) {
if (/^0+$/.test(format)) {
// Leading zeroes
return (ret + "").padStart(format.length, "0").slice(-format.length);
}
else {
format = {name: "long", shortname: "short"}[format] || format;
ret = dateO.toLocaleString(locale, {[component]: format});
ret = ret.replace(/\u200e/g, ""); // Stupid Edge bug
return ret;
}
}
return component === "year"? ret : +ret;
},
date: function(date) {
date = val(date);
if (!date) {
return null;
}
var object = new Date(date);
// Either arg is not string or is exactly the same as a re-serialization of it as a date
if ($.type(date) !== "string" || !isNaN(object) && (object + "" == date)) {
return object;
}
date = $u.fixDateString(date);
if (date === null) {
return null;
}
var timezone = date.match(/[+-]\d{2}:?\d{2}|Z$/)?.[0];
if (timezone) {
// parse as ISO format
date = new Date(date);
}
else {
// construct date in local timezone
var fields = date.match(/\d+/g);
date = new Date(
// year, month, date,
fields[0], (fields[1] || 1) - 1, fields[2] || 1,
// hours, minutes, seconds, milliseconds,
fields[3] || 0, fields[4] || 0, fields[5] || 0, fields[6] || 0
);
}
return isNaN(date)? null : date;
}
});
})(Bliss, Mavo.value, Mavo.Functions);