-
Notifications
You must be signed in to change notification settings - Fork 0
/
chronos.js
809 lines (639 loc) · 23.8 KB
/
chronos.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
/*============================================================================*\
Chronos - Timer and sheduler
Timer and scheduler for node JS.
NB: This code is deprecated and no longer maintained.
Please use **timexe** instead - A drop-in replacement:
* github: [github.com/paragi/timexe](https://github.com/paragi/timexe)
* npm: [npmjs.com/package/timexe](https://www.npmjs.com/package/timexe)
syntax: chronos.add(<time expression>, <call back>, <parameter to call back>);
Call with a time expression string and a callback function.
The time expression can produce a more complex reoccurring time pattern or a one-time shot, within years, seconds or even milliseconds.
His is not cron. Rather it is an attempt at updating the very successful cron syntax.
This version have a time resolution in milliseconds, but the syntax supports higher resolutions.
A basic time expression is a series of fields specifying the time(s):
<year> <month> <day> <hour> <minute> <second> <millisecond> <microsecond> ...
or a time stamp.
The time expression can contain wild-cards, ranges, sets, not flags and every flags. Plus some special flags for year days and week days.
Chronos are based on the setTimeout function. It is not very precise. At present it seems to have an accuracy within 25 ms, without significant process load. However it seems to defer execution time, during process load. In some cases that I preferable, but it makes the timing less precise.
To increase precision, you would have to rewrite the function with some form of correction to real time.
To add a timed job every day at noon:
[code]
chronos.add(”* * * 12”,function(){console.log(“hello wolrd”)});
[/code]
Each field can be substituted with a wild card “*” , have flags, ranges or a set of values.
The epoch timestamp is seconds since 1970-01-01 UTC with fractions of second as decimal part:
@<epoch>[.<faction of second>]
Field syntax: [!][-]<value>[-<value>]|[,<value>]
or /<value>
or * (Any)
Meaning:
" " : field separator
* : all values. Flags will be ignored.
! : not
/ : every (can not be combined with ! and range)
- : Negative values are counted back from the maximum value
a-b : range. both a and b included.
a,b : set of values
Day field can have the one of the following flags as well
y: day of year
w: day of week 1-7 (1 is Monday)
Unspecified minor fields are assumed to have the lowest possible value
Note!:
- Time expression are in local time where as time stamps are in UTC
- Month and weekday use another offset then the javascript Date function:
- Month 1 is January
- Week day 1-7 starting with Monday
- In the current implementation, using javascripts settimeout, the accuracy is
likely within 25 ms.
Examples:
Every hour: * * * *
every day at noon: * * * 12
Every 3th Hour on work days: * * w1-5 /3
At a specific epoch time: @1422821601.123
At a specific time: 2014 5 13 18 53 7 300 230
2th to last day of the month at noon: * * -2 12
3th last day of the year: * * y-3
3 times an hour during work time: * * w1-5 9-17 0,20,40
Every morning at 7:30 but not on a weekend: * * !6-7 7 30
every 10 minutes in the day time: * * * 8-18 /10
Functions:
chronos.add(<time expression>, <call back>, <parameter to call back>);
Add a single shot or reoccurring job
Returns an object:
result: “ok” or null
error: <an failure explanation> or null
id: <integer used to identify the timer>
chronos.remove(id);
Removes the job
Returns an object:
result: “ok” or null
error: <an failure explanation> or null
id: <integer used to identify the timer>
\*============================================================================*/
/*============================================================================*\
Public functions
\*============================================================================*/
var chronos = {};
// For node JS
if(typeof module !== 'undefined') module.exports = exports = chronos;
// Settings
chronos.file=''; // File to store permanent time expressions
chronos.timeResolution=2; // Min 1 ms. This is the minimum time between execution.
chronos.maxTimerDelay=86400000 // some javascripts engines cant handle more then 32 bit 0x7FFFFFF - about 28 days
// Globals
chronos.list={};
// Job object
// timex: Time expression
// action: function to execute
// next: Next execution time
// timer: handle to setTimeOut
/*============================================================================*\
Timer
Returns an associative array:
result: "ok" or null
error: error text
id: new job ID
Parameter is an associative array:
timex: Time expression
action: function to execute
p: parameters parsed action function
\*============================================================================*/
chronos.add=function(timex,action,param){
// validate parameters
if(typeof timex !== 'string')
return {"result":"failed","error":"time expression not a string","id":""};
if(typeof action !== 'function')
return {"result":"failed","error":"Action is not a function","id":""};
// Add to job entry to list;
chronos.list[chronos.nextId]={};
chronos.list[chronos.nextId].timex=timex;
chronos.list[chronos.nextId].action=action;
chronos.list[chronos.nextId].param=param;
// Define the timeout Shortened flag
chronos.list[chronos.nextId].timeoutShortened=false;
// Start timed execution
var error=chronos._start(chronos.nextId);
if(error.length>0) return {"result":"failed","error":error,"id":""};
// Add to static file
if(typeof process === 'object' && chronos.file.length>0){
// Look for match in file
// concatanate file....
}
return {"result":"ok","error":"","id":chronos.nextId++};
}
chronos.remove=function(id){
if(typeof chronos.list[id] !== "undefined"){
clearTimeout(chronos.list[id].timer);
delete chronos.list[id];
// Delete from static file
if(typeof process === 'object' && chronos.file.length>0){
// Look for match in file
// splice file....
}
}
return;
}
chronos.get=function(id){
if(typeof id ==='number') return chronos.list[id];
return chronos.list;
}
/*============================================================================*\
Internal functions
\*============================================================================*/
// Globals
chronos.nextId=1;
// Constants
var limit = [
[1970, 3000]
,[1, 12]
,[1, 31]
,[0, 23]
,[0, 59]
,[0, 59]
,[0, 999]
,[0, 999]
,[0, 999]
,[0, 999]
,[1, 7] // Week day
,[1, 366] // Day of year
,[1, 53] // Week number
];
var fieldName=['year', 'month', 'day', 'hour', 'min', 'sec', 'ms','weekday','yearday','week'];
// start on load
/*============================================================================*\
Start timer for job
\*============================================================================*/
chronos._start=function(id){
// Get now
var now = (new Date).getTime()/1000;
var starFromTime=now;
var delay;
if(typeof chronos.list[id]=== 'undefined') console.log("undef : ",id);
// Set next execution time
if(!chronos.list[id].timeoutShortened){
// To avoid multiple hits and getting out of sync: set time to after last
// execution time (do to timing errors)
if(typeof chronos.list[id].next !== 'undefined')
if(starFromTime <= chronos.list[id].next)
starFromTime = chronos.list[id].next+chronos.timeResolution;
// get next execution time
var result=chronos.nextTime(chronos.list[id].timex,starFromTime);
if(result.error.length>0){
return result.error;
}
if(result.time < now){
chronos.remove(id);
return "time expression did not produce a valid future time";
}
chronos.list[id].next=result.time;
}
// Start timer for next execution
delay=parseInt((chronos.list[id].next-now)*1000);
chronos.list[id].timeoutShortened=false;
// In some engines, the delay can not be above 32 bit integer
if (delay>chronos.maxTimerDelay){
delay=chronos.maxTimerDelay;
// Let the _run function know
chronos.list[id].timeoutShortened=true;
}
chronos.list[id].delay=delay;
chronos.list[id].now=now;
chronos.list[id].timer=setTimeout(chronos._run, delay, id);
return '';
}
/*============================================================================*\
Run timed action
called by settimeout()
\*============================================================================*/
chronos._run=function(id){
if(typeof id !== 'number'){
console.error("Schedule timer fired with null id.");
return;
}
if(typeof chronos.list[id] !== 'object'){
console.error("Schedule timer fired with invalid id: " + id);
return;
}
// Execute action function
if(!chronos.list[id].timeoutShortened){
chronos.list[id].action(chronos.list[id].param);
}
// Restart timer
chronos._start(id);
}
/*============================================================================*\
Find next time (time exp [[,<strict> [,<timestamp> default to current time]])
Takes a time expression and calculate next valid time
Parameters:
Time expression: can either be a set of fields with flags or just an epoch timestamp.
Strict intrepretation flag: Boolean default to false.
Timestamp: epoch timestamp to use instead of current time
Return an associative array:
time: Epoch timestamp in seconds since 1970-01-01 UTC with fractions of second as decimal part
error: <any error message>
time expression fields:
<Year> <Month> <day> <Hour> <Minute> <Second> <Millisesond> ...
Unspecified minor fields are assumed to have the lowest posible value
Note!: Time expression are in local time where as time stamps are in UTC
Note!: Month and weekday use another offset then the javascript Date function:
Month 1 is January
Week day 1-7 starting with Monday
Field syntax: [!][-]<value>[-<value>]|[,<value>]
or /<value>
or * (Any)
Day feild can also have the one of the following flags
y: year day
w: Week day 1-7 where 1 is monday
The epoch timestamp is seconds since 1970-01-01 UTC with fractions of second as decimal part
@<epoch>[.<milliseconds>]
" " : field separator
* : all values. Flags will be ignored.
! : not
/ : every (can not be combined with ! and range)
- : Negative values are counted back from the maximum value
a-b : range. both a and b included.
a,b : set of values
Special flags for month field:
w : week of the year 1 - 53
Special flags for day field:
y : day of year 1-366 (month field are ignored, but must be valid)
w : day of week 1-7
Examples:
Every hour: * * * *
every day at noon: * * * 12
Every 3th Hour on work days: * * w1-5 /3
At a specific epoch time: @1422821601.123
At a specific time: 2014 5 13 18 53 7 300 230
2th to last day of the month at noon: * * -2 12
3th last day of the year: * * y-3
3 times an hour during work time: * * w1-5 9-17 0,20,40
Every morning at 7:30 but not on a weekend: * * !6-7 7 30
every 10 minutes in the day time: * * * 8-18 /10
every 3th thuesday at 18, execpt in the summer: * * w2 18 & * 21-28 & * !6-8
\*============================================================================*/
chronos.nextTime = function(tex,strict,startfromTime){
var carry=false;
var sign=false;
var next=-1;
var date;
var valid,last,lastdom;
var time;
// Set next time to now
if(typeof startfromTime === "number")
var ct = new Date(startfromTime*1000);
else{
var ct = new Date();
}
// Add a short delay to avoid small timing errors to generate multiple hits
// And to allow for execution time
ct.setUTCMilliseconds(ct.getUTCMilliseconds() + chronos.timeResolution );
var nt=[
ct.getFullYear()
,ct.getMonth()+1
,ct.getDate()
,ct.getHours()
,ct.getMinutes()
,ct.getSeconds()
,ct.getMilliseconds()
,0,0,0,0,0
];
var maxCarry=nt.length-2;
// Phase 1: Split time expression into segments of numbers and flags/other
//------------------------------------------------------------------------*/
// Remove whitespaces
tex=tex.replace(/\s+/g,' ').trim();
// Split into elements og number and other
var f=tex.match(/(\d+|[^\d])/g);
// Phase 2: Reassamble fields into object
//------------------------------------------------------------------------*/
var field=[]; field[0]={};field[0].val=[];field[0].range=[];field[0].flags=new String();;
var fpc=0; // Field position counter
var tp=0;
var l=0; // measure length of string, to make better error messages
var sign=false;
var val;
for(var i=0;i<f.length;i++){
// Flags
if(fpc==0 && "@".indexOf(f[i])>=0){
field[0].flags=f[i];
}else if("!/".indexOf(f[i])>=0){
field[fpc].flags+=f[i];
// Special day flags
}else if(fpc==2 && "yw".indexOf(f[i])>=0){
// Add flag
field[fpc].flags+=f[i];
// Set special index
if(f[i]=='w') tp=10;
else tp=11;
// Value set
}else if(f[i]==',' && field[fpc].val.length>0 && field[fpc].range.length==0){
continue;
// Allow decimal point in epoch time
}else if(fpc==0 && field[0].flags=='@' && f[i]=='.'){
continue;
// Range
}else if(f[i]=='-' && /\d$/.test(f[i-1]) && field[fpc].val.length==1){
field[fpc].range[0]=field[fpc].val[0];
field[fpc].val=[];
// Signed value
}else if(f[i]=='-' && /\d$/.test(f[i+1])){
sign=true;
// Wildcard
}else if(f[i]=='*' && field[fpc].val.length==0){
field[fpc].val[0]='*';
field[fpc].flags=''; // Remove flags from wildcard
field[fpc].range=[]; // Drop range
// Values
}else if(/\d$/.test(f[i]) ){
if(field[fpc].range.length>0)
field[fpc].range[1]=(sign?'-':'')+f[i];
else
field[fpc].val[field[fpc].val.length]=(sign?'-':'') + f[i];
sign=false;
// Syntax errors
}else if(strict && f[i]!=' ')
return {"time":0,
"error":"Can't intrepret \""+f[i]+'" at '+l + "\n" + tex + "\n" + " ".repeat(l) + '^'
}
// Move field pointer to next position
if( i+1 <f.length
&& f[i+1]!=','
&& f[i+1]!='-'
&& f[i+1]!='.'
&& (field[fpc].val.length>0 || field[fpc].range.length>1)
) {
fpc++;
tp=fpc;
field[fpc]={};
field[fpc].val=[];
field[fpc].range=[];
field[fpc].flags=new String();
range=false;
}
// sum length for displaying error mesage
l+=f[i].length;
}
// Phase 3: validate and interpret fields
//------------------------------------------------------------------------*/
// Loop through fields and assign time
// When time for a field exceeds limits; skip back one field and preset lower values to 0
for(fpc=0; fpc<field.length; fpc++){
tp=fpc;
last=-1;
// Day has special flags and limits
// Set up values to use
if(fpc==2){
// Make date of year, month and day
date=new Date(nt[0],nt[1]-1,nt[2]);
if(field[fpc].flags.length>0){
// day of week
if(field[fpc].flags.indexOf('w')>=0){
tp=10;
last=7;
lastdom=new Date(date.getFullYear(),date.getMonth()+1,0).getDate();
// Get week day of current next time
//[10]=((date.getDay()+7)%8+1);
nt[10]=(((date.getDay()+6)%7)+1);
// Day of year
}else if(field[fpc].flags.indexOf('y')>=0){
tp=11;
last=((new Date(date.getFullYear()+1, 1, 1) - new Date(date.getFullYear(), 1, 1)) / 86400000);
// Get yearday of current next time
nt[tp]=Math.ceil((date - new Date(date.getFullYear(), 0, 0)) / 86400000);
if(nt[11]==0) nt[1]=1;
}
}
// day of month
if(last<0){
last=new Date(date.getFullYear(),date.getMonth()+1,0).getDate();
}
}
// Wildcard: skip all checks and go to next field
if(field[fpc].val[0]=='*'){
// If last field; Preset unused fields to low contraint.
if(fpc==field.length-1){
for(i=fpc+1;i<nt.length-2;i++)
if(nt[i]>limit[i][0]){
nt[i]=limit[i][0];
carry=true;
}
if(carry) nt[fpc]++;
break;
}else
continue;
}
if(last<0) last=limit[tp][1];
next=nt[tp];
// last field; Test if remaining part are above minimum
if(!carry && fpc==field.length-1)
for(i=fpc+1;i<maxCarry;i++)
if(nt[i]>limit[i][0]){
next++;
break;
}
// Test for cascading carry
if(next>last){
if(fpc==0)
return {"time":0,"error":"Out of range time expression: "+tex};
carry=true
}else{
// Test that values are within limits
// Recalculate negative numbers to be counted backwards
do{
// Loop through all values
for(var key in field[fpc]){
// Test only values of arrays
if(Object.prototype.toString.call(field[fpc][key]) != '[object Array]') continue;
if(field[fpc][key].length<1) continue;
for(var i=0;i<field[fpc][key].length;i++){
valid=false;
// Not a number?
if(isNaN(field[fpc][key][i])) break;
// Cahch epoch
if(fpc==0 && field[0].flags=='@' && !isNaN(field[0].val[0])){
valid=true;
break;
}
// Test i minus sign. (Might be -0)
sign=(field[fpc][key][i]<0
|| (typeof field[fpc][key][i]==='string' && field[fpc][key][i].indexOf('-')>=0));
// Make integer
field[fpc][key][i]=parseInt(field[fpc][key][i]);
// Negative values are counted backwards from "last" value
if(sign){
// Year: Set to maximum
if(fpc==0) field[fpc][key][i]=limit[tp][1];
// count back from last
else field[fpc][key][i]=last+(fpc>2?1:0)+field[fpc][key][i];
}
//test low limits
if( field[fpc][key][i] < limit[tp][0] ){
if(key!='val' && field[fpc].flags.indexOf('/')<0){
if(strict) break;
// Range: Set to minimum
if(key=='range') field[fpc][key][i]=limit[tp][0];
// Value set: delete invalid value
else if(field[fpc][key].length>1) delete field[fpc][key][i];
// value: Invalid result
else break;
}
}
// Test high limit
if( field[fpc][key][i] > limit[tp][1] ){
if(strict) break;
// Range: Set to maximum
if(key=='range') field[fpc][key][i]=limit[tp][1];
// Value set: delete invalid value
else if(field[fpc][key].length>1) delete field[fpc][key][i];
// value: invalid result
else break;
}
valid=true;
}
if(!valid) break;
}
}while(false);
// Error
if(!valid)
return {"time":0,
"error":'(2) Unable to interpret time expression field '+fieldName[fpc]+'. as: "'
+field[fpc][key][i]+'" ('+key+')'
}
// Phase 4: Find next time
//------------------------------------------------------------------------*/
// Epoch time stamp
if(fpc==0 && field[0].flags=='@' && !isNaN(field[0].val[0]))
// Make a float
return {"time":Number(field[0].val[0] + '.' + (isNaN(field[0].val[1])?"0":field[0].val[1]) ),
"error":''};
// Range
if(field[fpc].range.length==2){
// Convert reverse range to a forward rande with the not flag set
if(field[fpc].range[0] > field[fpc].range[1]){
if(fpc==0){
// reverse range
val=field[fpc].range[1];
field[fpc].range[1]=field[fpc].range[0];
field[fpc].range[0]=val;
}else{
// Toggle ! flag
if(field[fpc].flags.indexOf('!')>=0)
field[fpc].flags=field[fpc].flags.replace('!','');
else
field[fpc].flags+='!';
// reverse range
var val=field[fpc].range[1];
field[fpc].range[1]=field[fpc].range[0]-1;
field[fpc].range[0]=val+1;
}
}
// Check for hit
if(next>=field[fpc].range[0] && next<=field[fpc].range[1]){
// Flag !
if(field[fpc].flags.indexOf('!')>=0){
next=field[fpc].range[1]+1;
}
// Out of range; set next time (unless ! flag)
}else{
// If ! flag: its a hit
if(field[fpc].flags.indexOf('!')<0){
// over range
if(next>field[fpc].range[1]) carry=true;
next=field[fpc].range[0];
}
}
// Every value
}else if(field[fpc].flags.indexOf('/')>=0 && field[fpc].val.length==1){
// meningsless values treated as a hit
if(field[fpc].val[0]>1){
// Calculate next multiplum
val=next%field[fpc].val[0];
next+=(val?field[fpc].val[0]-val:0);
}
// Regular value and value set
}else if(field[fpc].val.length>=1){
// order values
if(field[fpc].val.length>1) field[fpc].val.sort();
for(i=0; i<field[fpc].val.length; i++){
// Break on hit
if(field[fpc].val[i]==next && field[fpc].flags.indexOf('!')<0) break;
// Over
if(field[fpc].val[i]>next){
// ! flag; loop until hole in values
if(field[fpc].flags.indexOf('!')>=0){
if( i>0 && field[fpc].val[i]-1==field[fpc].val[i]) continue;
}else{
next=field[fpc].val[i];
}
break;
}
}
// If no hit: set carry
if(i>=field[fpc].val.length){
// ! flag: increment last value
if(field[fpc].flags.indexOf('!')>=0){
next=field[fpc].val[field[fpc].val.length-1]+1;
if(next>last) carry=true;
} else
carry=true;
}
// Syntax errors
}else if(strict)
return {"time":0,
"error":'(3) Unable to interpret time expression field '+fieldName[fpc]+' as:'+field[fpc]
};
// if next has changed: clear smaller fields
if(!carry && next != nt[tp]){
// Convert week day to day of month
if(tp==10){
next=nt[2]+next-nt[10];
tp=2;
last=lastdom;
}
// Check if next is over limits
if(next<=last){
for(var ii=fpc+1; ii<maxCarry;ii++)
nt[ii]=limit[ii][0];
nt[tp]=next;
}else{
carry=true;
}
}
}
// Handle carry condition
if(carry){
// if carry on same field twice: throw error
if(fpc>=maxCarry)
return {"time":0,"error":'Carry overflow'};
var lastMaxCarry=maxCarry;
maxCarry=fpc;
// if Year day, skip month
if(tp==11)
fpc=0;
// If not weekday and every flag are not set, jump to more significant field
else if(tp!=10 && (field[fpc].flags.indexOf('/')<0 || next>last) && fpc!=0)
fpc--;
else
// if just incrementing same field, it dosent count as a real carry
maxCarry=fpc+1;
// Preset rest of fields to minimum value
for(var i=fpc+1; i<lastMaxCarry;i++)
nt[i]=limit[i][0];
// add carry
nt[fpc]++;
// Run loop again, but compensate for loop increment
fpc--;
carry=false;
}
}
// Convert next date to an epoch timestamp
// Date function parameters are: year, month, day, hours, minutes, seconds, milliseconds
// NB: months start with 0 for january!
if(!nt[11])
time=(new Date(nt[0],nt[1]-1,nt[2],nt[3],nt[4],nt[5],nt[6])).getTime();
// for year daye: use month 0 and set day to year day
else
time=(new Date(nt[0],0,nt[11],nt[3],nt[4],nt[5],nt[6])).getTime();
// Return epoch time, with milliseconds as the decimal values
return {"time":Math.floor(time/1000)+"."+("00"+time%1000).slice(-3),"error":''};
}