-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
util_date.js
97 lines (91 loc) · 3.21 KB
/
util_date.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
/*global CSL: true */
CSL.dateMacroAsSortKey = function (state, Item) {
CSL.dateAsSortKey.call(this, state, Item, true);
};
CSL.dateAsSortKey = function (state, Item, isMacro) {
var dp, elem, value, e, yr, prefix, i, ilen, num;
var variable = this.variables[0];
var macroFlag = "empty";
if (isMacro && state.tmp.extension) {
macroFlag = "macro-with-date";
}
dp = Item[variable];
if ("undefined" === typeof dp) {
dp = {"date-parts": [[0]] };
if (!dp.year) {
state.tmp.empty_date = true;
}
}
if ("undefined" === typeof this.dateparts) {
this.dateparts = ["year", "month", "day"];
}
if (dp.raw) {
dp = state.fun.dateparser.parseDateToArray(dp.raw);
} else if (dp["date-parts"]) {
dp = state.dateParseArray(dp);
}
if ("undefined" === typeof dp) {
dp = {};
}
for (i = 0, ilen = CSL.DATE_PARTS_INTERNAL.length; i < ilen; i += 1) {
elem = CSL.DATE_PARTS_INTERNAL[i];
value = 0;
e = elem;
if (e.slice(-4) === "_end") {
e = e.slice(0, -4);
}
if (dp[elem] && this.dateparts.indexOf(e) > -1) {
value = dp[elem];
}
if (elem.slice(0, 4) === "year") {
yr = CSL.Util.Dates[e].numeric(state, value);
var prefix = "Y";
if (yr[0] === "-") {
prefix = "X";
yr = yr.slice(1);
yr = 9999 - parseInt(yr, 10);
}
state.output.append(CSL.Util.Dates[elem.slice(0, 4)].numeric(state, (prefix + yr)), macroFlag);
} else {
value = CSL.Util.Dates[e]["numeric-leading-zeros"](state, value);
// Ugh.
if (!value) {
value = "00";
}
state.output.append(value, macroFlag);
}
}
};
CSL.Engine.prototype.dateParseArray = function (date_obj) {
var ret, field, dpos, ppos, dp, exts, llen, pos, len, pppos, lllen;
ret = {};
for (field in date_obj) {
if (field === "date-parts") {
dp = date_obj["date-parts"];
if (dp.length > 1) {
if (dp[0].length !== dp[1].length) {
CSL.error("CSL data error: element mismatch in date range input.");
}
}
exts = ["", "_end"];
for (var i = 0, ilen = dp.length; i < ilen; i += 1) {
for (var j = 0, jlen = CSL.DATE_PARTS.length; j < jlen; j += 1) {
if ("undefined" === typeof dp[i][j]) {
ret[(CSL.DATE_PARTS[j] + exts[i])] = dp[i][j];
} else {
ret[(CSL.DATE_PARTS[j] + exts[i])] = parseInt(dp[i][j], 10);
}
}
}
} else if (date_obj.hasOwnProperty(field)) {
// XXXX: temporary workaround
if (field === "literal" && "object" === typeof date_obj.literal && "string" === typeof date_obj.literal.part) {
CSL.debug("Warning: fixing up weird literal date value");
ret.literal = date_obj.literal.part;
} else {
ret[field] = date_obj[field];
}
}
}
return ret;
};