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 | 32x 146x 158x 174x 92x 24x 2x 22x 46x 6x 2x 345x 345x 345x 345x 345x 345x 345x 345x 345x 345x 1x 344x 2x 1x 41x 18x 7x 5x 6x 35x 2x 11x 2x 11x 11x 11x 2x 2x 3x 4x 8x 4x 2x 2x 154x 345x 345x 345x 333x 46x 46x 42x 42x 42x 311x 311x 311x 311x 172x 311x 42x 4x 46x 172x 11x 11x 11x 13x 30x 2x 31x 36x 9x 3x 2x 13x 46x 6x 40x 2x 38x 46x 1x 46x 2x 46x 172x 172x 159x 172x 46x 48x 48x 345x 335x 48x 2x 46x 46x 46x 46x 48x 48x | import { Util } from './util'; import { Formatter } from './formatter'; import { FixedOffsetZone } from '../zones/fixedOffsetZone'; import { IANAZone } from '../zones/IANAZone'; const MISSING_FTP = 'missing Intl.DateTimeFormat.formatToParts support'; function intUnit(regex, post = i => i) { return { regex, deser: ([s]) => post(parseInt(s, 10)) }; } function fixListRegex(s) { // make dots optional and also make them literal return s.replace(/\./, '\\.?'); } function stripInsensitivities(s) { return s.replace(/\./, '').toLowerCase(); } function oneOf(strings, startIndex) { if (strings === null) { return null; } else { return { regex: RegExp(strings.map(fixListRegex).join('|')), deser: ([s]) => strings.findIndex(i => stripInsensitivities(s) === stripInsensitivities(i)) + startIndex }; } } function offset(regex, groups) { return { regex, deser: ([, h, m]) => Util.signedOffset(h, m), groups }; } function simple(regex) { return { regex, deser: ([s]) => s }; } function unitForToken(token, loc) { const one = /\d/, two = /\d\d/, three = /\d{3}/, four = /\d{4}/, oneOrTwo = /\d\d?/, oneToThree = /\d\d{2}?/, twoToFour = /\d\d\d{2}?/, literal = t => ({ regex: RegExp(t.val), deser: ([s]) => s, literal: true }), unitate = t => { if (token.literal) { return literal(t); } switch (t.val) { // era case 'G': return oneOf(loc.eras('short', false), 0); case 'GG': return oneOf(loc.eras('long', false), 0); // years case 'yyyy': return intUnit(four); case 'yy': return intUnit(twoToFour, Util.untruncateYear); // months case 'M': return intUnit(oneOrTwo); case 'MM': return intUnit(two); case 'MMM': return oneOf(loc.months('short', false, false), 1); case 'MMMM': return oneOf(loc.months('long', false, false), 1); case 'L': return intUnit(oneOrTwo); case 'LL': return intUnit(two); case 'LLL': return oneOf(loc.months('short', true, false), 1); case 'LLLL': return oneOf(loc.months('long', true, false), 1); // dates case 'd': return intUnit(oneOrTwo); case 'dd': return intUnit(two); // ordinals case 'o': return intUnit(oneToThree); case 'ooo': return intUnit(three); // time case 'HH': return intUnit(two); case 'H': return intUnit(oneOrTwo); case 'hh': return intUnit(two); case 'h': return intUnit(oneOrTwo); case 'mm': return intUnit(two); case 'm': return intUnit(oneOrTwo); case 's': return intUnit(oneOrTwo); case 'ss': return intUnit(two); case 'S': return intUnit(oneToThree); case 'SSS': return intUnit(three); // meridiem case 'a': return oneOf(loc.meridiems(), 0); // weekYear (k) case 'kkkk': return intUnit(four); case 'kk': return intUnit(twoToFour, Util.untruncateYear); // weekNumber (W) case 'W': return intUnit(oneOrTwo); case 'WW': return intUnit(two); // weekdays case 'E': case 'c': return intUnit(one); case 'EEE': return oneOf(loc.weekdays('short', false, false), 1); case 'EEEE': return oneOf(loc.weekdays('long', false, false), 1); case 'ccc': return oneOf(loc.weekdays('short', true, false), 1); case 'cccc': return oneOf(loc.weekdays('long', true, false), 1); // offset/zone case 'Z': case 'ZZ': return offset(/([+-]\d{1,2})(?::(\d{2}))?/, 2); case 'ZZZ': return offset(/([+-]\d{1,2})(\d{2})?/, 2); // we don't support ZZZZ (PST) or ZZZZZ (Pacific Standard Time) in parsing // because we don't have any way to figure out what they are case 'z': return simple(/[A-Za-z_]+\/[A-Za-z_]+/); default: return literal(t); } }; const unit = unitate(token) || { invalidReason: MISSING_FTP }; unit.token = token; return unit; } function buildRegex(units) { return [units.map(u => u.regex).reduce((f, r) => `${f}(${r.source})`, ''), units]; } function match(input, regex, handlers) { const matches = input.match(regex); if (matches) { const all = {}; let matchIndex = 1; for (const i in handlers) { Eif (handlers.hasOwnProperty(i)) { const h = handlers[i], groups = h.groups ? h.groups + 1 : 1; if (!h.literal && h.token) { all[h.token.val[0]] = h.deser(matches.slice(matchIndex, matchIndex + groups)); } matchIndex += groups; } } return [matches, all]; } else { return [matches, {}]; } } function dateTimeFromMatches(matches) { const toField = token => { switch (token) { case 'S': return 'millisecond'; case 's': return 'second'; case 'm': return 'minute'; case 'h': case 'H': return 'hour'; case 'd': return 'day'; case 'o': return 'ordinal'; case 'L': case 'M': return 'month'; case 'y': return 'year'; case 'E': case 'c': return 'weekday'; case 'W': return 'weekNumber'; case 'k': return 'weekYear'; default: return null; } }; let zone; if (!Util.isUndefined(matches.Z)) { zone = new FixedOffsetZone(matches.Z); } else if (!Util.isUndefined(matches.z)) { zone = new IANAZone(matches.z); } else { zone = null; } if (!Util.isUndefined(matches.h) && matches.a === 1) { matches.h += 12; } if (matches.G === 0 && matches.y) { matches.y = -matches.y; } const vals = Object.keys(matches).reduce((r, k) => { const f = toField(k); if (f) { r[f] = matches[k]; } return r; }, {}); return [vals, zone]; } /** * @private */ export class TokenParser { constructor(loc) { Object.defineProperty(this, 'loc', { value: loc, enumerable: true }); } explainParse(input, format) { const tokens = Formatter.parseFormat(format), units = tokens.map(t => unitForToken(t, this.loc)), disqualifyingUnit = units.find(t => t.invalidReason); if (disqualifyingUnit) { return { input, tokens, invalidReason: disqualifyingUnit.invalidReason }; } else { const [regex, handlers] = buildRegex(units), [rawMatches, matches] = match(input, RegExp(regex, 'i'), handlers), [result, zone] = matches ? dateTimeFromMatches(matches) : [null, null]; return { input, tokens, regex, rawMatches, matches, result, zone }; } } parseDateTime(input, format) { const { result, zone, invalidReason } = this.explainParse(input, format); return [result, zone, invalidReason]; } } |