-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathdate.js
More file actions
1234 lines (1089 loc) · 34.9 KB
/
Copy pathdate.js
File metadata and controls
1234 lines (1089 loc) · 34.9 KB
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
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2009 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
(function() {
var Utilities_Date = new function(){
this.isSQLDate = isSQLDate;
this.isSQLDateTime = isSQLDateTime;
this.sqlHasYear = sqlHasYear;
this.sqlHasMonth = sqlHasMonth;
this.sqlHasDay = sqlHasDay;
this.getUnixTimestamp = getUnixTimestamp;
this.toUnixTimestamp = toUnixTimestamp;
this.getFileDateString = getFileDateString;
this.getFileTimeString = getFileTimeString;
this.getLocaleDateOrder = getLocaleDateOrder;
var _localeDateOrder = null;
var _months;
var _monthsWithEnglish;
/**
* Initializes localized months for strToDate month parsing
* @param dateFormatsJSON {Object} the JSON from resource/dateFormats.json
*/
this.init = function (dateFormatsJSON) {
if ((Zotero.isFx || Zotero.isElectron) && !dateFormatsJSON) {
dateFormatsJSON = Zotero.File.getResource('resource://zotero/schema/dateFormats.json');
}
if (!dateFormatsJSON) {
throw new Error("Zotero.Date.init() must be called with dateFormats.json");
}
if (typeof dateFormatsJSON == 'string') {
dateFormatsJSON = JSON.parse(dateFormatsJSON);
}
var locale = Zotero.locale;
var english = locale.startsWith('en');
// If no exact match, try first two characters ('de')
if (!dateFormatsJSON[locale]) {
locale = locale.substr(0, 2);
}
// Try first two characters repeated ('de-DE')
if (!dateFormatsJSON[locale]) {
locale = locale + "-" + locale.toUpperCase();
}
// Look for another locale with same first two characters
if (!dateFormatsJSON[locale]) {
let sameLang = Object.keys(dateFormatsJSON).filter(l => l.startsWith(locale.substr(0, 2)));
if (sameLang.length) {
locale = sameLang[0];
}
}
// If all else fails, use English
if (!dateFormatsJSON[locale]) {
locale = 'en-US';
english = true;
}
_months = dateFormatsJSON[locale];
// Add English versions if not already added
if (english) {
_monthsWithEnglish = _months;
}
else {
_monthsWithEnglish = {};
for (let key in _months) {
_monthsWithEnglish[key] = _months[key].concat(dateFormatsJSON['en-US'][key]);
}
}
let months = _monthsWithEnglish.short.map(m => m.toLowerCase())
.concat(_monthsWithEnglish.long.map(m => m.toLowerCase()));
// TODO: Switch back to native RegExp in Fx102 when Unicode property escapes are supported
_monthRe = Zotero.Utilities.XRegExp("(.*)(?:^|[^\\p{L}])(" + months.join("|") + ")[^ ]*(?: (.*)$|$)", "iu");
};
/**
* @param {Boolean} [withEnglish = false] - Include English months
* @return {Object} - Object with 'short' and 'long' arrays
*/
this.getMonths = function (withEnglish) {
if (withEnglish) {
if (_monthsWithEnglish) return _monthsWithEnglish;
}
else {
if (_months) return _months;
}
throw new Error("Zotero.Date: Zotero.Date.init() not called with resource/dateFormats.json");
}
/**
* Convert an SQL date in the form '2006-06-13 11:03:05' into a JS Date object
*
* Can also accept just the date part (e.g. '2006-06-13')
**/
this.sqlToDate = function (sqldate, isUTC) {
try {
if (!this.isSQLDate(sqldate)
&& !this.isSQLDateTime(sqldate)
&& !this.isSQLDateTimeWithoutSeconds(sqldate)) {
throw new Error("Invalid date");
}
var datetime = sqldate.split(' ');
var dateparts = datetime[0].split('-');
if (datetime[1]){
var timeparts = datetime[1].split(':');
}
else {
timeparts = [false, false, false];
}
// Invalid date part
if (dateparts.length==1){
throw new Error("Invalid date part");
}
// Allow missing seconds
if (timeparts.length == 2) {
timeparts[2] = '00';
}
if (isUTC){
return new Date(Date.UTC(dateparts[0], dateparts[1]-1, dateparts[2],
timeparts[0], timeparts[1], timeparts[2]));
}
return new Date(dateparts[0], dateparts[1]-1, dateparts[2],
timeparts[0], timeparts[1], timeparts[2]);
}
catch (e){
Zotero.debug(sqldate + ' is not a valid SQL date', 2)
return false;
}
}
/**
* Convert a JS Date object to an SQL date in the form '2006-06-13 11:03:05'
*
* If _toUTC_ is true, creates a UTC date
**/
this.dateToSQL = function (date, toUTC) {
try {
if (toUTC){
var year = date.getUTCFullYear();
var month = date.getUTCMonth();
var day = date.getUTCDate();
var hours = date.getUTCHours();
var minutes = date.getUTCMinutes();
var seconds = date.getUTCSeconds();
}
else {
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
}
year = Zotero.Utilities.lpad(year, '0', 4);
month = Zotero.Utilities.lpad(month + 1, '0', 2);
day = Zotero.Utilities.lpad(day, '0', 2);
hours = Zotero.Utilities.lpad(hours, '0', 2);
minutes = Zotero.Utilities.lpad(minutes, '0', 2);
seconds = Zotero.Utilities.lpad(seconds, '0', 2);
return year + '-' + month + '-' + day + ' '
+ hours + ':' + minutes + ':' + seconds;
}
catch (e){
Zotero.debug(date + ' is not a valid JS date', 2);
return '';
}
}
/**
* Convert a JS Date object to an ISO 8601 UTC date/time
*
* @param {Date} date JS Date object
* @return {String} ISO 8601 UTC date/time
* e.g. 2008-08-15T20:00:00Z
*/
this.dateToISO = function (date) {
var year = date.getUTCFullYear();
var month = date.getUTCMonth();
var day = date.getUTCDate();
var hours = date.getUTCHours();
var minutes = date.getUTCMinutes();
var seconds = date.getUTCSeconds();
year = Zotero.Utilities.lpad(year, '0', 4);
month = Zotero.Utilities.lpad(month + 1, '0', 2);
day = Zotero.Utilities.lpad(day, '0', 2);
hours = Zotero.Utilities.lpad(hours, '0', 2);
minutes = Zotero.Utilities.lpad(minutes, '0', 2);
seconds = Zotero.Utilities.lpad(seconds, '0', 2);
return year + '-' + month + '-' + day + 'T'
+ hours + ':' + minutes + ':' + seconds + 'Z';
}
var _re8601 = /^([0-9]{4})(-([0-9]{2})(-([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?$/;
/**
* @return {Boolean} - True if string is an ISO 8601 date, false if not
*/
this.isISODate = function (str) {
return _re8601.test(str);
}
/**
* Convert an ISO 8601–formatted date/time to a JS Date
*
* @param {String} isoDate ISO 8601 date
* @return {Date|False} - JS Date, or false if not a valid date
*/
this.isoToDate = function (isoDate) {
var d = isoDate.match(_re8601);
if (!d) return false;
return new Date(isoDate);
}
this.isoToSQL = function (isoDate) {
return Utilities_Date.dateToSQL(Utilities_Date.isoToDate(isoDate), true); // no 'this' for translator sandbox
}
/*
* converts a string to an object containing:
* day: integer form of the day
* month: integer form of the month (indexed from 0, not 1)
* year: 4 digit year (or, year + BC/AD/etc.)
* part: anything that does not fall under any of the above categories
* (e.g., "Summer," etc.)
*
* Note: the returned object is *not* a JS Date object
*/
var _slashRe = /^(.*?)\b([0-9]{1,4})(?:([\-\/\.\u5e74])([0-9]{1,2}))?(?:([\-\/\.\u6708])([0-9]{1,4}))?((?:\b|[^0-9]).*?)$/
var _yearRe = /^(.*?)\b((?:circa |around |about |c\.? ?)?[0-9]{1,4}(?: ?B\.? ?C\.?(?: ?E\.?)?| ?C\.? ?E\.?| ?A\.? ?D\.?)|[0-9]{3,4})\b(.*?)$/i;
var _monthRe = null;
var _dayRe = null;
this.strToDate = function (string) {
var date = {
order: ''
};
if (typeof string == 'string' || typeof string == 'number') {
string = Zotero.Utilities.trimInternal(string.toString());
}
// skip empty things
if(!string) {
return date;
}
var parts = [];
// first, directly inspect the string
var m = _slashRe.exec(string);
if(m &&
((!m[5] || !m[3]) || m[3] == m[5] || (m[3] == "\u5e74" && m[5] == "\u6708")) && // require sane separators
((m[2] && m[4] && m[6]) || (!m[1] && !m[7]))) { // require that either all parts are found,
// or else this is the entire date field
// figure out date based on parts
if(m[2].length == 3 || m[2].length == 4 || m[3] == "\u5e74") {
// ISO 8601 style date (big endian)
date.year = m[2];
date.month = m[4];
date.day = m[6];
date.order += m[2] ? 'y' : '';
date.order += m[4] ? 'm' : '';
date.order += m[6] ? 'd' : '';
} else if(m[2] && !m[4] && m[6]) {
date.month = m[2];
date.year = m[6];
date.order += m[2] ? 'm' : '';
date.order += m[6] ? 'y' : '';
} else {
// local style date (middle or little endian)
var country = Zotero.locale ? Zotero.locale.substr(3) : "US";
if(country == "US" || // The United States
country == "FM" || // The Federated States of Micronesia
country == "PW" || // Palau
country == "PH") { // The Philippines
date.month = m[2];
date.day = m[4];
date.order += m[2] ? 'm' : '';
date.order += m[4] ? 'd' : '';
} else {
date.month = m[4];
date.day = m[2];
date.order += m[2] ? 'd' : '';
date.order += m[4] ? 'm' : '';
}
date.year = m[6];
if (m[6] !== undefined) {
date.order += 'y';
}
}
var longYear = date.year && date.year.toString().length > 2;
if(date.year) date.year = parseInt(date.year, 10);
if(date.day) date.day = parseInt(date.day, 10);
if(date.month) {
date.month = parseInt(date.month, 10);
if(date.month > 12) {
// swap day and month
var tmp = date.day;
date.day = date.month
date.month = tmp;
date.order = date.order.replace('m', 'D')
.replace('d', 'M')
.replace('D', 'd')
.replace('M', 'm');
}
}
if((!date.month || date.month <= 12) && (!date.day || date.day <= 31)) {
// Parse pre-100 years with leading zeroes (001, 0001, 012, 0012, 0123, but not 08)
if (date.year && date.year < 100 && !longYear) {
var today = new Date();
var year = today.getFullYear();
var twoDigitYear = year % 100;
var century = year - twoDigitYear;
if(date.year <= twoDigitYear) {
// assume this date is from our century
date.year = century + date.year;
} else {
// assume this date is from the previous century
date.year = century - 100 + date.year;
}
}
if(date.month) date.month--; // subtract one for JS style
else delete date.month;
//Zotero.debug("DATE: retrieved with algorithms: "+JSON.stringify(date));
parts.push(
{ part: m[1], before: true },
{ part: m[7] }
);
} else {
// give up; we failed the sanity check
Zotero.debug("DATE: algorithms failed sanity check");
var date = {
order: ''
};
parts.push({ part: string });
}
} else {
//Zotero.debug("DATE: could not apply algorithms");
parts.push({ part: string });
}
// couldn't find something with the algorithms; use regexp
// YEAR
if(!date.year) {
for (var i in parts) {
var m = _yearRe.exec(parts[i].part);
if (m) {
date.year = m[2];
date.order = _insertDateOrderPart(date.order, 'y', parts[i]);
parts.splice(
i, 1,
{ part: m[1], before: true },
{ part: m[3] }
);
//Zotero.debug("DATE: got year (" + date.year + ", " + JSON.stringify(parts) + ")");
break;
}
}
}
// MONTH
if(date.month === undefined) {
// compile month regular expression
let months = Utilities_Date.getMonths(true); // no 'this' for translator sandbox
months = months.short.map(m => m.toLowerCase())
.concat(months.long.map(m => m.toLowerCase()));
for (var i in parts) {
var m = _monthRe.exec(parts[i].part);
if (m) {
// Modulo 12 in case we have multiple languages
date.month = months.indexOf(m[2].toLowerCase()) % 12;
date.order = _insertDateOrderPart(date.order, 'm', parts[i]);
parts.splice(
i, 1,
{ part: m[1], before: "m" },
{ part: m[3], after: "m" }
);
//Zotero.debug("DATE: got month (" + date.month + ", " + JSON.stringify(parts) + ")");
break;
}
}
}
// DAY
if(!date.day) {
// compile day regular expression
if(!_dayRe) {
var daySuffixes = Zotero.isClient ? Zotero.getString("date.daySuffixes").replace(/, ?/g, "|") : "";
_dayRe = new RegExp("\\b([0-9]{1,2})(?:"+daySuffixes+")?\\b(.*)", "i");
}
for (var i in parts) {
var m = _dayRe.exec(parts[i].part);
if (m) {
var day = parseInt(m[1], 10);
// Sanity check
if (day <= 31) {
date.day = day;
date.order = _insertDateOrderPart(date.order, 'd', parts[i]);
if(m.index > 0) {
var part = parts[i].part.substr(0, m.index);
if(m[2]) {
part += " " + m[2];;
}
} else {
var part = m[2];
}
parts.splice(
i, 1,
{ part: part }
);
//Zotero.debug("DATE: got day (" + date.day + ", " + JSON.stringify(parts) + ")");
break;
}
}
}
}
// Concatenate date parts
date.part = '';
for (var i in parts) {
date.part += parts[i].part + ' ';
}
// clean up date part
if(date.part) {
date.part = date.part.replace(/^[^A-Za-z0-9]+|[^A-Za-z0-9]+$/g, "");
}
if(date.part === "" || date.part == undefined) {
delete date.part;
}
//make sure year is always a string
if(date.year || date.year === 0) date.year += '';
return date;
}
this.isHTTPDate = function(str) {
var dayNames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"];
str = str.trim();
var temp = str.split(',');
if (temp.length > 1) {
var dayOfWeek = temp[0];
if(dayNames.indexOf(dayOfWeek) == -1) {
return false;
}
str = temp[1].trim();
}
temp = str.split(' ');
temp = temp.filter((t) => ! t.match(/^\s*$/));
if (temp.length < 5) {
return false;
}
if (!temp[0].trim().match(/[0-3]\d/)) {
return false;
}
if (monthNames.indexOf(temp[1].trim()) == -1) {
return false;
}
if (!temp[2].trim().match(/\d\d\d\d/)) {
return false;
}
temp.splice(0, 3);
var time = temp[0].trim().split(':');
if (time.length < 2) {
return false;
}
for (let t of time) {
if (!t.match(/\d\d/)) {
return false;
}
}
temp.splice(0, 1);
var zone = temp.join(' ').trim();
return !!zone.match(/([+-]\d\d\d\d|UTC?|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT)/)
};
function _insertDateOrderPart(dateOrder, part, partOrder) {
if (!dateOrder) {
return part;
}
if (partOrder.before === true) {
return part + dateOrder;
}
if (partOrder.after === true) {
return dateOrder + part;
}
if (partOrder.before) {
var pos = dateOrder.indexOf(partOrder.before);
if (pos == -1) {
return dateOrder;
}
return dateOrder.replace(new RegExp("(" + partOrder.before + ")"), part + '$1');
}
if (partOrder.after) {
var pos = dateOrder.indexOf(partOrder.after);
if (pos == -1) {
return dateOrder + part;
}
return dateOrder.replace(new RegExp("(" + partOrder.after + ")"), '$1' + part);
}
return dateOrder + part;
}
/**
* does pretty formatting of a date object returned by strToDate()
*
* @param {Object} date A date object, as returned from strToDate()
* @param {Boolean} shortFormat Whether to return a short (12/1/95) date
* @return A formatted date string
* @type String
**/
this.formatDate = function (date, shortFormat) {
if(shortFormat) {
var localeDateOrder = getLocaleDateOrder();
var string = localeDateOrder[0]+"/"+localeDateOrder[1]+"/"+localeDateOrder[2];
return string.replace("y", (date.year !== undefined ? date.year : "00"))
.replace("m", (date.month !== undefined ? 1+date.month : "0"))
.replace("d", (date.day !== undefined ? date.day : "0"));
} else {
var string = "";
if(date.part) {
string += date.part+" ";
}
var months = Utilities_Date.getMonths().long; // no 'this' for translator sandbox
if(date.month != undefined && months[date.month]) {
// get short month strings from CSL interpreter
string += months[date.month];
if(date.day) {
string += " "+date.day+", ";
} else {
string += " ";
}
}
if(date.year) {
string += date.year;
}
}
return string;
}
this.strToISO = function (str) {
var date = this.strToDate(str);
if(date.year) {
var dateString = Zotero.Utilities.lpad(date.year, "0", 4);
if (parseInt(date.month) == date.month) {
dateString += "-"+Zotero.Utilities.lpad(date.month+1, "0", 2);
if(date.day) {
dateString += "-"+Zotero.Utilities.lpad(date.day, "0", 2);
}
}
return dateString;
}
return false;
}
this.sqlToISO8601 = function (sqlDate) {
var date = sqlDate.substr(0, 10);
var matches = date.match(/^([0-9]{4})\-([0-9]{2})\-([0-9]{2})/);
if (!matches) {
return false;
}
date = matches[1];
// Drop parts for reduced precision
if (matches[2] !== "00") {
date += "-" + matches[2];
if (matches[3] !== "00") {
date += "-" + matches[3];
}
}
var time = sqlDate.substr(11);
// TODO: validate times
if (time) {
date += "T" + time + "Z";
}
return date;
}
// A date, optionally followed by a slash and a second date, where each date is a
// 4-digit year with an optional minus sign followed by month/day/qualifier
// characters
var _edtfShapeRE = /^-?[0-9]{4}[-0-9~?%]*(\/-?[0-9]{4}[-0-9~?%]*)?$/;
// Era markers, with optional periods and spaces
var _bceMarker = 'B\\.?\\s?C\\.?(?:\\s?E\\.?)?';
var _ceMarker = 'C\\.?\\s?E\\.?|A\\.?\\s?D\\.?';
var _bceMarkerRE = new RegExp(`^${_bceMarker}$`, 'i');
// A year with an era marker after it ("429 BCE") or, for CE, before it ("AD 429")
var _eraYearRE = new RegExp(
`^(?:(${_ceMarker})\\s*)?([0-9]{1,4})(?:\\s*(${_bceMarker}|${_ceMarker}))?$`, 'i'
);
// A range of years with an era marker on either year
var _eraRangeRE = new RegExp(
`^(?:(${_ceMarker})\\s*)?([0-9]{1,4})(?:\\s*(${_bceMarker}|${_ceMarker}))?`
+ `\\s*-\\s*([0-9]{1,4})(?:\\s*(${_bceMarker}|${_ceMarker}))?$`, 'i'
);
// Convert a year and an era marker to a signed, zero-padded EDTF year
function _toEDTFYear(year, marker) {
return (_bceMarkerRE.test(marker) ? '-' : '') + year.padStart(4, '0');
}
/**
* Convert a year or range of years with an era marker to EDTF, with BCE years as
* negative years
*
* On a range, a marker on only one of the years applies to both ("1000-900 BCE",
* "AD 429-500"), so ranges spanning the eras need both ("100 BCE-50 CE").
*
* @param {String} str
* @return {String|false} - An EDTF year or interval, or false if the string isn't
* a year or range of years with an era marker
*/
function _eraToEDTF(str) {
var match = str.match(_eraRangeRE) || str.match(_eraYearRE);
if (!match) {
return false;
}
var [, prefix, beginYear, beginMarker, endYear, endMarker] = match;
// A marker both before and after the same year is nonsense
if (prefix && beginMarker) {
return false;
}
beginMarker = prefix || beginMarker;
if (!beginMarker && !endMarker) {
return false;
}
// Neither era has a year zero
if (/^0+$/.test(beginYear) || (endYear && /^0+$/.test(endYear))) {
return false;
}
var edtf = _toEDTFYear(beginYear, beginMarker || endMarker);
if (endYear) {
edtf += '/' + _toEDTFYear(endYear, endMarker || beginMarker);
}
return edtf;
}
// A year with an era marker next to it ("429 BCE", "AD 429")
var _eraMarkerRE = new RegExp(
`[0-9]\\s*(?:${_bceMarker}|${_ceMarker})|(?:${_ceMarker})\\s*[0-9]`, 'i'
);
// A negative year, at the beginning of a string or after an interval slash
var _negativeYearRE = /(?:^|\/)-[0-9]/;
var _eraMarkersRE = new RegExp(`${_bceMarker}|${_ceMarker}`, 'gi');
// A circa prefix, in EDTF's notation or in words
var _circaPrefixRE = /^(?:~|circa |ca\.? ?|c\. ?|about |around )\s*/i;
// EDTF punctuation, and nothing a date could be spelled out with
var _edtfCharsRE = /^[-0-9T:+Z~?%/\s.]*$/i;
// Treat en dashes, em dashes, minus signs, and double hyphens as hyphens, since
// ranges are written with all of them ("1995–1996", "1995--1996")
function _normalizeDashes(str) {
return str.replace(/[\u2013\u2014\u2212]/g, '-').replace(/--+/g, '-');
}
// Days in a 1-indexed month, in the proleptic Gregorian calendar
function _daysInMonth(year, month) {
if (month == 2) {
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28;
}
return [4, 6, 9, 11].includes(month) ? 30 : 31;
}
// Check whether a string gives a year in a notation strToDate() misreads, either a
// negative year or an era marker. An era marker counts only if the rest of the
// string is EDTF punctuation, so that spelled-out dates ("January 10 200 BCE") are
// left to strToDate(), which reads their months and days.
function _hasEraNotation(str) {
return _negativeYearRE.test(str)
|| (_eraMarkerRE.test(str) && _edtfCharsRE.test(str.replace(_eraMarkersRE, '')));
}
/**
* Convert a year with an era marker ("200 BCE", "AD 200") to a signed year
*
* strToDate() leaves the marker in the year it returns, which citeproc-js reads as
* a CE year, since it runs date parts through parseInt().
*
* @param {String} str
* @return {Number|false} - A signed year, or false if the string isn't a year with
* an era marker
*/
this.parseEraYear = function (str) {
if (typeof str != 'string') {
return false;
}
var edtf = _eraToEDTF(_normalizeDashes(str.trim()));
if (!edtf || edtf.includes('/')) {
return false;
}
return parseInt(edtf, 10);
};
/**
* Check whether a string is written as an EDTF date -- an EDTF-shaped value, a
* negative year, or an era marker
*
* strToDate() reads a year and a season out of such a string, giving a CE year for
* a BCE date and a season for the second half of a range, so a string that
* parseEDTF() rejects is better treated as a literal date than reinterpreted.
*
* @param {String} str
* @return {Boolean}
*/
this.looksLikeEDTF = function (str) {
if (typeof str != 'string') {
return false;
}
str = _normalizeDashes(str.trim()).replace(_circaPrefixRE, '');
return _hasEraNotation(str) || _edtfShapeRE.test(str);
};
/**
* Parse a string in EDTF (ISO 8601-2) format
*
* Handles the subset of EDTF that maps to other Zotero date handling: dates with
* optional uncertain/approximate qualifiers and closed intervals. Some common
* non-EDTF notations are normalized to EDTF equivalents:
*
* - Dash-separated year ranges ("1995-1996"), including condensed ranges
* ("1995-96", "2021-22"), become intervals, with en dashes and other dashes
* treated as hyphens
* - Circa prefixes ("~1995", "circa 1995", "ca. 1995") become approximate
* qualifiers
* - Era markers become signed years, on a single year ("429 BCE", "AD 429") or
* on a range, where a marker on only one year applies to both ("1000-900 BCE")
*
* Anything else is rejected, including seasons, whose EDTF notation ("2021-22"
* for Summer 2021) collides with condensed ranges, and strings that only parse
* at EDTF level 2 (e.g., "429", which is a level 2 notation for the decade 429X).
*
* @param {String} str
* @return {Object|false} - Object with 'begin' ({year, month, day} -- month 0-indexed,
* as in strToDate()), 'end' (same, for intervals), and 'circa' (true if any part
* is uncertain or approximate); false if the string isn't EDTF or uses unsupported
* features
*/
this.parseEDTF = function (str) {
if (typeof str != 'string') {
return false;
}
str = _normalizeDashes(str.trim());
// Convert a circa prefix to an approximate qualifier below
var circa = _circaPrefixRE.test(str);
if (circa) {
str = str.replace(_circaPrefixRE, '');
}
// Convert era markers to signed years ("429 BCE" to "-0429", "AD 429" to "0429")
var eraEDTF = _eraToEDTF(str);
if (eraEDTF) {
str = eraEDTF;
}
// EDTF contains no spaces and begins with a digit or minus sign
if (!/^[-0-9]\S*$/.test(str)) {
return false;
}
// Allow unpadded negative years (e.g., "-429" for "-0429")
str = str.replace(
/(^|\/)-([0-9]{1,3})(?=[-~?%/]|$)/g,
(m, pre, year) => pre + '-' + year.padStart(4, '0')
);
// Treat dash-separated years as a range ("1995-1996"), including condensed
// ranges ("1995-96", "2021-22") when the second value is greater than the
// first and so can't be a month
var range = str.match(/^([0-9]{4})-([0-9]{2}|[0-9]{4})$/);
if (range) {
let begin = parseInt(range[1], 10);
let end = parseInt(range[2], 10);
if (range[2].length == 2) {
end = end > 12 ? Math.floor(begin / 100) * 100 + end : 0;
}
if (end > begin) {
str = range[1] + '/' + String(end).padStart(4, '0');
}
}
if (circa && !/[~?%]$/.test(str)) {
str += '~';
}
// Reject strings that can't be in the supported EDTF subset,
// e.g., other numeric date formats like "5/13/2021" and "13.5.2021"
if (!_edtfShapeRE.test(str)) {
return false;
}
// A single date: a signed 4-digit year, an optional month and day, and an
// optional trailing uncertain/approximate qualifier ("2004-06~", "-0429?").
// A qualifier anywhere else ("2004-06~-11") is rejected, as are month codes
// above 12, including seasons, and invalid calendar days.
function parseDate(dateStr) {
var m = dateStr.match(/^(-?[0-9]{4})(?:-([0-9]{2})(?:-([0-9]{2}))?)?([~?%])?$/);
if (!m) {
return false;
}
let date = { year: parseInt(m[1], 10) };
if (m[2]) {
let month = parseInt(m[2], 10);
if (month < 1 || month > 12) {
return false;
}
date.month = month - 1;
if (m[3]) {
let day = parseInt(m[3], 10);
if (day < 1 || day > _daysInMonth(date.year, month)) {
return false;
}
date.day = day;
}
}
return { date, circa: !!m[4] };
}
var [beginStr, endStr] = str.split('/');
var begin = parseDate(beginStr);
if (!begin) {
return false;
}
let result = { begin: begin.date };
if (begin.circa) {
result.circa = true;
}
if (endStr !== undefined) {
let end = parseDate(endStr);
if (!end) {
return false;
}
result.end = end.date;
if (end.circa) {
result.circa = true;
}
// Reject reversed and degenerate intervals
let toComparable = date => date.year * 10000
+ (date.month !== undefined ? (date.month + 1) * 100 : 0)
+ (date.day || 0);
if (toComparable(result.end) <= toComparable(result.begin)) {
return false;
}
}
return result;
};
this.strToMultipart = function (str) {
if (!str){
return '';
}
// For an EDTF date, sort by the start of any range, ignoring qualifiers
var edtfDate = this.parseEDTF(str);
var parts;
if (edtfDate) {
parts = edtfDate.begin;
}
// A date whose era strToDate() would drop has no sortable year, since the sort
// key can't represent one anyway
else if (_hasEraNotation(_normalizeDashes(str.trim()).replace(_circaPrefixRE, ''))) {
parts = {};
}
else {
parts = this.strToDate(str);
}
// FIXME: Until we have a better BCE date solution,
// remove year value if not between 1 and 9999
if (parts.year) {
var year = parts.year + '';
if (!year.match(/^[0-9]{1,4}$/)) {
delete parts.year;
}
}
parts.month = typeof parts.month != "undefined" ? parts.month + 1 : '';
var multi = (parts.year ? Zotero.Utilities.lpad(parts.year, '0', 4) : '0000') + '-'
+ Zotero.Utilities.lpad(parts.month, '0', 2) + '-'
+ (parts.day ? Zotero.Utilities.lpad(parts.day, '0', 2) : '00')
+ ' '
+ str;
return multi;
}
// Regexes for multipart and SQL dates
// Allow zeroes in multipart dates
// TODO: Allow negative multipart in DB and here with \-?
var _multipartRE = /^[0-9]{4}\-(0[0-9]|10|11|12)\-(0[0-9]|[1-2][0-9]|30|31) /;
var _sqldateRE = /^\-?[0-9]{4}\-(0[1-9]|10|11|12)\-(0[1-9]|[1-2][0-9]|30|31)$/;
var _sqldateWithZeroesRE = /^\-?[0-9]{4}\-(0[0-9]|10|11|12)\-(0[0-9]|[1-2][0-9]|30|31)$/;
var _sqldatetimeRE = /^\-?[0-9]{4}\-(0[1-9]|10|11|12)\-(0[1-9]|[1-2][0-9]|30|31) ([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])$/;
var _sqlDateTimeWithoutSecondsRE = /^\-?[0-9]{4}\-(0[1-9]|10|11|12)\-(0[1-9]|[1-2][0-9]|30|31) ([0-1][0-9]|[2][0-3]):([0-5][0-9])$/;
/**
* Tests if a string is a multipart date string
* e.g. '2006-11-03 November 3rd, 2006'
*/
this.isMultipart = function (str) {
if (this.isSQLDateTime(str) || this.isSQLDateTimeWithoutSeconds(str)) {
return false;
}
return _multipartRE.test(str);
}
/**
* Returns the SQL part of a multipart date string
* (e.g. '2006-11-03 November 3rd, 2006' returns '2006-11-03')
*/
this.multipartToSQL = function (multi) {
if (!multi){
return '';
}
if (!this.isMultipart(multi)) {
return '0000-00-00';
}
return multi.substr(0, 10);
}
/**
* Returns the user part of a multipart date string
* (e.g. '2006-11-03 November 3rd, 2006' returns 'November 3rd, 2006')
*/
this.multipartToStr = function (multi) {
if (!multi){
return '';
}
if (!this.isMultipart(multi)) {
return multi;
}
return multi.substr(11);
}
/**
* Convert 'yesterday'/'today'/'tomorrow' to SQL date, or else return original string
*
* @param {String} str
* @return {String}