forked from kbwood/calendars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.calendars.all.js
3168 lines (3018 loc) · 126 KB
/
jquery.calendars.all.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
/* http://keith-wood.name/calendars.html
Calendars for jQuery v2.0.2.
Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009.
Available under the MIT (http://keith-wood.name/licence.html) license.
Please attribute the author if you use it. */
(function($) { // Hide scope, no $ conflict
function Calendars() {
this.regionalOptions = [];
this.regionalOptions[''] = {
invalidCalendar: 'Calendar {0} not found',
invalidDate: 'Invalid {0} date',
invalidMonth: 'Invalid {0} month',
invalidYear: 'Invalid {0} year',
differentCalendars: 'Cannot mix {0} and {1} dates'
};
this.local = this.regionalOptions[''];
this.calendars = {};
this._localCals = {};
}
/** Create the calendars plugin.
<p>Provides support for various world calendars in a consistent manner.</p>
@class Calendars
@example $.calendars.instance('julian').newDate(2014, 12, 25) */
$.extend(Calendars.prototype, {
/** Obtain a calendar implementation and localisation.
@memberof Calendars
@param [name='gregorian'] {string} The name of the calendar, e.g. 'gregorian', 'persian', 'islamic'.
@param [language=''] {string} The language code to use for localisation (default is English).
@return {Calendar} The calendar and localisation.
@throws Error if calendar not found. */
instance: function(name, language) {
name = (name || 'gregorian').toLowerCase();
language = language || '';
var cal = this._localCals[name + '-' + language];
if (!cal && this.calendars[name]) {
cal = new this.calendars[name](language);
this._localCals[name + '-' + language] = cal;
}
if (!cal) {
throw (this.local.invalidCalendar || this.regionalOptions[''].invalidCalendar).
replace(/\{0\}/, name);
}
return cal;
},
/** Create a new date - for today if no other parameters given.
@memberof Calendars
@param year {CDate|number} The date to copy or the year for the date.
@param [month] {number} The month for the date.
@param [day] {number} The day for the date.
@param [calendar='gregorian'] {BaseCalendar|string} The underlying calendar or the name of the calendar.
@param [language=''] {string} The language to use for localisation (default English).
@return {CDate} The new date.
@throws Error if an invalid date. */
newDate: function(year, month, day, calendar, language) {
calendar = (year != null && year.year ? year.calendar() : (typeof calendar === 'string' ?
this.instance(calendar, language) : calendar)) || this.instance();
return calendar.newDate(year, month, day);
},
/** A simple digit substitution function for localising numbers via the Calendar digits option.
@member Calendars
@param digits {string[]} The substitute digits, for 0 through 9.
@return {function} The substitution function. */
substituteDigits: function(digits) {
return function(value) {
return (value + '').replace(/[0-9]/g, function(digit) {
return digits[digit];
});
}
},
/** Digit substitution function for localising Chinese style numbers via the Calendar digits option.
@member Calendars
@param digits {string[]} The substitute digits, for 0 through 9.
@param powers {string[]} The characters denoting powers of 10, i.e. 1, 10, 100, 1000.
@return {function} The substitution function. */
substituteChineseDigits: function(digits, powers) {
return function(value) {
var localNumber = '';
var power = 0;
while (value > 0) {
var units = value % 10;
localNumber = (units === 0 ? '' : digits[units] + powers[power]) + localNumber;
power++;
value = Math.floor(value / 10);
}
if (localNumber.indexOf(digits[1] + powers[1]) === 0) {
localNumber = localNumber.substr(1);
}
return localNumber || digits[0];
}
}
});
/** Generic date, based on a particular calendar.
@class CDate
@param calendar {BaseCalendar} The underlying calendar implementation.
@param year {number} The year for this date.
@param month {number} The month for this date.
@param day {number} The day for this date.
@return {CDate} The date object.
@throws Error if an invalid date. */
function CDate(calendar, year, month, day) {
this._calendar = calendar;
this._year = year;
this._month = month;
this._day = day;
if (this._calendar._validateLevel === 0 &&
!this._calendar.isValid(this._year, this._month, this._day)) {
throw ($.calendars.local.invalidDate || $.calendars.regionalOptions[''].invalidDate).
replace(/\{0\}/, this._calendar.local.name);
}
}
/** Pad a numeric value with leading zeroes.
@private
@param value {number} The number to format.
@param length {number} The minimum length.
@return {string} The formatted number. */
function pad(value, length) {
value = '' + value;
return '000000'.substring(0, length - value.length) + value;
}
$.extend(CDate.prototype, {
/** Create a new date.
@memberof CDate
@param [year] {CDate|number} The date to copy or the year for the date (default this date).
@param [month] {number} The month for the date.
@param [day] {number} The day for the date.
@return {CDate} The new date.
@throws Error if an invalid date. */
newDate: function(year, month, day) {
return this._calendar.newDate((year == null ? this : year), month, day);
},
/** Set or retrieve the year for this date.
@memberof CDate
@param [year] {number} The year for the date.
@return {number|CDate} The date's year (if no parameter) or the updated date.
@throws Error if an invalid date. */
year: function(year) {
return (arguments.length === 0 ? this._year : this.set(year, 'y'));
},
/** Set or retrieve the month for this date.
@memberof CDate
@param [month] {number} The month for the date.
@return {number|CDate} The date's month (if no parameter) or the updated date.
@throws Error if an invalid date. */
month: function(month) {
return (arguments.length === 0 ? this._month : this.set(month, 'm'));
},
/** Set or retrieve the day for this date.
@memberof CDate
@param [day] {number} The day for the date.
@return {number|CData} The date's day (if no parameter) or the updated date.
@throws Error if an invalid date. */
day: function(day) {
return (arguments.length === 0 ? this._day : this.set(day, 'd'));
},
/** Set new values for this date.
@memberof CDate
@param year {number} The year for the date.
@param month {number} The month for the date.
@param day {number} The day for the date.
@return {CDate} The updated date.
@throws Error if an invalid date. */
date: function(year, month, day) {
if (!this._calendar.isValid(year, month, day)) {
throw ($.calendars.local.invalidDate || $.calendars.regionalOptions[''].invalidDate).
replace(/\{0\}/, this._calendar.local.name);
}
this._year = year;
this._month = month;
this._day = day;
return this;
},
/** Determine whether this date is in a leap year.
@memberof CDate
@return {boolean} <code>true</code> if this is a leap year, <code>false</code> if not. */
leapYear: function() {
return this._calendar.leapYear(this);
},
/** Retrieve the epoch designator for this date, e.g. BCE or CE.
@memberof CDate
@return {string} The current epoch. */
epoch: function() {
return this._calendar.epoch(this);
},
/** Format the year, if not a simple sequential number.
@memberof CDate
@return {string} The formatted year. */
formatYear: function() {
return this._calendar.formatYear(this);
},
/** Retrieve the month of the year for this date,
i.e. the month's position within a numbered year.
@memberof CDate
@return {number} The month of the year: <code>minMonth</code> to months per year. */
monthOfYear: function() {
return this._calendar.monthOfYear(this);
},
/** Retrieve the week of the year for this date.
@memberof CDate
@return {number} The week of the year: 1 to weeks per year. */
weekOfYear: function() {
return this._calendar.weekOfYear(this);
},
/** Retrieve the number of days in the year for this date.
@memberof CDate
@return {number} The number of days in this year. */
daysInYear: function() {
return this._calendar.daysInYear(this);
},
/** Retrieve the day of the year for this date.
@memberof CDate
@return {number} The day of the year: 1 to days per year. */
dayOfYear: function() {
return this._calendar.dayOfYear(this);
},
/** Retrieve the number of days in the month for this date.
@memberof CDate
@return {number} The number of days. */
daysInMonth: function() {
return this._calendar.daysInMonth(this);
},
/** Retrieve the day of the week for this date.
@memberof CDate
@return {number} The day of the week: 0 to number of days - 1. */
dayOfWeek: function() {
return this._calendar.dayOfWeek(this);
},
/** Determine whether this date is a week day.
@memberof CDate
@return {boolean} <code>true</code> if a week day, <code>false</code> if not. */
weekDay: function() {
return this._calendar.weekDay(this);
},
/** Retrieve additional information about this date.
@memberof CDate
@return {object} Additional information - contents depends on calendar. */
extraInfo: function() {
return this._calendar.extraInfo(this);
},
/** Add period(s) to a date.
@memberof CDate
@param offset {number} The number of periods to adjust by.
@param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day.
@return {CDate} The updated date. */
add: function(offset, period) {
return this._calendar.add(this, offset, period);
},
/** Set a portion of the date.
@memberof CDate
@param value {number} The new value for the period.
@param period {string} One of 'y' for year, 'm' for month, 'd' for day.
@return {CDate} The updated date.
@throws Error if not a valid date. */
set: function(value, period) {
return this._calendar.set(this, value, period);
},
/** Compare this date to another date.
@memberof CDate
@param date {CDate} The other date.
@return {number} -1 if this date is before the other date,
0 if they are equal, or +1 if this date is after the other date. */
compareTo: function(date) {
if (this._calendar.name !== date._calendar.name) {
throw ($.calendars.local.differentCalendars || $.calendars.regionalOptions[''].differentCalendars).
replace(/\{0\}/, this._calendar.local.name).replace(/\{1\}/, date._calendar.local.name);
}
var c = (this._year !== date._year ? this._year - date._year :
this._month !== date._month ? this.monthOfYear() - date.monthOfYear() :
this._day - date._day);
return (c === 0 ? 0 : (c < 0 ? -1 : +1));
},
/** Retrieve the calendar backing this date.
@memberof CDate
@return {BaseCalendar} The calendar implementation. */
calendar: function() {
return this._calendar;
},
/** Retrieve the Julian date equivalent for this date,
i.e. days since January 1, 4713 BCE Greenwich noon.
@memberof CDate
@return {number} The equivalent Julian date. */
toJD: function() {
return this._calendar.toJD(this);
},
/** Create a new date from a Julian date.
@memberof CDate
@param jd {number} The Julian date to convert.
@return {CDate} The equivalent date. */
fromJD: function(jd) {
return this._calendar.fromJD(jd);
},
/** Convert this date to a standard (Gregorian) JavaScript Date.
@memberof CDate
@return {Date} The equivalent JavaScript date. */
toJSDate: function() {
return this._calendar.toJSDate(this);
},
/** Create a new date from a standard (Gregorian) JavaScript Date.
@memberof CDate
@param jsd {Date} The JavaScript date to convert.
@return {CDate} The equivalent date. */
fromJSDate: function(jsd) {
return this._calendar.fromJSDate(jsd);
},
/** Convert to a string for display.
@memberof CDate
@return {string} This date as a string. */
toString: function() {
return (this.year() < 0 ? '-' : '') + pad(Math.abs(this.year()), 4) +
'-' + pad(this.month(), 2) + '-' + pad(this.day(), 2);
}
});
/** Basic functionality for all calendars.
Other calendars should extend this:
<pre>OtherCalendar.prototype = new BaseCalendar;</pre>
@class BaseCalendar */
function BaseCalendar() {
this.shortYearCutoff = '+10';
}
$.extend(BaseCalendar.prototype, {
_validateLevel: 0, // "Stack" to turn validation on/off
/** Create a new date within this calendar - today if no parameters given.
@memberof BaseCalendar
@param year {CDate|number} The date to duplicate or the year for the date.
@param [month] {number} The month for the date.
@param [day] {number} The day for the date.
@return {CDate} The new date.
@throws Error if not a valid date or a different calendar used. */
newDate: function(year, month, day) {
if (year == null) {
return this.today();
}
if (year.year) {
this._validate(year, month, day,
$.calendars.local.invalidDate || $.calendars.regionalOptions[''].invalidDate);
day = year.day();
month = year.month();
year = year.year();
}
return new CDate(this, year, month, day);
},
/** Create a new date for today.
@memberof BaseCalendar
@return {CDate} Today's date. */
today: function() {
return this.fromJSDate(new Date());
},
/** Retrieve the epoch designator for this date.
@memberof BaseCalendar
@param year {CDate|number} The date to examine or the year to examine.
@return {string} The current epoch.
@throws Error if an invalid year or a different calendar used. */
epoch: function(year) {
var date = this._validate(year, this.minMonth, this.minDay,
$.calendars.local.invalidYear || $.calendars.regionalOptions[''].invalidYear);
return (date.year() < 0 ? this.local.epochs[0] : this.local.epochs[1]);
},
/** Format the year, if not a simple sequential number
@memberof BaseCalendar
@param year {CDate|number} The date to format or the year to format.
@return {string} The formatted year.
@throws Error if an invalid year or a different calendar used. */
formatYear: function(year) {
var date = this._validate(year, this.minMonth, this.minDay,
$.calendars.local.invalidYear || $.calendars.regionalOptions[''].invalidYear);
return (date.year() < 0 ? '-' : '') + pad(Math.abs(date.year()), 4)
},
/** Retrieve the number of months in a year.
@memberof BaseCalendar
@param year {CDate|number} The date to examine or the year to examine.
@return {number} The number of months.
@throws Error if an invalid year or a different calendar used. */
monthsInYear: function(year) {
this._validate(year, this.minMonth, this.minDay,
$.calendars.local.invalidYear || $.calendars.regionalOptions[''].invalidYear);
return 12;
},
/** Calculate the month's ordinal position within the year -
for those calendars that don't start at month 1!
@memberof BaseCalendar
@param year {CDate|number} The date to examine or the year to examine.
@param month {number} The month to examine.
@return {number} The ordinal position, starting from <code>minMonth</code>.
@throws Error if an invalid year/month or a different calendar used. */
monthOfYear: function(year, month) {
var date = this._validate(year, month, this.minDay,
$.calendars.local.invalidMonth || $.calendars.regionalOptions[''].invalidMonth);
return (date.month() + this.monthsInYear(date) - this.firstMonth) %
this.monthsInYear(date) + this.minMonth;
},
/** Calculate actual month from ordinal position, starting from minMonth.
@memberof BaseCalendar
@param year {number} The year to examine.
@param ord {number} The month's ordinal position.
@return {number} The month's number.
@throws Error if an invalid year/month. */
fromMonthOfYear: function(year, ord) {
var m = (ord + this.firstMonth - 2 * this.minMonth) %
this.monthsInYear(year) + this.minMonth;
this._validate(year, m, this.minDay,
$.calendars.local.invalidMonth || $.calendars.regionalOptions[''].invalidMonth);
return m;
},
/** Retrieve the number of days in a year.
@memberof BaseCalendar
@param year {CDate|number} The date to examine or the year to examine.
@return {number} The number of days.
@throws Error if an invalid year or a different calendar used. */
daysInYear: function(year) {
var date = this._validate(year, this.minMonth, this.minDay,
$.calendars.local.invalidYear || $.calendars.regionalOptions[''].invalidYear);
return (this.leapYear(date) ? 366 : 365);
},
/** Retrieve the day of the year for a date.
@memberof BaseCalendar
@param year {CDate|number} The date to convert or the year to convert.
@param [month] {number} The month to convert.
@param [day] {number} The day to convert.
@return {number} The day of the year.
@throws Error if an invalid date or a different calendar used. */
dayOfYear: function(year, month, day) {
var date = this._validate(year, month, day,
$.calendars.local.invalidDate || $.calendars.regionalOptions[''].invalidDate);
return date.toJD() - this.newDate(date.year(),
this.fromMonthOfYear(date.year(), this.minMonth), this.minDay).toJD() + 1;
},
/** Retrieve the number of days in a week.
@memberof BaseCalendar
@return {number} The number of days. */
daysInWeek: function() {
return 7;
},
/** Retrieve the day of the week for a date.
@memberof BaseCalendar
@param year {CDate|number} The date to examine or the year to examine.
@param [month] {number} The month to examine.
@param [day] {number} The day to examine.
@return {number} The day of the week: 0 to number of days - 1.
@throws Error if an invalid date or a different calendar used. */
dayOfWeek: function(year, month, day) {
var date = this._validate(year, month, day,
$.calendars.local.invalidDate || $.calendars.regionalOptions[''].invalidDate);
return (Math.floor(this.toJD(date)) + 2) % this.daysInWeek();
},
/** Retrieve additional information about a date.
@memberof BaseCalendar
@param year {CDate|number} The date to examine or the year to examine.
@param [month] {number} The month to examine.
@param [day] {number} The day to examine.
@return {object} Additional information - contents depends on calendar.
@throws Error if an invalid date or a different calendar used. */
extraInfo: function(year, month, day) {
this._validate(year, month, day,
$.calendars.local.invalidDate || $.calendars.regionalOptions[''].invalidDate);
return {};
},
/** Add period(s) to a date.
Cater for no year zero.
@memberof BaseCalendar
@param date {CDate} The starting date.
@param offset {number} The number of periods to adjust by.
@param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day.
@return {CDate} The updated date.
@throws Error if a different calendar used. */
add: function(date, offset, period) {
this._validate(date, this.minMonth, this.minDay,
$.calendars.local.invalidDate || $.calendars.regionalOptions[''].invalidDate);
return this._correctAdd(date, this._add(date, offset, period), offset, period);
},
/** Add period(s) to a date.
@memberof BaseCalendar
@private
@param date {CDate} The starting date.
@param offset {number} The number of periods to adjust by.
@param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day.
@return {CDate} The updated date. */
_add: function(date, offset, period) {
this._validateLevel++;
if (period === 'd' || period === 'w') {
var jd = date.toJD() + offset * (period === 'w' ? this.daysInWeek() : 1);
var d = date.calendar().fromJD(jd);
this._validateLevel--;
return [d.year(), d.month(), d.day()];
}
try {
var y = date.year() + (period === 'y' ? offset : 0);
var m = date.monthOfYear() + (period === 'm' ? offset : 0);
var d = date.day();// + (period === 'd' ? offset : 0) +
//(period === 'w' ? offset * this.daysInWeek() : 0);
var resyncYearMonth = function(calendar) {
while (m < calendar.minMonth) {
y--;
m += calendar.monthsInYear(y);
}
var yearMonths = calendar.monthsInYear(y);
while (m > yearMonths - 1 + calendar.minMonth) {
y++;
m -= yearMonths;
yearMonths = calendar.monthsInYear(y);
}
};
if (period === 'y') {
if (date.month() !== this.fromMonthOfYear(y, m)) { // Hebrew
m = this.newDate(y, date.month(), this.minDay).monthOfYear();
}
m = Math.min(m, this.monthsInYear(y));
d = Math.min(d, this.daysInMonth(y, this.fromMonthOfYear(y, m)));
}
else if (period === 'm') {
resyncYearMonth(this);
d = Math.min(d, this.daysInMonth(y, this.fromMonthOfYear(y, m)));
}
var ymd = [y, this.fromMonthOfYear(y, m), d];
this._validateLevel--;
return ymd;
}
catch (e) {
this._validateLevel--;
throw e;
}
},
/** Correct a candidate date after adding period(s) to a date.
Handle no year zero if necessary.
@memberof BaseCalendar
@private
@param date {CDate} The starting date.
@param ymd {number[]} The added date.
@param offset {number} The number of periods to adjust by.
@param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day.
@return {CDate} The updated date. */
_correctAdd: function(date, ymd, offset, period) {
if (!this.hasYearZero && (period === 'y' || period === 'm')) {
if (ymd[0] === 0 || // In year zero
(date.year() > 0) !== (ymd[0] > 0)) { // Crossed year zero
var adj = {y: [1, 1, 'y'], m: [1, this.monthsInYear(-1), 'm'],
w: [this.daysInWeek(), this.daysInYear(-1), 'd'],
d: [1, this.daysInYear(-1), 'd']}[period];
var dir = (offset < 0 ? -1 : +1);
ymd = this._add(date, offset * adj[0] + dir * adj[1], adj[2]);
}
}
return date.date(ymd[0], ymd[1], ymd[2]);
},
/** Set a portion of the date.
@memberof BaseCalendar
@param date {CDate} The starting date.
@param value {number} The new value for the period.
@param period {string} One of 'y' for year, 'm' for month, 'd' for day.
@return {CDate} The updated date.
@throws Error if an invalid date or a different calendar used. */
set: function(date, value, period) {
this._validate(date, this.minMonth, this.minDay,
$.calendars.local.invalidDate || $.calendars.regionalOptions[''].invalidDate);
var y = (period === 'y' ? value : date.year());
var m = (period === 'm' ? value : date.month());
var d = (period === 'd' ? value : date.day());
if (period === 'y' || period === 'm') {
d = Math.min(d, this.daysInMonth(y, m));
}
return date.date(y, m, d);
},
/** Determine whether a date is valid for this calendar.
@memberof BaseCalendar
@param year {number} The year to examine.
@param month {number} The month to examine.
@param day {number} The day to examine.
@return {boolean} <code>true</code> if a valid date, <code>false</code> if not. */
isValid: function(year, month, day) {
this._validateLevel++;
var valid = (this.hasYearZero || year !== 0);
if (valid) {
var date = this.newDate(year, month, this.minDay);
valid = (month >= this.minMonth && month - this.minMonth < this.monthsInYear(date)) &&
(day >= this.minDay && day - this.minDay < this.daysInMonth(date));
}
this._validateLevel--;
return valid;
},
/** Convert the date to a standard (Gregorian) JavaScript Date.
@memberof BaseCalendar
@param year {CDate|number} The date to convert or the year to convert.
@param [month] {number} The month to convert.
@param [day] {number} The day to convert.
@return {Date} The equivalent JavaScript date.
@throws Error if an invalid date or a different calendar used. */
toJSDate: function(year, month, day) {
var date = this._validate(year, month, day,
$.calendars.local.invalidDate || $.calendars.regionalOptions[''].invalidDate);
return $.calendars.instance().fromJD(this.toJD(date)).toJSDate();
},
/** Convert the date from a standard (Gregorian) JavaScript Date.
@memberof BaseCalendar
@param jsd {Date} The JavaScript date.
@return {CDate} The equivalent calendar date. */
fromJSDate: function(jsd) {
return this.fromJD($.calendars.instance().fromJSDate(jsd).toJD());
},
/** Check that a candidate date is from the same calendar and is valid.
@memberof BaseCalendar
@private
@param year {CDate|number} The date to validate or the year to validate.
@param [month] {number} The month to validate.
@param [day] {number} The day to validate.
@param error {string} Rrror message if invalid.
@throws Error if different calendars used or invalid date. */
_validate: function(year, month, day, error) {
if (year.year) {
if (this._validateLevel === 0 && this.name !== year.calendar().name) {
throw ($.calendars.local.differentCalendars || $.calendars.regionalOptions[''].differentCalendars).
replace(/\{0\}/, this.local.name).replace(/\{1\}/, year.calendar().local.name);
}
return year;
}
try {
this._validateLevel++;
if (this._validateLevel === 1 && !this.isValid(year, month, day)) {
throw error.replace(/\{0\}/, this.local.name);
}
var date = this.newDate(year, month, day);
this._validateLevel--;
return date;
}
catch (e) {
this._validateLevel--;
throw e;
}
}
});
/** Implementation of the Proleptic Gregorian Calendar.
See <a href=":http://en.wikipedia.org/wiki/Gregorian_calendar">http://en.wikipedia.org/wiki/Gregorian_calendar</a>
and <a href="http://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar">http://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar</a>.
@class GregorianCalendar
@augments BaseCalendar
@param [language=''] {string} The language code (default English) for localisation. */
function GregorianCalendar(language) {
this.local = this.regionalOptions[language] || this.regionalOptions[''];
}
GregorianCalendar.prototype = new BaseCalendar;
$.extend(GregorianCalendar.prototype, {
/** The calendar name.
@memberof GregorianCalendar */
name: 'Gregorian',
/** Julian date of start of Gregorian epoch: 1 January 0001 CE.
@memberof GregorianCalendar */
jdEpoch: 1721425.5,
/** Days per month in a common year.
@memberof GregorianCalendar */
daysPerMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
/** <code>true</code> if has a year zero, <code>false</code> if not.
@memberof GregorianCalendar */
hasYearZero: false,
/** The minimum month number.
@memberof GregorianCalendar */
minMonth: 1,
/** The first month in the year.
@memberof GregorianCalendar */
firstMonth: 1,
/** The minimum day number.
@memberof GregorianCalendar */
minDay: 1,
/** Localisations for the plugin.
Entries are objects indexed by the language code ('' being the default US/English).
Each object has the following attributes.
@memberof GregorianCalendar
@property name {string} The calendar name.
@property epochs {string[]} The epoch names.
@property monthNames {string[]} The long names of the months of the year.
@property monthNamesShort {string[]} The short names of the months of the year.
@property dayNames {string[]} The long names of the days of the week.
@property dayNamesShort {string[]} The short names of the days of the week.
@property dayNamesMin {string[]} The minimal names of the days of the week.
@property dateFormat {string} The date format for this calendar.
See the options on <a href="BaseCalendar.html#formatDate"><code>formatDate</code></a> for details.
@property firstDay {number} The number of the first day of the week, starting at 0.
@property isRTL {number} <code>true</code> if this localisation reads right-to-left. */
regionalOptions: { // Localisations
'': {
name: 'Gregorian',
epochs: ['BCE', 'CE'],
monthNames: ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'],
monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
digits: null,
dateFormat: 'mm/dd/yyyy',
firstDay: 0,
isRTL: false
}
},
/** Determine whether this date is in a leap year.
@memberof GregorianCalendar
@param year {CDate|number} The date to examine or the year to examine.
@return {boolean} <code>true</code> if this is a leap year, <code>false</code> if not.
@throws Error if an invalid year or a different calendar used. */
leapYear: function(year) {
var date = this._validate(year, this.minMonth, this.minDay,
$.calendars.local.invalidYear || $.calendars.regionalOptions[''].invalidYear);
var year = date.year() + (date.year() < 0 ? 1 : 0); // No year zero
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
},
/** Determine the week of the year for a date - ISO 8601.
@memberof GregorianCalendar
@param year {CDate|number} The date to examine or the year to examine.
@param [month] {number} The month to examine.
@param [day] {number} The day to examine.
@return {number} The week of the year, starting from 1.
@throws Error if an invalid date or a different calendar used. */
weekOfYear: function(year, month, day) {
// Find Thursday of this week starting on Monday
var checkDate = this.newDate(year, month, day);
checkDate.add(4 - (checkDate.dayOfWeek() || 7), 'd');
return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1;
},
/** Retrieve the number of days in a month.
@memberof GregorianCalendar
@param year {CDate|number} The date to examine or the year of the month.
@param [month] {number} The month.
@return {number} The number of days in this month.
@throws Error if an invalid month/year or a different calendar used. */
daysInMonth: function(year, month) {
var date = this._validate(year, month, this.minDay,
$.calendars.local.invalidMonth || $.calendars.regionalOptions[''].invalidMonth);
return this.daysPerMonth[date.month() - 1] +
(date.month() === 2 && this.leapYear(date.year()) ? 1 : 0);
},
/** Determine whether this date is a week day.
@memberof GregorianCalendar
@param year {CDate|number} The date to examine or the year to examine.
@param [month] {number} The month to examine.
@param [day] {number} The day to examine.
@return {boolean} <code>true</code> if a week day, <code>false</code> if not.
@throws Error if an invalid date or a different calendar used. */
weekDay: function(year, month, day) {
return (this.dayOfWeek(year, month, day) || 7) < 6;
},
/** Retrieve the Julian date equivalent for this date,
i.e. days since January 1, 4713 BCE Greenwich noon.
@memberof GregorianCalendar
@param year {CDate|number} The date to convert or the year to convert.
@param [month] {number} The month to convert.
@param [day] {number} The day to convert.
@return {number} The equivalent Julian date.
@throws Error if an invalid date or a different calendar used. */
toJD: function(year, month, day) {
var date = this._validate(year, month, day,
$.calendars.local.invalidDate || $.calendars.regionalOptions[''].invalidDate);
year = date.year();
month = date.month();
day = date.day();
if (year < 0) { year++; } // No year zero
// Jean Meeus algorithm, "Astronomical Algorithms", 1991
if (month < 3) {
month += 12;
year--;
}
var a = Math.floor(year / 100);
var b = 2 - a + Math.floor(a / 4);
return Math.floor(365.25 * (year + 4716)) +
Math.floor(30.6001 * (month + 1)) + day + b - 1524.5;
},
/** Create a new date from a Julian date.
@memberof GregorianCalendar
@param jd {number} The Julian date to convert.
@return {CDate} The equivalent date. */
fromJD: function(jd) {
// Jean Meeus algorithm, "Astronomical Algorithms", 1991
var z = Math.floor(jd + 0.5);
var a = Math.floor((z - 1867216.25) / 36524.25);
a = z + 1 + a - Math.floor(a / 4);
var b = a + 1524;
var c = Math.floor((b - 122.1) / 365.25);
var d = Math.floor(365.25 * c);
var e = Math.floor((b - d) / 30.6001);
var day = b - d - Math.floor(e * 30.6001);
var month = e - (e > 13.5 ? 13 : 1);
var year = c - (month > 2.5 ? 4716 : 4715);
if (year <= 0) { year--; } // No year zero
return this.newDate(year, month, day);
},
/** Convert this date to a standard (Gregorian) JavaScript Date.
@memberof GregorianCalendar
@param year {CDate|number} The date to convert or the year to convert.
@param [month] {number} The month to convert.
@param [day] {number} The day to convert.
@return {Date} The equivalent JavaScript date.
@throws Error if an invalid date or a different calendar used. */
toJSDate: function(year, month, day) {
var date = this._validate(year, month, day,
$.calendars.local.invalidDate || $.calendars.regionalOptions[''].invalidDate);
var jsd = new Date(date.year(), date.month() - 1, date.day());
jsd.setHours(0);
jsd.setMinutes(0);
jsd.setSeconds(0);
jsd.setMilliseconds(0);
// Hours may be non-zero on daylight saving cut-over:
// > 12 when midnight changeover, but then cannot generate
// midnight datetime, so jump to 1AM, otherwise reset.
jsd.setHours(jsd.getHours() > 12 ? jsd.getHours() + 2 : 0);
return jsd;
},
/** Create a new date from a standard (Gregorian) JavaScript Date.
@memberof GregorianCalendar
@param jsd {Date} The JavaScript date to convert.
@return {CDate} The equivalent date. */
fromJSDate: function(jsd) {
return this.newDate(jsd.getFullYear(), jsd.getMonth() + 1, jsd.getDate());
}
});
// Singleton manager
$.calendars = new Calendars();
// Date template
$.calendars.cdate = CDate;
// Base calendar template
$.calendars.baseCalendar = BaseCalendar;
// Gregorian calendar implementation
$.calendars.calendars.gregorian = GregorianCalendar;
})(jQuery);
/* http://keith-wood.name/calendars.html
Calendars extras for jQuery v2.0.2.
Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009.
Available under the MIT (http://keith-wood.name/licence.html) license.
Please attribute the author if you use it. */
(function($) { // Hide scope, no $ conflict
$.extend($.calendars.regionalOptions[''], {
invalidArguments: 'Invalid arguments',
invalidFormat: 'Cannot format a date from another calendar',
missingNumberAt: 'Missing number at position {0}',
unknownNameAt: 'Unknown name at position {0}',
unexpectedLiteralAt: 'Unexpected literal at position {0}',
unexpectedText: 'Additional text found at end'
});
$.calendars.local = $.calendars.regionalOptions[''];
$.extend($.calendars.cdate.prototype, {
/** Format this date.
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof CDate
@param [format] {string} The date format to use (see <a href="BaseCalendar.html#formatDate"><code>formatDate</code></a>).
@param [settings] {object} Options for the <code>formatDate</code> function.
@return {string} The formatted date. */
formatDate: function(format, settings) {
if (typeof format !== 'string') {
settings = format;
format = '';
}
return this._calendar.formatDate(format || '', this, settings);
}
});
$.extend($.calendars.baseCalendar.prototype, {
UNIX_EPOCH: $.calendars.instance().newDate(1970, 1, 1).toJD(),
SECS_PER_DAY: 24 * 60 * 60,
TICKS_EPOCH: $.calendars.instance().jdEpoch, // 1 January 0001 CE
TICKS_PER_DAY: 24 * 60 * 60 * 10000000,
/** Date form for ATOM (RFC 3339/ISO 8601).
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
ATOM: 'yyyy-mm-dd',
/** Date form for cookies.
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
COOKIE: 'D, dd M yyyy',
/** Date form for full date.
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
FULL: 'DD, MM d, yyyy',
/** Date form for ISO 8601.
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
ISO_8601: 'yyyy-mm-dd',
/** Date form for Julian date.
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
JULIAN: 'J',
/** Date form for RFC 822.
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
RFC_822: 'D, d M yy',
/** Date form for RFC 850.
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
RFC_850: 'DD, dd-M-yy',
/** Date form for RFC 1036.
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
RFC_1036: 'D, d M yy',
/** Date form for RFC 1123.
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
RFC_1123: 'D, d M yyyy',
/** Date form for RFC 2822.
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
RFC_2822: 'D, d M yyyy',
/** Date form for RSS (RFC 822).
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
RSS: 'D, d M yy',
/** Date form for Windows ticks.
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
TICKS: '!',
/** Date form for Unix timestamp.
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
TIMESTAMP: '@',
/** Date form for W3c (ISO 8601).
Found in the <code>jquery.calendars.plus.js</code> module.
@memberof BaseCalendar */
W3C: 'yyyy-mm-dd',
/** Format a date object into a string value.
The format can be combinations of the following:
<ul>
<li>d - day of month (no leading zero)</li>
<li>dd - day of month (two digit)</li>
<li>o - day of year (no leading zeros)</li>
<li>oo - day of year (three digit)</li>
<li>D - day name short</li>
<li>DD - day name long</li>