-
Notifications
You must be signed in to change notification settings - Fork 95
/
countdown.js
1357 lines (1151 loc) · 27.8 KB
/
countdown.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global window module */
/**
* @license countdown.js v2.6.1 http://countdownjs.org
* Copyright (c)2006-2014 Stephen M. McKamey.
* Licensed under The MIT License.
*/
/*jshint bitwise:false */
/**
* API entry
* @public
* @param {function(Object)|Date|number} start the starting date
* @param {function(Object)|Date|number} end the ending date
* @param {number} units the units to populate
* @return {Object|number}
*/
var countdown = (
function() {
/*jshint smarttabs:true */
'use strict';
/**
* @private
* @const
* @type {number}
*/
var MILLISECONDS = 0x001;
/**
* @private
* @const
* @type {number}
*/
var SECONDS = 0x002;
/**
* @private
* @const
* @type {number}
*/
var MINUTES = 0x004;
/**
* @private
* @const
* @type {number}
*/
var HOURS = 0x008;
/**
* @private
* @const
* @type {number}
*/
var DAYS = 0x010;
/**
* @private
* @const
* @type {number}
*/
var WEEKS = 0x020;
/**
* @private
* @const
* @type {number}
*/
var MONTHS = 0x040;
/**
* @private
* @const
* @type {number}
*/
var YEARS = 0x080;
/**
* @private
* @const
* @type {number}
*/
var DECADES = 0x100;
/**
* @private
* @const
* @type {number}
*/
var CENTURIES = 0x200;
/**
* @private
* @const
* @type {number}
*/
var MILLENNIA = 0x400;
/**
* @private
* @const
* @type {number}
*/
var DEFAULTS = YEARS|MONTHS|DAYS|HOURS|MINUTES|SECONDS;
/**
* @private
* @const
* @type {number}
*/
var MILLISECONDS_PER_SECOND = 1000;
/**
* @private
* @const
* @type {number}
*/
var SECONDS_PER_MINUTE = 60;
/**
* @private
* @const
* @type {number}
*/
var MINUTES_PER_HOUR = 60;
/**
* @private
* @const
* @type {number}
*/
var HOURS_PER_DAY = 24;
/**
* @private
* @const
* @type {number}
*/
var MILLISECONDS_PER_DAY = HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE * MILLISECONDS_PER_SECOND;
/**
* @private
* @const
* @type {number}
*/
var DAYS_PER_WEEK = 7;
/**
* @private
* @const
* @type {number}
*/
var MONTHS_PER_YEAR = 12;
/**
* @private
* @const
* @type {number}
*/
var YEARS_PER_DECADE = 10;
/**
* @private
* @const
* @type {number}
*/
var DECADES_PER_CENTURY = 10;
/**
* @private
* @const
* @type {number}
*/
var CENTURIES_PER_MILLENNIUM = 10;
/**
* @private
* @param {number} x number
* @return {number}
*/
var ceil = Math.ceil;
/**
* @private
* @param {number} x number
* @return {number}
*/
var floor = Math.floor;
/**
* @private
* @param {Date} ref reference date
* @param {number} shift number of months to shift
* @return {number} number of days shifted
*/
function borrowMonths(ref, shift) {
var prevTime = ref.getTime();
// increment month by shift
ref.setMonth( ref.getMonth() + shift );
// this is the trickiest since months vary in length
return Math.round( (ref.getTime() - prevTime) / MILLISECONDS_PER_DAY );
}
/**
* @private
* @param {Date} ref reference date
* @return {number} number of days
*/
function daysPerMonth(ref) {
var a = ref.getTime();
// increment month by 1
var b = new Date(a);
b.setMonth( ref.getMonth() + 1 );
// this is the trickiest since months vary in length
return Math.round( (b.getTime() - a) / MILLISECONDS_PER_DAY );
}
/**
* @private
* @param {Date} ref reference date
* @return {number} number of days
*/
function daysPerYear(ref) {
var a = ref.getTime();
// increment year by 1
var b = new Date(a);
b.setFullYear( ref.getFullYear() + 1 );
// this is the trickiest since years (periodically) vary in length
return Math.round( (b.getTime() - a) / MILLISECONDS_PER_DAY );
}
/**
* Applies the Timespan to the given date.
*
* @private
* @param {Timespan} ts
* @param {Date=} date
* @return {Date}
*/
function addToDate(ts, date) {
date = (date instanceof Date) || ((date !== null) && isFinite(date)) ? new Date(+date) : new Date();
if (!ts) {
return date;
}
// if there is a value field, use it directly
var value = +ts.value || 0;
if (value) {
date.setTime(date.getTime() + value);
return date;
}
value = +ts.milliseconds || 0;
if (value) {
date.setMilliseconds(date.getMilliseconds() + value);
}
value = +ts.seconds || 0;
if (value) {
date.setSeconds(date.getSeconds() + value);
}
value = +ts.minutes || 0;
if (value) {
date.setMinutes(date.getMinutes() + value);
}
value = +ts.hours || 0;
if (value) {
date.setHours(date.getHours() + value);
}
value = +ts.weeks || 0;
if (value) {
value *= DAYS_PER_WEEK;
}
value += +ts.days || 0;
if (value) {
date.setDate(date.getDate() + value);
}
value = +ts.months || 0;
if (value) {
date.setMonth(date.getMonth() + value);
}
value = +ts.millennia || 0;
if (value) {
value *= CENTURIES_PER_MILLENNIUM;
}
value += +ts.centuries || 0;
if (value) {
value *= DECADES_PER_CENTURY;
}
value += +ts.decades || 0;
if (value) {
value *= YEARS_PER_DECADE;
}
value += +ts.years || 0;
if (value) {
date.setFullYear(date.getFullYear() + value);
}
return date;
}
/**
* @private
* @const
* @type {number}
*/
var LABEL_MILLISECONDS = 0;
/**
* @private
* @const
* @type {number}
*/
var LABEL_SECONDS = 1;
/**
* @private
* @const
* @type {number}
*/
var LABEL_MINUTES = 2;
/**
* @private
* @const
* @type {number}
*/
var LABEL_HOURS = 3;
/**
* @private
* @const
* @type {number}
*/
var LABEL_DAYS = 4;
/**
* @private
* @const
* @type {number}
*/
var LABEL_WEEKS = 5;
/**
* @private
* @const
* @type {number}
*/
var LABEL_MONTHS = 6;
/**
* @private
* @const
* @type {number}
*/
var LABEL_YEARS = 7;
/**
* @private
* @const
* @type {number}
*/
var LABEL_DECADES = 8;
/**
* @private
* @const
* @type {number}
*/
var LABEL_CENTURIES = 9;
/**
* @private
* @const
* @type {number}
*/
var LABEL_MILLENNIA = 10;
/**
* @private
* @type {Array}
*/
var LABELS_SINGLUAR;
/**
* @private
* @type {Array}
*/
var LABELS_PLURAL;
/**
* @private
* @type {string}
*/
var LABEL_LAST;
/**
* @private
* @type {string}
*/
var LABEL_DELIM;
/**
* @private
* @type {string}
*/
var LABEL_NOW;
/**
* Formats a number & unit as a string
*
* @param {number} value
* @param {number} unit
* @return {string}
*/
var formatter;
/**
* Formats a number as a string
*
* @private
* @param {number} value
* @return {string}
*/
var formatNumber;
/**
* @private
* @param {number} value
* @param {number} unit unit index into label list
* @return {string}
*/
function plurality(value, unit) {
return formatNumber(value)+((value === 1) ? LABELS_SINGLUAR[unit] : LABELS_PLURAL[unit]);
}
/**
* Formats the entries with singular or plural labels
*
* @private
* @param {Timespan} ts
* @return {Array}
*/
var formatList;
/**
* Timespan representation of a duration of time
*
* @private
* @this {Timespan}
* @constructor
*/
function Timespan() {}
/**
* Formats the Timespan as a sentence
*
* @param {string=} emptyLabel the string to use when no values returned
* @return {string}
*/
Timespan.prototype.toString = function(emptyLabel) {
var label = formatList(this);
var count = label.length;
if (!count) {
return emptyLabel ? ''+emptyLabel : LABEL_NOW;
}
if (count === 1) {
return label[0];
}
var last = LABEL_LAST+label.pop();
return label.join(LABEL_DELIM)+last;
};
/**
* Formats the Timespan as a sentence in HTML
*
* @param {string=} tag HTML tag name to wrap each value
* @param {string=} emptyLabel the string to use when no values returned
* @return {string}
*/
Timespan.prototype.toHTML = function(tag, emptyLabel) {
tag = tag || 'span';
var label = formatList(this);
var count = label.length;
if (!count) {
emptyLabel = emptyLabel || LABEL_NOW;
return emptyLabel ? '<'+tag+'>'+emptyLabel+'</'+tag+'>' : emptyLabel;
}
for (var i=0; i<count; i++) {
// wrap each unit in tag
label[i] = '<'+tag+'>'+label[i]+'</'+tag+'>';
}
if (count === 1) {
return label[0];
}
var last = LABEL_LAST+label.pop();
return label.join(LABEL_DELIM)+last;
};
/**
* Applies the Timespan to the given date
*
* @param {Date=} date the date to which the timespan is added.
* @return {Date}
*/
Timespan.prototype.addTo = function(date) {
return addToDate(this, date);
};
/**
* Formats the entries as English labels
*
* @private
* @param {Timespan} ts
* @return {Array}
*/
formatList = function(ts) {
var list = [];
var value = ts.millennia;
if (value) {
list.push(formatter(value, LABEL_MILLENNIA));
}
value = ts.centuries;
if (value) {
list.push(formatter(value, LABEL_CENTURIES));
}
value = ts.decades;
if (value) {
list.push(formatter(value, LABEL_DECADES));
}
value = ts.years;
if (value) {
list.push(formatter(value, LABEL_YEARS));
}
value = ts.months;
if (value) {
list.push(formatter(value, LABEL_MONTHS));
}
value = ts.weeks;
if (value) {
list.push(formatter(value, LABEL_WEEKS));
}
value = ts.days;
if (value) {
list.push(formatter(value, LABEL_DAYS));
}
value = ts.hours;
if (value) {
list.push(formatter(value, LABEL_HOURS));
}
value = ts.minutes;
if (value) {
list.push(formatter(value, LABEL_MINUTES));
}
value = ts.seconds;
if (value) {
list.push(formatter(value, LABEL_SECONDS));
}
value = ts.milliseconds;
if (value) {
list.push(formatter(value, LABEL_MILLISECONDS));
}
return list;
};
/**
* Borrow any underflow units, carry any overflow units
*
* @private
* @param {Timespan} ts
* @param {string} toUnit
*/
function rippleRounded(ts, toUnit) {
switch (toUnit) {
case 'seconds':
if (ts.seconds !== SECONDS_PER_MINUTE || isNaN(ts.minutes)) {
return;
}
// ripple seconds up to minutes
ts.minutes++;
ts.seconds = 0;
/* falls through */
case 'minutes':
if (ts.minutes !== MINUTES_PER_HOUR || isNaN(ts.hours)) {
return;
}
// ripple minutes up to hours
ts.hours++;
ts.minutes = 0;
/* falls through */
case 'hours':
if (ts.hours !== HOURS_PER_DAY || isNaN(ts.days)) {
return;
}
// ripple hours up to days
ts.days++;
ts.hours = 0;
/* falls through */
case 'days':
if (ts.days !== DAYS_PER_WEEK || isNaN(ts.weeks)) {
return;
}
// ripple days up to weeks
ts.weeks++;
ts.days = 0;
/* falls through */
case 'weeks':
if (ts.weeks !== daysPerMonth(ts.refMonth)/DAYS_PER_WEEK || isNaN(ts.months)) {
return;
}
// ripple weeks up to months
ts.months++;
ts.weeks = 0;
/* falls through */
case 'months':
if (ts.months !== MONTHS_PER_YEAR || isNaN(ts.years)) {
return;
}
// ripple months up to years
ts.years++;
ts.months = 0;
/* falls through */
case 'years':
if (ts.years !== YEARS_PER_DECADE || isNaN(ts.decades)) {
return;
}
// ripple years up to decades
ts.decades++;
ts.years = 0;
/* falls through */
case 'decades':
if (ts.decades !== DECADES_PER_CENTURY || isNaN(ts.centuries)) {
return;
}
// ripple decades up to centuries
ts.centuries++;
ts.decades = 0;
/* falls through */
case 'centuries':
if (ts.centuries !== CENTURIES_PER_MILLENNIUM || isNaN(ts.millennia)) {
return;
}
// ripple centuries up to millennia
ts.millennia++;
ts.centuries = 0;
/* falls through */
}
}
/**
* Ripple up partial units one place
*
* @private
* @param {Timespan} ts timespan
* @param {number} frac accumulated fractional value
* @param {string} fromUnit source unit name
* @param {string} toUnit target unit name
* @param {number} conversion multiplier between units
* @param {number} digits max number of decimal digits to output
* @return {number} new fractional value
*/
function fraction(ts, frac, fromUnit, toUnit, conversion, digits) {
if (ts[fromUnit] >= 0) {
frac += ts[fromUnit];
delete ts[fromUnit];
}
frac /= conversion;
if (frac + 1 <= 1) {
// drop if below machine epsilon
return 0;
}
if (ts[toUnit] >= 0) {
// ensure does not have more than specified number of digits
ts[toUnit] = +(ts[toUnit] + frac).toFixed(digits);
rippleRounded(ts, toUnit);
return 0;
}
return frac;
}
/**
* Ripple up partial units to next existing
*
* @private
* @param {Timespan} ts
* @param {number} digits max number of decimal digits to output
*/
function fractional(ts, digits) {
var frac = fraction(ts, 0, 'milliseconds', 'seconds', MILLISECONDS_PER_SECOND, digits);
if (!frac) { return; }
frac = fraction(ts, frac, 'seconds', 'minutes', SECONDS_PER_MINUTE, digits);
if (!frac) { return; }
frac = fraction(ts, frac, 'minutes', 'hours', MINUTES_PER_HOUR, digits);
if (!frac) { return; }
frac = fraction(ts, frac, 'hours', 'days', HOURS_PER_DAY, digits);
if (!frac) { return; }
frac = fraction(ts, frac, 'days', 'weeks', DAYS_PER_WEEK, digits);
if (!frac) { return; }
frac = fraction(ts, frac, 'weeks', 'months', daysPerMonth(ts.refMonth)/DAYS_PER_WEEK, digits);
if (!frac) { return; }
frac = fraction(ts, frac, 'months', 'years', daysPerYear(ts.refMonth)/daysPerMonth(ts.refMonth), digits);
if (!frac) { return; }
frac = fraction(ts, frac, 'years', 'decades', YEARS_PER_DECADE, digits);
if (!frac) { return; }
frac = fraction(ts, frac, 'decades', 'centuries', DECADES_PER_CENTURY, digits);
if (!frac) { return; }
frac = fraction(ts, frac, 'centuries', 'millennia', CENTURIES_PER_MILLENNIUM, digits);
// should never reach this with remaining fractional value
if (frac) { throw new Error('Fractional unit overflow'); }
}
/**
* Borrow any underflow units, carry any overflow units
*
* @private
* @param {Timespan} ts
*/
function ripple(ts) {
var x;
if (ts.milliseconds < 0) {
// ripple seconds down to milliseconds
x = ceil(-ts.milliseconds / MILLISECONDS_PER_SECOND);
ts.seconds -= x;
ts.milliseconds += x * MILLISECONDS_PER_SECOND;
} else if (ts.milliseconds >= MILLISECONDS_PER_SECOND) {
// ripple milliseconds up to seconds
ts.seconds += floor(ts.milliseconds / MILLISECONDS_PER_SECOND);
ts.milliseconds %= MILLISECONDS_PER_SECOND;
}
if (ts.seconds < 0) {
// ripple minutes down to seconds
x = ceil(-ts.seconds / SECONDS_PER_MINUTE);
ts.minutes -= x;
ts.seconds += x * SECONDS_PER_MINUTE;
} else if (ts.seconds >= SECONDS_PER_MINUTE) {
// ripple seconds up to minutes
ts.minutes += floor(ts.seconds / SECONDS_PER_MINUTE);
ts.seconds %= SECONDS_PER_MINUTE;
}
if (ts.minutes < 0) {
// ripple hours down to minutes
x = ceil(-ts.minutes / MINUTES_PER_HOUR);
ts.hours -= x;
ts.minutes += x * MINUTES_PER_HOUR;
} else if (ts.minutes >= MINUTES_PER_HOUR) {
// ripple minutes up to hours
ts.hours += floor(ts.minutes / MINUTES_PER_HOUR);
ts.minutes %= MINUTES_PER_HOUR;
}
if (ts.hours < 0) {
// ripple days down to hours
x = ceil(-ts.hours / HOURS_PER_DAY);
ts.days -= x;
ts.hours += x * HOURS_PER_DAY;
} else if (ts.hours >= HOURS_PER_DAY) {
// ripple hours up to days
ts.days += floor(ts.hours / HOURS_PER_DAY);
ts.hours %= HOURS_PER_DAY;
}
while (ts.days < 0) {
// NOTE: never actually seen this loop more than once
// ripple months down to days
ts.months--;
ts.days += borrowMonths(ts.refMonth, 1);
}
// weeks is always zero here
if (ts.days >= DAYS_PER_WEEK) {
// ripple days up to weeks
ts.weeks += floor(ts.days / DAYS_PER_WEEK);
ts.days %= DAYS_PER_WEEK;
}
if (ts.months < 0) {
// ripple years down to months
x = ceil(-ts.months / MONTHS_PER_YEAR);
ts.years -= x;
ts.months += x * MONTHS_PER_YEAR;
} else if (ts.months >= MONTHS_PER_YEAR) {
// ripple months up to years
ts.years += floor(ts.months / MONTHS_PER_YEAR);
ts.months %= MONTHS_PER_YEAR;
}
// years is always non-negative here
// decades, centuries and millennia are always zero here
if (ts.years >= YEARS_PER_DECADE) {
// ripple years up to decades
ts.decades += floor(ts.years / YEARS_PER_DECADE);
ts.years %= YEARS_PER_DECADE;
if (ts.decades >= DECADES_PER_CENTURY) {
// ripple decades up to centuries
ts.centuries += floor(ts.decades / DECADES_PER_CENTURY);
ts.decades %= DECADES_PER_CENTURY;
if (ts.centuries >= CENTURIES_PER_MILLENNIUM) {
// ripple centuries up to millennia
ts.millennia += floor(ts.centuries / CENTURIES_PER_MILLENNIUM);
ts.centuries %= CENTURIES_PER_MILLENNIUM;
}
}
}
}
/**
* Remove any units not requested
*
* @private
* @param {Timespan} ts
* @param {number} units the units to populate
* @param {number} max number of labels to output
* @param {number} digits max number of decimal digits to output
*/
function pruneUnits(ts, units, max, digits) {
var count = 0;
// Calc from largest unit to smallest to prevent underflow
if (!(units & MILLENNIA) || (count >= max)) {
// ripple millennia down to centuries
ts.centuries += ts.millennia * CENTURIES_PER_MILLENNIUM;
delete ts.millennia;
} else if (ts.millennia) {
count++;
}
if (!(units & CENTURIES) || (count >= max)) {
// ripple centuries down to decades
ts.decades += ts.centuries * DECADES_PER_CENTURY;
delete ts.centuries;
} else if (ts.centuries) {
count++;
}
if (!(units & DECADES) || (count >= max)) {
// ripple decades down to years
ts.years += ts.decades * YEARS_PER_DECADE;
delete ts.decades;
} else if (ts.decades) {
count++;
}
if (!(units & YEARS) || (count >= max)) {
// ripple years down to months
ts.months += ts.years * MONTHS_PER_YEAR;
delete ts.years;
} else if (ts.years) {
count++;
}
if (!(units & MONTHS) || (count >= max)) {
// ripple months down to days
if (ts.months) {
ts.days += borrowMonths(ts.refMonth, ts.months);
}
delete ts.months;
if (ts.days >= DAYS_PER_WEEK) {
// ripple day overflow back up to weeks
ts.weeks += floor(ts.days / DAYS_PER_WEEK);
ts.days %= DAYS_PER_WEEK;
}
} else if (ts.months) {
count++;
}
if (!(units & WEEKS) || (count >= max)) {
// ripple weeks down to days
ts.days += ts.weeks * DAYS_PER_WEEK;
delete ts.weeks;
} else if (ts.weeks) {
count++;
}
if (!(units & DAYS) || (count >= max)) {
//ripple days down to hours
ts.hours += ts.days * HOURS_PER_DAY;
delete ts.days;
} else if (ts.days) {
count++;
}
if (!(units & HOURS) || (count >= max)) {
// ripple hours down to minutes
ts.minutes += ts.hours * MINUTES_PER_HOUR;
delete ts.hours;
} else if (ts.hours) {
count++;
}
if (!(units & MINUTES) || (count >= max)) {
// ripple minutes down to seconds
ts.seconds += ts.minutes * SECONDS_PER_MINUTE;
delete ts.minutes;
} else if (ts.minutes) {
count++;
}
if (!(units & SECONDS) || (count >= max)) {
// ripple seconds down to milliseconds
ts.milliseconds += ts.seconds * MILLISECONDS_PER_SECOND;
delete ts.seconds;
} else if (ts.seconds) {
count++;
}
// nothing to ripple milliseconds down to
// so ripple back up to smallest existing unit as a fractional value
if (!(units & MILLISECONDS) || (count >= max)) {
fractional(ts, digits);
}
}
/**
* Populates the Timespan object
*
* @private
* @param {Timespan} ts
* @param {?Date} start the starting date
* @param {?Date} end the ending date
* @param {number} units the units to populate
* @param {number} max number of labels to output
* @param {number} digits max number of decimal digits to output