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 | 174x 20x 154x 94x 60x 60x 610x 606x 4x 2x 2x 2x 42x 17715x 5169x 446x 32x 2356x 21x 176x 176x 78x 168x 6x 14x 14x 6x 8x 5x 3x 126x 504x 504x 212x 33x 1181x 53x 1128x 4x 4x 4x 4x 4x 4x 4x 44x 4x 720x 720x 274x 446x 56x 56x 53x 47x 2x 390x 390x 390x 1681x 1681x 5751x 5751x 5751x 5072x 5072x 4846x 1681x 126x 2x 112x 112x 112x 112x | import { Duration } from '../duration'; import { DateTime } from '../datetime'; import { Zone } from '../zone'; import { LocalZone } from '../zones/localZone'; import { IANAZone } from '../zones/IANAZone'; import { FixedOffsetZone } from '../zones/fixedOffsetZone'; import { Settings } from '../settings'; import { InvalidArgumentError } from '../errors'; /** * @private */ export class Util { static friendlyDuration(duration) { if (Util.isNumber(duration)) { return Duration.fromMillis(duration); } else if (duration instanceof Duration) { return duration; } else Eif (duration instanceof Object) { return Duration.fromObject(duration); } else { throw new InvalidArgumentError('Unknown duration argument'); } } static friendlyDateTime(dateTimeish) { if (dateTimeish instanceof DateTime) { return dateTimeish; } else if (dateTimeish.valueOf && Util.isNumber(dateTimeish.valueOf())) { return DateTime.fromJSDate(dateTimeish); } else Eif (dateTimeish instanceof Object) { return DateTime.fromObject(dateTimeish); } else { throw new InvalidArgumentError('Unknown datetime argument'); } } static maybeArray(thing) { return Array.isArray(thing) ? thing : [thing]; } static isUndefined(o) { return typeof o === 'undefined'; } static isNumber(o) { return typeof o === 'number'; } static isString(o) { return typeof o === 'string'; } static isDate(o) { return Object.prototype.toString.call(o) === '[object Date]'; } static numberBetween(thing, bottom, top) { return Util.isNumber(thing) && thing >= bottom && thing <= top; } static pad(input, n = 2) { return ('0'.repeat(n) + input).slice(-n); } static towardZero(input) { return input < 0 ? Math.ceil(input) : Math.floor(input); } // DateTime -> JS date such that the date's UTC time is the datetimes's local time static asIfUTC(dt) { const ts = dt.ts - dt.offset; return new Date(ts); } // http://stackoverflow.com/a/15030117 static flatten(arr) { return arr.reduce( (flat, toFlatten) => flat.concat(Array.isArray(toFlatten) ? Util.flatten(toFlatten) : toFlatten), [] ); } static bestBy(arr, by, compare) { return arr.reduce((best, next) => { const pair = [by(next), next]; if (!best) { return pair; } else if (compare.apply(null, [best[0], pair[0]]) === best[0]) { return best; } else { return pair; } }, null)[1]; } static pick(obj, keys) { return keys.reduce((a, k) => { a[k] = obj[k]; return a; }, {}); } static isLeapYear(year) { return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); } static daysInYear(year) { return Util.isLeapYear(year) ? 366 : 365; } static daysInMonth(year, month) { if (month === 2) { return Util.isLeapYear(year) ? 29 : 28; } else { return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]; } } static parseZoneInfo(ts, offsetFormat, locale, timeZone = null) { const date = new Date(ts), intl = { hour12: false, // avoid AM/PM year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }; Eif (timeZone) { intl.timeZone = timeZone; } const modified = Object.assign({ timeZoneName: offsetFormat }, intl); Eif (Intl.DateTimeFormat.prototype.formatToParts) { const parsed = new Intl.DateTimeFormat(locale, modified) .formatToParts(date) .find(m => m.type.toLowerCase() === 'timezonename'); return parsed ? parsed.value : null; } else { // this probably doesn't work for all locales const without = new Intl.DateTimeFormat(locale, intl).format(date), included = new Intl.DateTimeFormat(locale, modified).format(date), diffed = included.substring(without.length), trimmed = diffed.replace(/^[, ]+/, ''); return trimmed; } } static normalizeZone(input) { Iif (input === null) { return LocalZone.instance; } else if (input instanceof Zone) { return input; } else if (Util.isString(input)) { const lowered = input.toLowerCase(); if (lowered === 'local') return LocalZone.instance; else if (lowered === 'utc') return FixedOffsetZone.utcInstance; else if (IANAZone.isValidSpecier(lowered)) return new IANAZone(input); else return FixedOffsetZone.parseSpecifier(lowered) || Settings.defaultZone; } else Iif (Util.isNumber(input)) { return FixedOffsetZone.instance(input); } else Iif (typeof input === 'object' && input.offset) { // This is dumb, but the instanceof check above doesn't seem to really work // so we're duck checking it return input; } else { return Settings.defaultZone; } } static normalizeObject(obj, normalizer, ignoreUnknown = false) { const normalized = {}; for (const u in obj) { Eif (obj.hasOwnProperty(u)) { const v = obj[u]; if (v !== null && !Util.isUndefined(v) && !Number.isNaN(v)) { const mapped = normalizer(u, ignoreUnknown); if (mapped) { normalized[mapped] = v; } } } } return normalized; } static timeObject(obj) { return Util.pick(obj, ['hour', 'minute', 'second', 'millisecond']); } static untrucateYear(year) { return year > 60 ? 1900 + year : 2000 + year; } // signedOffset('-5', '30') -> -330 static signedOffset(offHourStr, offMinuteStr) { const offHour = parseInt(offHourStr, 10) || 0, offMin = parseInt(offMinuteStr, 10) || 0, offMinSigned = offHour < 0 ? -offMin : offMin; return offHour * 60 + offMinSigned; } } |