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 | 32x 81x 81x 81x 32x 32x 32x 25x 32x 13x 32x 81x 7x 7x 84x 84x 7x 2x 2x 14x 14x 2x 49x 49x 2x 47x 31x 16x 18x 18x 18x 18x 48x 1440x 1440x 1440x 1440x 1440x 1440x 1384x 56x 56x 56x 873x 81x 81x 81x 81x 81x 81x 81x 81x 279x 279x 279x 279x 279x 2x 277x 199x 78x 399x 296x 103x 23x 10x 10x 10x 84x 10x 14x 3x 3x 3x 2x 14x 3x 4x 3x 1x 1x 1x 2x 1x 8x 2x 2x 2x 4x 2x 115x 115x 259x 115x 333x 315x 315x 281x 315x 309x 315x 18x 205x 176x 176x 29x 5x 24x 24x 205x 205x 205x 200x 205x 110x | import { Util } from './util'; import { English } from './english'; import { Settings } from '../settings'; import { DateTime } from '../datetime'; const localeCache = {}; function intlConfigString(locale, numberingSystem, outputCalendar) { let loc = locale || new Intl.DateTimeFormat().resolvedOptions().locale; loc = Array.isArray(locale) ? locale : [locale]; if (outputCalendar || numberingSystem) { loc = loc.map(l => { l += '-u'; if (outputCalendar) { l += '-ca-' + outputCalendar; } if (numberingSystem) { l += '-nu-' + numberingSystem; } return l; }); } return loc; } function mapMonths(f) { const ms = []; for (let i = 1; i <= 12; i++) { const dt = DateTime.utc(2016, i, 1); ms.push(f(dt)); } return ms; } function mapWeekdays(f) { const ms = []; for (let i = 1; i <= 7; i++) { const dt = DateTime.utc(2016, 11, 13 + i); ms.push(f(dt)); } return ms; } function listStuff(loc, length, defaultOK, englishFn, intlFn) { const mode = loc.listingMode(defaultOK); if (mode === 'error') { return null; } else if (mode === 'en') { return englishFn(length); } else { return intlFn(length); } } /** * @private */ class PolyNumberFormatter { constructor(opts) { this.padTo = opts.padTo || 0; this.round = opts.round || false; } format(i) { const maybeRounded = this.round ? Math.round(i) : i; return maybeRounded.toString().padStart(this.padTo, '0'); } } class PolyDateFormatter { format(d) { return d.toString(); } resolvedOptions() { return { locale: 'en-US', numberingSystem: 'latn', outputCalendar: 'gregory' }; } } /** * @private */ export class Locale { static fromOpts(opts) { return Locale.create(opts.locale, opts.numberingSystem, opts.outputCalendar); } static create(locale, numberingSystem, outputCalendar) { const localeR = locale || Settings.defaultLocale, numberingSystemR = numberingSystem || null, outputCalendarR = outputCalendar || null, cacheKey = `${localeR}|${numberingSystemR}|${outputCalendarR}`, cached = localeCache[cacheKey]; if (cached) { return cached; } else { const fresh = new Locale(localeR, numberingSystemR, outputCalendarR); localeCache[cacheKey] = fresh; return fresh; } } static fromObject({ locale, numberingSystem, outputCalendar } = {}) { return Locale.create(locale, numberingSystem, outputCalendar); } constructor(locale, numbering, outputCalendar) { Object.defineProperty(this, 'locale', { value: locale, enumerable: true }); Object.defineProperty(this, 'numberingSystem', { value: numbering || null, enumerable: true }); Object.defineProperty(this, 'outputCalendar', { value: outputCalendar || null, enumerable: true }); Object.defineProperty(this, 'intl', { value: intlConfigString(this.locale, this.numberingSystem, this.outputCalendar), enumerable: false }); // cached usefulness Object.defineProperty(this, 'weekdaysCache', { value: { format: {}, standalone: {} }, enumerable: false }); Object.defineProperty(this, 'monthsCache', { value: { format: {}, standalone: {} }, enumerable: false }); Object.defineProperty(this, 'meridiemCache', { value: null, enumerable: false, writable: true }); Object.defineProperty(this, 'eraCache', { value: {}, enumerable: false, writable: true }); } // todo: cache me listingMode(defaultOk = true) { const hasIntl = Intl && Intl.DateTimeFormat, hasFTP = hasIntl && Intl.DateTimeFormat.prototype.formatToParts, isActuallyEn = this.locale === 'en' || this.locale.toLowerCase() === 'en-us' || (hasIntl && Intl.DateTimeFormat(this.intl) .resolvedOptions() .locale.startsWith('en-US')), hasNoWeirdness = (this.numberingSystem === null || this.numberingSystem === 'latn') && (this.outputCalendar === null || this.outputCalendar === 'gregory'); if (!hasFTP && !(isActuallyEn && hasNoWeirdness) && !defaultOk) { return 'error'; } else if (!hasFTP || (isActuallyEn && hasNoWeirdness)) { return 'en'; } else { return 'intl'; } } clone(alts) { if (!alts || Object.getOwnPropertyNames(alts).length === 0) { return this; } else { return Locale.create( alts.locale || this.locale, alts.numberingSystem || this.numberingSystem, alts.outputCalendar || this.outputCalendar ); } } months(length, format = false, defaultOK = true) { return listStuff(this, length, defaultOK, English.months, () => { const intl = format ? { month: length, day: 'numeric' } : { month: length }, formatStr = format ? 'format' : 'standalone'; if (!this.monthsCache[formatStr][length]) { this.monthsCache[formatStr][length] = mapMonths(dt => this.extract(dt, intl, 'month')); } return this.monthsCache[formatStr][length]; }); } weekdays(length, format = false, defaultOK = true) { return listStuff(this, length, defaultOK, English.weekdays, () => { const intl = format ? { weekday: length, year: 'numeric', month: 'long', day: 'numeric' } : { weekday: length }, formatStr = format ? 'format' : 'standalone'; if (!this.weekdaysCache[formatStr][length]) { this.weekdaysCache[formatStr][length] = mapWeekdays(dt => this.extract(dt, intl, 'weekday') ); } return this.weekdaysCache[formatStr][length]; }); } meridiems(defaultOK = true) { return listStuff( this, undefined, defaultOK, () => English.meridiems, () => { // In theory there could be aribitrary day periods. We're gonna assume there are exactly two // for AM and PM. This is probably wrong, but it's makes parsing way easier. Eif (!this.meridiemCache) { const intl = { hour: 'numeric', hour12: true }; this.meridiemCache = [ DateTime.utc(2016, 11, 13, 9), DateTime.utc(2016, 11, 13, 19) ].map(dt => this.extract(dt, intl, 'dayperiod')); } return this.meridiemCache; } ); } eras(length, defaultOK = true) { return listStuff(this, length, defaultOK, English.eras, () => { const intl = { era: length }; // This is utter bullshit. Different calendars are going to define eras totally differently. What I need is the minimum set of dates // to definitely enumerate them. Eif (!this.eraCache[length]) { this.eraCache[length] = [DateTime.utc(-40, 1, 1), DateTime.utc(2017, 1, 1)].map(dt => this.extract(dt, intl, 'era') ); } return this.eraCache[length]; }); } extract(dt, intlOpts, field) { const [df, d] = this.dtFormatter(dt, intlOpts), results = df.formatToParts(d), matching = results.find(m => m.type.toLowerCase() === field); return matching ? matching.value : null; } numberFormatter(opts = {}, intlOpts = {}) { if (Intl && Intl.NumberFormat) { const realIntlOpts = Object.assign({ useGrouping: false }, intlOpts); if (opts.padTo > 0) { realIntlOpts.minimumIntegerDigits = opts.padTo; } if (opts.round) { realIntlOpts.maximumFractionDigits = 0; } return new Intl.NumberFormat(this.intl, realIntlOpts); } else { return new PolyNumberFormatter(opts); } } dtFormatter(dt, intlOpts = {}) { let d, z; if (dt.zone.universal) { // if we have a fixed-offset zone that isn't actually UTC, // (like UTC+8), we need to make do with just displaying // the time in UTC; the formatter doesn't know how to handle UTC+8 d = Util.asIfUTC(dt); z = 'UTC'; } else if (dt.zone.type === 'local') { d = dt.toJSDate(); } else { d = dt.toJSDate(); z = dt.zone.name; } Eif (Intl && Intl.DateTimeFormat) { const realIntlOpts = Object.assign({}, intlOpts); if (z) { realIntlOpts.timeZone = z; } return [new Intl.DateTimeFormat(this.intl, realIntlOpts), d]; } else { return [new PolyDateFormatter(), d]; } } equals(other) { return ( this.locale === other.locale && this.numberingSystem === other.numberingSystem && this.outputCalendar === other.outputCalendar ); } } |