-
Notifications
You must be signed in to change notification settings - Fork 0
/
timewave.js
715 lines (710 loc) · 29.1 KB
/
timewave.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
const isDate = (value) => value && typeof(value)==="object" && value instanceof Date;
const durationMilliseconds = {
ms: 1,
s: 1000,
m: 1000 * 60,
h: 1000 * 60 * 60,
d: 1000 * 60 * 60 * 24 * 365.2424177 / 365,
w: 1000 * 60 * 60 * 24 * 365.2424177 / 52,
mo: 1000 * 60 * 60 * 24 * 365.2424177 / 12,
q: 1000 * 60 * 60 * 24 * 365.2424177 / 4,
y: 1000 * 60 * 60 * 24 * 365.2424177
};
const durations = {
milliseconds: "ms",
seconds: "s",
minutes: "m",
hours: "h",
days: "d",
weeks: "w",
months: "mo",
quarters: "q",
years: "y"
}
const types = {
ms:"Milliseconds",
s:"Seconds",
m:"Minutes",
h:"Hours",
d:"Days",
w:"Weeks",
mo:"Months",
q:"Quarters",
y:"Years"
}
// https://stackoverflow.com/questions/11887934/how-to-check-if-dst-daylight-saving-time-is-in-effect-and-if-so-the-offset
/*
Let x be the expected number of milliseconds into the year of interest without factoring in daylight savings.
Let y be the number of milliseconds since the Epoch from the start of the year of the date of interest.
Let z be the number of milliseconds since the Epoch of the full date and time of interest
Let t be the subtraction of both x and y from z: z - y - x. This yields the offset due to DST.
If t is zero, then DST is not in effect. If t is not zero, then DST is in effect.
*/
const dstOffsetAtDate = (dateInput) => {
var fullYear = dateInput.getFullYear()|0;
// "Leap Years are any year that can be exactly divided by 4 (2012, 2016, etc)
// except if it can be exactly divided by 100, then it isn't (2100,2200,etc)
// except if it can be exactly divided by 400, then it is (2000, 2400)"
// (https://www.mathsisfun.com/leap-years.html).
var isLeapYear = ((fullYear & 3) | (fullYear/100 & 3)) === 0 ? 1 : 0;
// (fullYear & 3) = (fullYear % 4), but faster
//Alternative:var isLeapYear=(new Date(currentYear,1,29,12)).getDate()===29?1:0
var fullMonth = dateInput.getMonth()|0;
return (
// 1. We know what the time since the Epoch really is
(+dateInput) // same as the dateInput.getTime() method
// 2. We know what the time since the Epoch at the start of the year is
- (+new Date(fullYear, 0)) // day defaults to 1 if not explicitly zeroed
// 3. Now, subtract what we would expect the time to be if daylight savings
// did not exist. This yields the time-offset due to daylight savings.
- ((
((
// Calculate the day of the year in the Gregorian calendar
// The code below works based upon the facts of signed right shifts
// • (x) >> n: shifts n and fills in the n highest bits with 0s
// • (-x) >> n: shifts n and fills in the n highest bits with 1s
// (This assumes that x is a positive integer)
-1 + // first day in the year is day 1
(31 & ((-fullMonth) >> 4)) + // January // (-11)>>4 = -1
((28 + isLeapYear) & ((1-fullMonth) >> 4)) + // February
(31 & ((2-fullMonth) >> 4)) + // March
(30 & ((3-fullMonth) >> 4)) + // April
(31 & ((4-fullMonth) >> 4)) + // May
(30 & ((5-fullMonth) >> 4)) + // June
(31 & ((6-fullMonth) >> 4)) + // July
(31 & ((7-fullMonth) >> 4)) + // August
(30 & ((8-fullMonth) >> 4)) + // September
(31 & ((9-fullMonth) >> 4)) + // October
(30 & ((10-fullMonth) >> 4)) + // November
// There are no months past December: the year rolls into the next.
// Thus, fullMonth is 0-based, so it will never be 12 in Javascript
(dateInput.getDate()|0) // get day of the month
)&0xffff) * 24 * 60 // 24 hours in a day, 60 minutes in an hour
+ (dateInput.getHours()&0xff) * 60 // 60 minutes in an hour
+ (dateInput.getMinutes()&0xff)
)|0) * 60 * 1000 // 60 seconds in a minute * 1000 milliseconds in a second
- (dateInput.getSeconds()&0xff) * 1000 // 1000 milliseconds in a second
- dateInput.getMilliseconds()
);
}
const parseDuration = (value) => {
let type = typeof(value);
if(value && type==="object") {
if(value instanceof D) return {...D};
if(value instanceof Period) {
value = value.valueOf();
type = typeof(value);
}
}
if(type==="number") return {ms:value,counts:[ms],types:["ms"]};
if(type==="string") {
const parts = value.split(" ");
let ms = 0, counts = [], types = [];
for(const part of parts) {
const num = parseFloat(part),
suffix = part.match(/\d([mshdwqy]+[os]?)+/)[1];
if(typeof(num)==="number" && !isNaN(num) && suffix in durationMilliseconds) {
ms += durationMilliseconds[suffix] * num;
counts.push(num);
types.push(suffix);
} else {
throw new TypeError(`${part} in ${value} is not a valid duration ${JSON.stringify(part.match(/\d([mshdqy]+[os]?)+/))}`)
}
}
return {ms,counts,types};
}
throw new TypeError(`Invalid duration: ${value}`)
};
function Clock(date=new Date(),{tz,hz=60,tick=1000/hz,run,sync=true,alarms=[]}={}) {
const type = typeof(date);
if(!date || (type!=="number" && !isDate(date))) throw new TypeError(`Clock() expects a Date or number of ms not ${JSON.stringify(date)}`);
//const offset = type === "number" ? 0 : dstOffsetAtDate(date);
//console.log("offset",offset);
//const offset = tz ? getTimezoneOffset(date,tz) : 0;
//date = new Date(type==="number" ? date : date.getTime()+offset*60*1000);
date = new Date(type==="number" ? date : date.getTime());
const stats = {
initialDate:new Date(date),
hz,
tick,
sync,
cycles:[],
alarms:[...alarms]
};
let interval;
if(tz) {
stats.tz = tz;
const _toString = date.toString,
toString = () => {
//const dt = new Date(date.getTime() + offset);
const dt = date;
const string = _toString.call(date),
offset = proxy.getTimezoneOffset() / 60,
fraction = offset % 1,
minutes = (1 - fraction) >= .017 ? `${Math.round(fraction * 60)}` : "00", // .017 = 1 minute
hours = `${Math.abs(Math.round(offset))}`,
gmt = `GMT${offset>0 ? "-" : "+"}${hours.padStart(2,"0")}${minutes.padStart(2,"0")}`,
zone = tz ? ` (${tz} ${proxy.isInDST ? "Daylight Time" : "Standard Time"})` : "";
return string.replace(/GMT.*/g,gmt) + zone;
};
Object.defineProperty(date,"toString",{configurable:true,writable:true,value:toString});
}
const extensions = {
addMilliseconds(value) {
date.setTime(date.getTime()+value);
return proxy;
},
addSeconds(value) {
date.setTime(date.getTime()+value*1000);
return proxy;
},
addMinutes(value) {
date.setTime(date.getTime()+value*1000*60);
return proxy;
},
addHours(value) {
date.setTime(date.getTime()+value*1000*60*60);
return proxy;
},
addDays(value) {
if(value===0) {
return proxy;
}
if(value<0) {
return proxy.subtractDays(-value);
}
if(value<1) {
return proxy.addHours(value * 24);
}
const daysinmonth = proxy.daysInMonth,
day = date.getDate(),
restofmonth = daysinmonth - day;
if(value>restofmonth) {
proxy.addMonths(1);
date.setDate(1);
return proxy.addDays((value - 1) - restofmonth)
}
date.setDate(day + value);
return proxy;
},
addWeeks(value) {
return proxy.addDays(value/7);
},
addMonths(value) {
if(value<=0) return proxy;
if(value<1) {
return proxy.addDays(value * proxy.daysInMonth)
}
const month = date.getMonth(),
restofyear = 11 - month;
if(value>restofyear) {
proxy.addYears(1);
date.setMonth(0);
return proxy.addMonths((value - 1) - restofyear);
}
date.setMonth(month + value);
return proxy;
},
addQuarters(value) {
return proxy.addMonths(value / 4);
},
addYears(value) {
if(value>=1) {
const future = date.getFullYear() + Math.trunc(value);
// future is not a leap year and current is a leap year and Feb 29th
if((future % 4 || !(future % 100) && future % 400) && proxy.isInLeapYear && date.getMonth()==1 && date.getDate()===29) {
date.setDate(28);
}
date.setYear(future);
}
if((value % 1) > 0) {
proxy.addDays((value % 1) * 365.2424177);
}
return proxy;
},
subtractMilliseconds(value) {
date.setTime(date.getTime()-value);
return proxy;
},
subtractSeconds(value) {
date.setTime(date.getTime()-value*1000);
return proxy;
},
subtractMinutes(value) {
date.setTime(date.getTime()-value*1000*60);
return proxy;
},
subtractHours(value) {
date.setTime(date.getTime()-value*1000*60*60);
return proxy;
},
subtractDays(value) {
if(value===0) {
return proxy;
}
if(value<0) {
return proxy.addDays(-value);
}
if(value<1) {
return proxy.subtractHours(value * 24);
}
const daysinmonth = proxy.daysInMonth,
day = date.getDate(),
startofmonth = Math.abs(day - daysinmonth);
if(value>startofmonth) {
proxy.subtractMonths(1);
date.setDate(1);
return proxy.subtractDays((value - 1) - startofmonth);
}
date.setDate(day - value);
return proxy;
},
subtractWeeks(value) {
return proxy.subtractDays(value/7);
},
subtractMonths(value) {
if(value<=0) return proxy;
if(value<1) {
return proxy.subtractDays(value * proxy.daysInMonth)
}
const month = date.getMonth(),
startofyear = Math.abs(month - 11);
if(value>startofyear) {
date.setMonth(11);
proxy.subtractYears(1);
return proxy.subtractMonths((value - 1) - startofyear);
}
date.setMonth(month - value);
return proxy;
},
subtractQuarters(value) {
return proxy.subtractMonths(value / 4);
},
subtractYears(value) {
const past = date.getFullYear() - value;
// future is not a leap year and current is a leap year and Feb 29th
if((past % 4 || !(past % 100) && past % 400) && proxy.isInLeapYear && date.getMonth()==1 && date.getDate()===29) {
date.setDate(28);
}
date.setYear(past);
proxy.subtractDays((past % 1) * 365.2424177);
return proxy;
},
plus(duration,times=1) {
const type = typeof(duration);
if(type!=="number" && type!=="string" && !D.is(duration) && !Period.is(duration)) {
throw new TypeError(`${JSON.stringify(duration)} is not a duration and can't be coerced into one`);
}
if(type==="number" || type==="string") duration = D(duration);
duration.types.forEach((type,i) => {
type = types[type];
const count = duration.counts[i];
if(count>0) {
proxy["add"+type](count);
} else if(count<0) {
proxy["subtract"+type](-count);
}
})
return this;
},
minus(duration) {
const type = typeof(duration);
if(type!=="number" && type!=="string" && !D.is(duration) && !Period.is(duration)) {
throw new TypeError(`${JSON.stringify(duration)} is not a duration and can't be coerced into one`);
}
if(type==="number" || type==="string") duration = D(duration);
duration.types.forEach((type,i) => {
type = types[type];
const count = duration.counts[i];
if(count>0) {
proxy["subtract"+type](count);
} else if(count<0) {
proxy["add"+type](-count);
}
})
return this;
},
clone(options= {tz,hz,tick,alarms}) {
const {tz,hz,tick,alarms} = options,
thereDate = new Clock(new Date(),{tz}),
thereOffset = thereDate.getTimezoneOffset(),
offset = proxy.getTimezoneOffset() - thereOffset,
newDate = new Date(date.getTime() + offset * 1000 * 60)
return new Clock(newDate,{tz,hz,tick,alarms});
},
endOf(period) {
if(period.length>2) period = durations[period+"s"];
const ms = date.getTime();
if(period==="s") {
date.setMilliseconds(999);
} else if(period==="m") {
date.setSeconds(59);
date.setMilliseconds(999);
} else if(period==="h") {
date.setMinutes(59);
date.setSeconds(59);
date.setMilliseconds(999);
} else if(period==="d") {
date.setHours(59);
date.setMinutes(59);
date.setSeconds(59);
date.setMilliseconds(999);
} else if(period==="w") {
throw new Error("endOf('w') not implemented");
} else if(period==="mo") {
date.setHours(59);
date.setMinutes(59);
date.setSeconds(59);
date.setMilliseconds(999)
date.setDate(proxy.daysInMonth);
} else if(period==="q") {
throw new Error("endOf('q') not implemented");
} else if(period==="y") {
date.setHours(59);
date.setMinutes(59);
date.setSeconds(59);
date.setMilliseconds(999)
date.setDate(proxy.daysInMonth);
date.setMonth(11);
}
return proxy;
},
startOf(period) {
if(period.length>2) period = durations[period+"s"];
if(period==="s") {
date.setMilliseconds(0);
} else if(period==="m") {
date.setSeconds(0);
date.setMilliseconds(0);
} else if(period==="h") {
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
} else if(period==="d") {
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
} else if(period==="w") {
throw new Error("startOf('w') not implemented");
} else if(period==="mo") {
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0)
date.setDate(1);
} else if(period==="q") {
throw new Error("startOf('q') not implemented");
} else if(period==="y") {
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0)
date.setDate(1);
date.setMonth(0);
}
return proxy;
},
getTimezoneOffset() {
const thereLocaleStr = date.toLocaleString('en-US', {timeZone: tz}),
thereDate = new Date(thereLocaleStr),
diff = thereDate.getTime() - date.getTime();
return Math.round(date.getTimezoneOffset() - (diff / (1000 * 60)));
//return getTimezoneOffset(date);
},
stop() {
const cycle = stats.cycles[stats.cycles.length-1];
cycle.time.stop = Date.now();
cycle.clockTime.stop = date.getTime();
cycle.skew = cycle.clockTime.stop - cycle.time.stop;
clearInterval(interval);
interval = null;
return proxy;
},
start(options={hz,tick,sync}) {
const {hz=60,tick=1000/hz,sync=false,reverse=tick<0} = options,
cycle = {hz,tick,sync,time:{start:Date.now()},clockTime:{start:date.getTime()},reverse},
previousCycle = stats.cycles[stats.cycles.length-1];
if(Math.round(tick)===0) throw new Error("Clock tick cannot be or round to 0");
if(previousCycle && previousCycle.reverse!==reverse) {
alarms.forEach((alarm) => alarm.executed = false);
}
stats.cycles.push(cycle);
if(interval) this.stop();
let previousTime = Date.now();
interval = setInterval(() => {
const now = Date.now();
if(sync) {
const diff = now - previousTime;
date.setTime(date.getTime() + ((reverse && tick>0) || (!reverse && tick<0) ? -1 * diff : diff));
}
previousTime = now;
proxy.plus(Math.round((reverse && tick>0) || (!reverse && tick<0) ? -1 * tick : tick));
stats.alarms.forEach((alarm) => {
const {condition,action,executed} = alarm;
if(!executed) {
let execute;
if(condition.for instanceof Date) {
const clock = Clock(condition.for,{tz:stats.tz});
execute = proxy.getTime() >= clock.getTime()
} else if(condition.for instanceof Period) {
const start = Clock(condition.for.start,{tz:stats.tz}),
end = Clock(condition.for.end,{tz:stats.tz}),
time = proxy.getTime();
execute = time >= start.getTime() && time <= end.getTime();
} else {
throw new TypeError(`Alarm condition.for must be a Date or Period not ${JSON.stringify(condition.for)}`);
}
if (execute) {
alarm.executed = execute;
if(typeof action==="function") setTimeout(() => action(proxy,true));
const handlers = listeners["alarm"];
if(handlers) {
for(const handler of handlers) {
const event = {
type:"alarm",
time:date.getTime(),
oldTime,
target:proxy,
alarm:action
}
setTimeout(() => handler(event));
}
}
}
}
})
const handlers = listeners["tick"];
if(handlers) {
for(const handler of handlers) {
const event = {
type:"tick",
time:date.getTime(),
oldTime,
target:proxy
}
setTimeout(() => handler(event));
}
}
},1000/Math.abs(hz));
return proxy;
},
reset(options={hz,tick,sync,run}) {
const {hz=60,tick=1000/hz,sync,run} = options,
cycle = stats.cycles[stats.cycles.length-1];
if(interval) this.stop();
stats.alarms.forEach((alarm) => alarm.executed=false)
date = new Date(stats.initialDate);
if(run) this.start({hz,tick,sync});
return proxy;
},
setAlarm(condition,action,name=action.name) {
stats.alarms.push({condition,action,name});
return proxy;
}
};
const listeners = {};
const proxy = new Proxy(date,{
get(target,property) {
if(property==="toString") return target.toString.bind(date);
let value = extensions[property];
if(value!==undefined) return value
if(property==="length") return proxy.getTime() + proxy.getTimezoneOffset() * 1000 * 60; // ?? whyc add offset?
if(property==="addEventListener") {
return (type,listener) => {
const handlers = listeners[type] ||= new Set();
handlers.add(listener);
return proxy;
}
};
if(property==="removeEventListener") {
return (type,listener) => {
const handlers = listeners[type] ||= new Set();
handlers.delete(listener);
return proxy;
}
};
if(property==="weekDay") return target.getDay()+1;
if(property==="dayOfMonth") return target.getDate();
if(property==="dayOfYear" || property==="ordinal") {
return Math.floor((Date.UTC(target.getFullYear(), target.getMonth(), target.getDate()) - Date.UTC(target.getFullYear(), 0, 0)) / durationMilliseconds["d"])
}
if(property==="daysInMonth") {
return new Date(date.getFullYear(), date.getMonth()+1, 0). getDate();
}
if(property==="isInLeapYear") {
const year = date.getFullYear();
return !(year % 4 || !(year % 100) && year % 400);
}
if(property==="isInDST") {
return !!dstOffsetAtDate(date);
}
if(property==="offset") return date.getTimezoneOffset();
if(property==="weekOfYear") {
return Math.floor((Date.UTC(target.getFullYear(), target.getMonth(), target.getDate()) - Date.UTC(target.getFullYear(), 0, 0)) / durationMilliseconds["w"])
}
if(property==="stats") {
const cycles = stats.cycles.map((cycle) => { return {...cycle,time:{...cycle.time},clockTime:{...cycle.clockTime}}}),
alarms = stats.alarms.map(({condition,action}) => { return {condition:{...condition},action}; });
return {...stats,cycles,alarms};
}
value = date[property];
if(typeof(value)==="function") {
if(property.startsWith("get")) return (timezone) => timezone ? proxy.clone({tz:timezone})[property]() : value.call(date);
return value.bind(date);
}
if(typeof(property)==="string") {
let fname = "get" + property[0].toUpperCase() + property.substring(1);
if(typeof(target[fname])==="function") return date[fname]();
fname += "s";
if(typeof(target[fname])==="function") return date[fname]();
}
return value;
}
});
if(run) extensions.start();
return proxy;
}
Clock.min = (...items) => {
const mintime = items.reduce((mintime,item) => Math.min(item.length,mintime),Infinity);
return items.reduce((min,item) => {
if(item.length===mintime) min.push(item);
return min;
},[])
}
Clock.max = (...items) => {
const maxtime = items.reduce((maxtime,item) => Math.max(item.length,maxtime),-Infinity);
return items.reduce((max,item) => {
if(item.length===maxtime) max.push(item);
return max;
},[])
}
function D(value,type) {
if(!this || !(this instanceof D)) return new D(value,type);
let duration = value;
if(isDate(duration)) {
if(type && type!=="ms") throw new TypeError(`A date can't be used to initialize ${type}`);
duration = value.getTime()+"ms";
} else if(typeof(duration)==="number") {
duration += type||"ms";
}
const {ms,counts,types} = parseDuration(duration);
//Object.defineProperty(this,"type",{get() { return type; }});
Object.assign(this,{ms,counts,types});
Object.defineProperty(this,"valueOf",{value:() => ms});
Object.defineProperty(this,"toJSON",{value:() => duration});
Object.defineProperty(this,"length",{get() { return this.valueOf();}});
return this;
}
D.is = (value) => value && typeof(value)==="object" && value instanceof D;
D.min = Clock.min;
D.max = Clock.max;
D.prototype.plus = function(duration,times=1) {
const type = typeof(duration);
if(type!=="number" && type!=="string" && !D.is(duration) && !Period.is(duration)) {
throw new TypeError(`${JSON.stringify(duration)} is not a duration and can't be coerced into one`);
}
if(type==="number" || type==="string" || duration instanceof Date) duration = D(duration);
return D(this.valueOf()+(duration * times));
},
D.prototype.minus = function(duration) {
return this.plus(duration,-1);
},
D.prototype.to = function(type="ms") {
const value = this.valueOf();
if(type==="Date") {
if(this instanceof Period) throw new TypeError(`Cannot convert Period to Date`);
return new Date(value);
}
if(!(type in durationMilliseconds)) throw new TypeError(`${type} is not a valid duration type`);
return value / durationMilliseconds[type];
}
Object.entries(durations).forEach(([key,value]) => {
Object.defineProperty(D.prototype,key,{get:function() { return this.to(value); }})
if(value!=="ms") Object.defineProperty(D.prototype,value,{get:function() { return this.to(value); }})
})
Object.defineProperty(D.prototype,"Date",{get:function() { return this.to("Date");}})
D.durations = durationMilliseconds;
function Period({start,end}) {
if(!this || !(this instanceof Period)) return new Period({start,end});
Object.defineProperty(this,"start",{
set(value) {
const type = typeof(value);
if (!value || (type!=="number" && (type !== "object" || !(value instanceof Date)))) throw new TypeError(`Period boundary must be a Date or number`);
start = new Clock(value);
},
get() {
return start;
}
});
Object.defineProperty(this,"end",{
set(value) {
const type = typeof(value);
if (!value || (type!=="number" && (type !== "object" || !(value instanceof Date)))) throw new TypeError(`Period boundary must be a Date or number`);
end = new Clock(value);
},
get() {
return end;
}
});
Object.defineProperty(this,"valueOf",{ value:() => {
const t1 = start.getTime(),
t2 = end.getTime();
return Math.max(t1,t2)-Math.min(t1,t2);
}});
Object.defineProperty(this,"toJSON",{value: () => { return {start,end}}});
Object.defineProperty(this,"length",{get() { return this.valueOf();}});
this.start = start;
this.end = end;
}
Period.is = (value) => value && typeof(value)==="object" && value instanceof Period;
Period.min = D.min;
Period.max = D.max;
Period.prototype.to = function(duration) {
if(Object.entries(durations).some(([key,value]) => duration===key || duration===value)) {
return (new D(this.length)).to(duration);
}
throw new TypeError(`${duration} is not a valid duration type`);
};
Object.entries(durations).forEach(([key,value]) => {
Object.defineProperty(Period.prototype,key,{get:function() { return this.to(value); }})
Object.defineProperty(Period.prototype,value,{get:function() { return this.to(value); }})
});
Period.prototype.shift = function(amount,{pure=true}={}) {
if(pure) {
const start = this.start.clone().plus(amount),
end = this.end.clone().plus(amount);
return new Period({start,end});
}
this.start.plus(amount),
this.end.plus(amount);
return this;
}
Period.prototype.extend = function(amount,{pure=true}={}) {
if(amount.valueOf()<0) {
if(pure) {
const start = this.start.clone().plus(amount);
return Period({start,end:this.end});
}
this.start.plus(amount);
} else {
if(pure) {
const end = this.end.clone().plus(amount);
return Period({start:this.start,end});
}
this.end.plus(amount);
}
return this;
}
// intersection
// union
// difference
const Timewave = {
Clock,
Period,
D
};
if(typeof(self)!=="undefined") {
self.Timewave = Timewave;
}
export {Clock,Period,D,Timewave as default}