/
util_dateparser.js
553 lines (509 loc) · 19.6 KB
/
util_dateparser.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
/*global CSL: true */
CSL.DateParser = function () {
/*
* Fixed values
*/
// jse imperial years
var epochPairs = [
["\u660E\u6CBB", 1867],
["\u5927\u6B63", 1911],
["\u662D\u548C", 1925],
["\u5E73\u6210", 1988]
];
// years by jse imperial epoch
var epochYearByName = {};
for (var i=0,ilen=epochPairs.length; i<ilen; i++) {
var key = epochPairs[i][0];
var val = epochPairs[i][1];
epochYearByName[key] = val;
}
var epochMatchStrings = [];
var epochMap = {};
for (var i=0,ilen=epochPairs.length; i<ilen; i++) {
var pair = epochPairs[i];
var val = pair[0];
epochMatchStrings.push(val);
epochMap[pair[0]] = pair[1];
}
var epochMatchString = epochMatchStrings.join("|");
// regular expression to trap year name and year
// (splitter and matcher, to cope with ancient JS implementations)
var epochSplitter = new RegExp("(?:" + epochMatchString + ")(?:[0-9]+)");
var epochMatcher = new RegExp("(?:" + epochMatchString + ")(?:[0-9]+)", "g");
// regular expression for month or day kanji label
var kanjiMonthDay = /(\u6708|\u5E74)/g;
// regular expression for year kanji label
var kanjiYear = /\u65E5/g;
// regular expression for double-width Japanese range marker
var kanjiRange = /\u301c/g;
// parsing regexps for normalized strings
// raw materials
var yearLast = "(?:[?0-9]{1,2}%%NUMD%%){0,2}[?0-9]{4}(?![0-9])";
var yearFirst = "[?0-9]{4}(?:%%NUMD%%[?0-9]{1,2}){0,2}(?![0-9])";
var numberVal = "[?0-9]{1,3}";
var rangeSeparator = "[%%DATED%%]";
var fuzzyChar = "[?~]";
var chars = "[^\-\/\~\?0-9]+";
var rexString = "(" + yearFirst + "|" + yearLast + "|" + numberVal + "|" + rangeSeparator + "|" + fuzzyChar + "|" + chars + ")";
// composed regexps
var rexDash = new RegExp(rexString.replace(/%%NUMD%%/g, "-").replace(/%%DATED%%/g, "-"));
var rexDashSlash = new RegExp(rexString.replace(/%%NUMD%%/g, "-").replace(/%%DATED%%/g, "\/"));
var rexSlashDash = new RegExp(rexString.replace(/%%NUMD%%/g, "\/").replace(/%%DATED%%/g, "-"));
/*
* Mutable values
*/
// months
var monthString = "january february march april may june july august september october november december spring summer fall winter spring summer";
this.monthStrings = monthString.split(" ");
/*
* Configuration functions
*/
this.setOrderDayMonth = function() {
// preferred ordering for numeric dates
this.monthGuess = 1;
this.dayGuess = 0;
};
this.setOrderMonthDay = function() {
// preferred ordering for numeric dates
this.monthGuess = 0;
this.dayGuess = 1;
};
this.resetDateParserMonths = function() {
// Function to reset months to default.
this.monthSets = [];
for (var i=0,ilen=this.monthStrings.length; i<ilen; i++) {
this.monthSets.push([this.monthStrings[i]]);
}
this.monthAbbrevs = [];
for (var i=0,ilen=this.monthSets.length; i<ilen; i++) {
this.monthAbbrevs.push([]);
for (var j=0,jlen=this.monthSets[i].length; j<jlen; j++) {
this.monthAbbrevs[i].push(this.monthSets[i][0].slice(0, 3));
}
}
this.monthRexes = [];
for (var i=0,ilen=this.monthAbbrevs.length; i<ilen; i++) {
this.monthRexes.push(new RegExp("(?:" + this.monthAbbrevs[i].join("|") + ")"));
}
};
this.addDateParserMonths = function(lst) {
// Extend list of months with an additional set of month abbreviations,
// extending strings as required to resolve ambiguities.
// Normalize string to list
if ("string" === typeof lst) {
lst = lst.split(/\s+/);
}
// Check that there are twelve (or sixteen) to add
if (lst.length !== 12 && lst.length !== 16) {
CSL.debug("month [+season] list of "+lst.length+", expected 12 or 16. Ignoring.");
return;
}
// Extend as necessary to resolve ambiguities
// For each new month string ...
for (var i=0,ilen=lst.length; i<ilen; i++) {
var abbrevLength = null;
var skip = false;
var insert = 3;
var extendedSets = {};
for (var j=0,jlen=this.monthAbbrevs.length; j<jlen; j++) {
extendedSets[j] = {};
if (j === i) {
// Mark for skipping if same as an existing abbreviation of same month
for (var k=0,klen=this.monthAbbrevs[i].length; k<klen; k++) {
if (this.monthAbbrevs[i][k] === lst[i].slice(0, this.monthAbbrevs[i][k].length)) {
skip = true;
break;
}
}
} else {
// Mark for extending if same as existing abbreviation of any expression of another month
for (var k=0,klen=this.monthAbbrevs[j].length; k<klen; k++) {
abbrevLength = this.monthAbbrevs[j][k].length;
if (this.monthAbbrevs[j][k] === lst[i].slice(0, abbrevLength)) {
while (this.monthSets[j][k].slice(0, abbrevLength) === lst[i].slice(0, abbrevLength)) {
// Abort when full length is hit, otherwise extend
if (abbrevLength > lst[i].length || abbrevLength > this.monthSets[j][k].length) {
CSL.debug("unable to disambiguate month string in date parser: "+lst[i]);
break;
} else {
// Mark both new entry and existing abbrev for extension
abbrevLength += 1;
}
}
insert = abbrevLength;
extendedSets[j][k] = abbrevLength;
}
}
}
for (var jKey in extendedSets) {
for (var kKey in extendedSets[jKey]) {
abbrevLength = extendedSets[jKey][kKey];
jKey = parseInt(jKey, 10);
kKey = parseInt(kKey, 10);
this.monthAbbrevs[jKey][kKey] = this.monthSets[jKey][kKey].slice(0, abbrevLength);
}
}
}
// Insert here
if (!skip) {
this.monthSets[i].push(lst[i]);
this.monthAbbrevs[i].push(lst[i].slice(0, insert));
}
}
// Compose
this.monthRexes = [];
this.monthRexStrs = [];
for (var i=0,ilen=this.monthAbbrevs.length; i<ilen; i++) {
this.monthRexes.push(new RegExp("^(?:" + this.monthAbbrevs[i].join("|") + ")"));
this.monthRexStrs.push("^(?:" + this.monthAbbrevs[i].join("|") + ")");
}
if (this.monthAbbrevs.length === 18) {
for (var i=12,ilen=14; i<ilen; i++) {
this.monthRexes[i+4] = new RegExp("^(?:" + this.monthAbbrevs[i].join("|") + ")");
this.monthRexStrs[i+4] = "^(?:" + this.monthAbbrevs[i].join("|") + ")";
}
}
};
/*
* Conversion functions
*/
this.convertDateObjectToArray = function (thedate) {
// Converts object in place and returns object
thedate["date-parts"] = [];
thedate["date-parts"].push([]);
var slicelen = 0;
var part;
for (var i=0,ilen=3; i<ilen; i++) {
part = ["year", "month", "day"][i];
if (!thedate[part]) {
break;
}
slicelen += 1;
thedate["date-parts"][0].push(thedate[part]);
delete thedate[part];
}
thedate["date-parts"].push([]);
for (var i=0, ilen=slicelen; i<ilen; i++) {
part = ["year_end", "month_end", "day_end"][i];
if (!thedate[part]) {
break;
}
thedate["date-parts"][1].push(thedate[part]);
delete thedate[part];
}
if (thedate["date-parts"][0].length !== thedate["date-parts"][1].length) {
thedate["date-parts"].pop();
}
return thedate;
};
// XXXX String output is currently unable to represent ranges
this.convertDateObjectToString = function(thedate) {
// Returns string
var ret = [];
for (var i = 0, ilen = 3; i < ilen; i += 1) {
if (thedate[CSL.DATE_PARTS_ALL[i]]) {
ret.push(thedate[CSL.DATE_PARTS_ALL[i]]);
} else {
break;
}
}
return ret.join("-");
};
/*
* Utility function
*/
this._parseNumericDate = function (ret, delim, suff, txt) {
if (!suff) {
suff = "";
}
var lst = txt.split(delim);
for (var i=0, ilen=lst.length; i<ilen; i++) {
if (lst[i].length === 4) {
ret[("year" + suff)] = lst[i].replace(/^0*/, "");
if (!i) {
lst = lst.slice(1);
} else {
lst = lst.slice(0, i);
}
break;
}
}
for (var i=0,ilen=lst.length; i<ilen; i++) {
lst[i] = parseInt(lst[i], 10);
}
if (lst.length === 1 || (lst.length === 2 && !lst[1])) {
ret[("month" + suff)] = "" + lst[0];
} else if (lst.length === 2) {
if (lst[this.monthGuess] > 12) {
ret[("month" + suff)] = "" + lst[this.dayGuess];
ret[("day" + suff)] = "" + lst[this.monthGuess];
} else {
ret[("month" + suff)] = "" + lst[this.monthGuess];
ret[("day" + suff)] = "" + lst[this.dayGuess];
}
}
};
/*
* Parsing functions
*/
this.parseDateToObject = function (txt) {
//
// Normalize the format and the year if it's a Japanese date
//
var orig = txt;
var slashPos = -1;
var dashPos = -1;
var yearIsNegative = false;
var lst;
if (txt) {
txt = txt.replace(/^(.*[0-9])T[0-9].*/, "$1");
// If string leads with a minus sign, strip and memo it.
if (txt.slice(0, 1) === "-") {
yearIsNegative = true;
txt = txt.slice(1);
}
// If string is a number of 1 to 3 characters only, treat as year.
if (txt.match(/^[0-9]{1,3}$/)) {
while (txt.length < 4) {
txt = "0" + txt;
}
}
// Normalize to string
txt = "" + txt;
// Remove things that look like times
txt = txt.replace(/\s*[0-9]{2}:[0-9]{2}(?::[0-9]+)/,"");
var m = txt.match(kanjiMonthDay);
if (m) {
txt = txt.replace(/\s+/g, "");
txt = txt.replace(kanjiYear, "");
txt = txt.replace(kanjiMonthDay, "-");
txt = txt.replace(kanjiRange, "/");
txt = txt.replace(/\-\//g, "/");
txt = txt.replace(/-$/g,"");
// Tortuous workaround for IE6
var slst = txt.split(epochSplitter);
lst = [];
var mm = txt.match(epochMatcher);
if (mm) {
var mmx = [];
for (var i=0,ilen=mm.length; i<ilen; i++) {
mmx = mmx.concat(mm[i].match(/([^0-9]+)([0-9]+)/).slice(1));
}
for (var i=0,ilen=slst.length; i<ilen; i++) {
lst.push(slst[i]);
if (i !== (ilen - 1)) {
// pos is undeclared, and multiplying by 2 here is insane.
var mmpos = (i * 2);
lst.push(mmx[mmpos]);
lst.push(mmx[mmpos + 1]);
}
}
} else {
lst = slst;
}
// workaround duly applied, this now works
for (var i=1,ilen=lst.length; i<ilen; i+=3) {
lst[i + 1] = epochMap[lst[i]] + parseInt(lst[i + 1], 10);
lst[i] = "";
}
txt = lst.join("");
txt = txt.replace(/\s*-\s*$/, "").replace(/\s*-\s*\//, "/");
//
// normalize date and identify delimiters
//
txt = txt.replace(/\.\s*$/, "");
// not sure what this is meant to do
txt = txt.replace(/\.(?! )/, "");
slashPos = txt.indexOf("/");
dashPos = txt.indexOf("-");
}
}
// drop punctuation from a.d., b.c.
txt = txt.replace(/([A-Za-z])\./g, "$1");
var number = "";
var note = "";
var thedate = {};
var rangeDelim;
var dateDelim;
if (txt.slice(0, 1) === "\"" && txt.slice(-1) === "\"") {
thedate.literal = txt.slice(1, -1);
return thedate;
}
if (slashPos > -1 && dashPos > -1) {
var slashCount = txt.split("/");
if (slashCount.length > 3) {
rangeDelim = "-";
txt = txt.replace(/\_/g, "-");
dateDelim = "/";
lst = txt.split(rexSlashDash);
} else {
rangeDelim = "/";
txt = txt.replace(/\_/g, "/");
dateDelim = "-";
lst = txt.split(rexDashSlash);
}
} else {
txt = txt.replace(/\//g, "-");
txt = txt.replace(/\_/g, "-");
rangeDelim = "-";
dateDelim = "-";
lst = txt.split(rexDash);
}
var ret = [];
for (var i=0,ilen=lst.length; i<ilen; i++) {
var m = lst[i].match(/^\s*([\-\/]|[^\-\/\~\?0-9]+|[\-~?0-9]+)\s*$/);
if (m) {
ret.push(m[1]);
}
}
//
// Phase 2
//
var delimPos = ret.indexOf(rangeDelim);
var delims = [];
var isRange = false;
if (delimPos > -1) {
delims.push([0, delimPos]);
delims.push([(delimPos + 1), ret.length]);
isRange = true;
} else {
delims.push([0, ret.length]);
}
//
// For each side of a range divide ...
//
var suff = "";
for (var i=0,ilen=delims.length; i<ilen; i++) {
var delim = delims[i];
//
// Process each element ...
//
var date = ret.slice(delim[0], delim[1]);
outer:
for (var j=0,jlen=date.length; j<jlen; j++) {
var element = date[j];
//
// If it's a numeric date, process it.
//
if (element.indexOf(dateDelim) > -1) {
this._parseNumericDate(thedate, dateDelim, suff, element);
continue;
}
//
// If it's an obvious year, record it.
//
if (element.match(/[0-9]{4}/)) {
thedate[("year" + suff)] = element.replace(/^0*/, "");
continue;
}
//
// If it's a fuzzy marker, record it.
//
if (element === "~" || element === "?" || element === "c" || element.match(/^cir/)) {
thedate.circa = true;
}
//
// If it's a month, record it.
//
for (var k=0,klen=this.monthRexes.length; k<klen; k++) {
if (element.toLocaleLowerCase().match(this.monthRexes[k])) {
thedate[("month" + suff)] = "" + (parseInt(k, 10) + 1);
continue outer;
}
}
//
// If it's a number, make a note of it
//
if (element.match(/^[0-9]+$/)) {
number = element;
}
//
// If it's a BC or AD marker, make a year of
// any note. Separate, reverse the sign of the year
// if it's BC.
//
if (element.toLocaleLowerCase().match(/^bc/) && number) {
thedate[("year" + suff)] = "" + (number * -1);
number = "";
continue;
}
if (element.toLocaleLowerCase().match(/^ad/) && number) {
thedate[("year" + suff)] = "" + number;
number = "";
continue;
}
//
// If it's cruft, make a note of it
//
if (element.toLocaleLowerCase().match(/(?:mic|tri|hil|eas)/) && !thedate[("season" + suff)]) {
note = element;
continue;
}
}
//
// If at the end of the string there's still a note
// hanging around, make a day of it.
//
if (number) {
thedate[("day" + suff)] = number;
number = "";
}
//
// If at the end of the string there's cruft lying
// around, and the season field is empty, put the
// cruft there.
//
if (note && !thedate[("season" + suff)]) {
thedate[("season" + suff)] = note.trim();
note = "";
}
suff = "_end";
}
//
// update any missing elements on each side of the divide
// from the other
//
if (isRange) {
for (var j=0,jlen=CSL.DATE_PARTS_ALL.length; j<jlen; j++) {
var item = CSL.DATE_PARTS_ALL[j];
if (thedate[item] && !thedate[(item + "_end")]) {
thedate[(item + "_end")] = thedate[item];
} else if (!thedate[item] && thedate[(item + "_end")]) {
thedate[item] = thedate[(item + "_end")];
}
}
}
//
// If there's no year, or if there only a year and a day, it's a failure; pass through the literal
//
if (!thedate.year || (thedate.year && thedate.day && !thedate.month)) {
thedate = { "literal": orig };
}
var parts = ["year", "month", "day", "year_end", "month_end", "day_end"];
for (var i=0,ilen=parts.length; i<ilen; i++) {
var part = parts[i];
if ("string" === typeof thedate[part] && thedate[part].match(/^[0-9]+$/)) {
thedate[part] = parseInt(thedate[part], 10);
}
}
if (yearIsNegative && Object.keys(thedate).indexOf("year") > -1) {
thedate.year = (thedate.year * -1);
}
return thedate;
};
this.parseDateToArray = function(txt) {
return this.convertDateObjectToArray(this.parseDateToObject(txt));
};
this.parseDateToString = function(txt) {
return this.convertDateObjectToString(this.parseDateToObject(txt));
};
this.parse = function(txt) {
return this.parseDateToObject(txt);
};
/*
* Setup
*/
this.setOrderMonthDay();
this.resetDateParserMonths();
};
CSL.DateParser = new CSL.DateParser();