|
| 1 | +import DurationFormat from './duration-format-ponyfill.js'; |
| 2 | +const durationRe = /^[-+]?P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/; |
| 3 | +export const unitNames = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second', 'millisecond']; |
| 4 | +export const isDuration = (str) => durationRe.test(str); |
| 5 | +export class Duration { |
| 6 | + constructor(years = 0, months = 0, weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0) { |
| 7 | + this.years = years; |
| 8 | + this.months = months; |
| 9 | + this.weeks = weeks; |
| 10 | + this.days = days; |
| 11 | + this.hours = hours; |
| 12 | + this.minutes = minutes; |
| 13 | + this.seconds = seconds; |
| 14 | + this.milliseconds = milliseconds; |
| 15 | + this.years || (this.years = 0); |
| 16 | + this.sign || (this.sign = Math.sign(this.years)); |
| 17 | + this.months || (this.months = 0); |
| 18 | + this.sign || (this.sign = Math.sign(this.months)); |
| 19 | + this.weeks || (this.weeks = 0); |
| 20 | + this.sign || (this.sign = Math.sign(this.weeks)); |
| 21 | + this.days || (this.days = 0); |
| 22 | + this.sign || (this.sign = Math.sign(this.days)); |
| 23 | + this.hours || (this.hours = 0); |
| 24 | + this.sign || (this.sign = Math.sign(this.hours)); |
| 25 | + this.minutes || (this.minutes = 0); |
| 26 | + this.sign || (this.sign = Math.sign(this.minutes)); |
| 27 | + this.seconds || (this.seconds = 0); |
| 28 | + this.sign || (this.sign = Math.sign(this.seconds)); |
| 29 | + this.milliseconds || (this.milliseconds = 0); |
| 30 | + this.sign || (this.sign = Math.sign(this.milliseconds)); |
| 31 | + this.blank = this.sign === 0; |
| 32 | + } |
| 33 | + abs() { |
| 34 | + return new Duration(Math.abs(this.years), Math.abs(this.months), Math.abs(this.weeks), Math.abs(this.days), Math.abs(this.hours), Math.abs(this.minutes), Math.abs(this.seconds), Math.abs(this.milliseconds)); |
| 35 | + } |
| 36 | + static from(durationLike) { |
| 37 | + var _a; |
| 38 | + if (typeof durationLike === 'string') { |
| 39 | + const str = String(durationLike).trim(); |
| 40 | + const factor = str.startsWith('-') ? -1 : 1; |
| 41 | + const parsed = (_a = str |
| 42 | + .match(durationRe)) === null || _a === void 0 ? void 0 : _a.slice(1).map(x => (Number(x) || 0) * factor); |
| 43 | + if (!parsed) |
| 44 | + return new Duration(); |
| 45 | + return new Duration(...parsed); |
| 46 | + } |
| 47 | + else if (typeof durationLike === 'object') { |
| 48 | + const { years, months, weeks, days, hours, minutes, seconds, milliseconds } = durationLike; |
| 49 | + return new Duration(years, months, weeks, days, hours, minutes, seconds, milliseconds); |
| 50 | + } |
| 51 | + throw new RangeError('invalid duration'); |
| 52 | + } |
| 53 | + static compare(one, two) { |
| 54 | + const now = Date.now(); |
| 55 | + const oneApplied = Math.abs(applyDuration(now, Duration.from(one)).getTime() - now); |
| 56 | + const twoApplied = Math.abs(applyDuration(now, Duration.from(two)).getTime() - now); |
| 57 | + return oneApplied > twoApplied ? -1 : oneApplied < twoApplied ? 1 : 0; |
| 58 | + } |
| 59 | + toLocaleString(locale, opts) { |
| 60 | + return new DurationFormat(locale, opts).format(this); |
| 61 | + } |
| 62 | +} |
| 63 | +export function applyDuration(date, duration) { |
| 64 | + const r = new Date(date); |
| 65 | + if (duration.sign < 0) { |
| 66 | + r.setUTCSeconds(r.getUTCSeconds() + duration.seconds); |
| 67 | + r.setUTCMinutes(r.getUTCMinutes() + duration.minutes); |
| 68 | + r.setUTCHours(r.getUTCHours() + duration.hours); |
| 69 | + r.setUTCDate(r.getUTCDate() + duration.weeks * 7 + duration.days); |
| 70 | + r.setUTCMonth(r.getUTCMonth() + duration.months); |
| 71 | + r.setUTCFullYear(r.getUTCFullYear() + duration.years); |
| 72 | + } |
| 73 | + else { |
| 74 | + r.setUTCFullYear(r.getUTCFullYear() + duration.years); |
| 75 | + r.setUTCMonth(r.getUTCMonth() + duration.months); |
| 76 | + r.setUTCDate(r.getUTCDate() + duration.weeks * 7 + duration.days); |
| 77 | + r.setUTCHours(r.getUTCHours() + duration.hours); |
| 78 | + r.setUTCMinutes(r.getUTCMinutes() + duration.minutes); |
| 79 | + r.setUTCSeconds(r.getUTCSeconds() + duration.seconds); |
| 80 | + } |
| 81 | + return r; |
| 82 | +} |
| 83 | +export function elapsedTime(date, precision = 'second', now = Date.now()) { |
| 84 | + const delta = date.getTime() - now; |
| 85 | + if (delta === 0) |
| 86 | + return new Duration(); |
| 87 | + const sign = Math.sign(delta); |
| 88 | + const ms = Math.abs(delta); |
| 89 | + const sec = Math.floor(ms / 1000); |
| 90 | + const min = Math.floor(sec / 60); |
| 91 | + const hr = Math.floor(min / 60); |
| 92 | + const day = Math.floor(hr / 24); |
| 93 | + const month = Math.floor(day / 30); |
| 94 | + const year = Math.floor(month / 12); |
| 95 | + const i = unitNames.indexOf(precision) || unitNames.length; |
| 96 | + return new Duration(i >= 0 ? year * sign : 0, i >= 1 ? (month - year * 12) * sign : 0, 0, i >= 3 ? (day - month * 30) * sign : 0, i >= 4 ? (hr - day * 24) * sign : 0, i >= 5 ? (min - hr * 60) * sign : 0, i >= 6 ? (sec - min * 60) * sign : 0, i >= 7 ? (ms - sec * 1000) * sign : 0); |
| 97 | +} |
| 98 | +export function roundToSingleUnit(duration, { relativeTo = Date.now() } = {}) { |
| 99 | + relativeTo = new Date(relativeTo); |
| 100 | + if (duration.blank) |
| 101 | + return duration; |
| 102 | + const sign = duration.sign; |
| 103 | + let years = Math.abs(duration.years); |
| 104 | + let months = Math.abs(duration.months); |
| 105 | + let weeks = Math.abs(duration.weeks); |
| 106 | + let days = Math.abs(duration.days); |
| 107 | + let hours = Math.abs(duration.hours); |
| 108 | + let minutes = Math.abs(duration.minutes); |
| 109 | + let seconds = Math.abs(duration.seconds); |
| 110 | + let milliseconds = Math.abs(duration.milliseconds); |
| 111 | + if (milliseconds >= 900) |
| 112 | + seconds += Math.round(milliseconds / 1000); |
| 113 | + if (seconds || minutes || hours || days || weeks || months || years) { |
| 114 | + milliseconds = 0; |
| 115 | + } |
| 116 | + if (seconds >= 55) |
| 117 | + minutes += Math.round(seconds / 60); |
| 118 | + if (minutes || hours || days || weeks || months || years) |
| 119 | + seconds = 0; |
| 120 | + if (minutes >= 55) |
| 121 | + hours += Math.round(minutes / 60); |
| 122 | + if (hours || days || weeks || months || years) |
| 123 | + minutes = 0; |
| 124 | + if (days && hours >= 12) |
| 125 | + days += Math.round(hours / 24); |
| 126 | + if (!days && hours >= 21) |
| 127 | + days += Math.round(hours / 24); |
| 128 | + if (days || weeks || months || years) |
| 129 | + hours = 0; |
| 130 | + const currentYear = relativeTo.getFullYear(); |
| 131 | + const currentMonth = relativeTo.getMonth(); |
| 132 | + const currentDate = relativeTo.getDate(); |
| 133 | + if (days >= 27 || years + months + days) { |
| 134 | + const newMonthDate = new Date(relativeTo); |
| 135 | + newMonthDate.setDate(1); |
| 136 | + newMonthDate.setMonth(currentMonth + months * sign + 1); |
| 137 | + newMonthDate.setDate(0); |
| 138 | + const monthDateCorrection = Math.max(0, currentDate - newMonthDate.getDate()); |
| 139 | + const newDate = new Date(relativeTo); |
| 140 | + newDate.setFullYear(currentYear + years * sign); |
| 141 | + newDate.setDate(currentDate - monthDateCorrection); |
| 142 | + newDate.setMonth(currentMonth + months * sign); |
| 143 | + newDate.setDate(currentDate - monthDateCorrection + days * sign); |
| 144 | + const yearDiff = newDate.getFullYear() - relativeTo.getFullYear(); |
| 145 | + const monthDiff = newDate.getMonth() - relativeTo.getMonth(); |
| 146 | + const daysDiff = Math.abs(Math.round((Number(newDate) - Number(relativeTo)) / 86400000)) + monthDateCorrection; |
| 147 | + const monthsDiff = Math.abs(yearDiff * 12 + monthDiff); |
| 148 | + if (daysDiff < 27) { |
| 149 | + if (days >= 6) { |
| 150 | + weeks += Math.round(days / 7); |
| 151 | + days = 0; |
| 152 | + } |
| 153 | + else { |
| 154 | + days = daysDiff; |
| 155 | + } |
| 156 | + months = years = 0; |
| 157 | + } |
| 158 | + else if (monthsDiff <= 11) { |
| 159 | + months = monthsDiff; |
| 160 | + years = 0; |
| 161 | + } |
| 162 | + else { |
| 163 | + months = 0; |
| 164 | + years = yearDiff * sign; |
| 165 | + } |
| 166 | + if (months || years) |
| 167 | + days = 0; |
| 168 | + } |
| 169 | + if (years) |
| 170 | + months = 0; |
| 171 | + if (weeks >= 4) |
| 172 | + months += Math.round(weeks / 4); |
| 173 | + if (months || years) |
| 174 | + weeks = 0; |
| 175 | + if (days && weeks && !months && !years) { |
| 176 | + weeks += Math.round(days / 7); |
| 177 | + days = 0; |
| 178 | + } |
| 179 | + return new Duration(years * sign, months * sign, weeks * sign, days * sign, hours * sign, minutes * sign, seconds * sign, milliseconds * sign); |
| 180 | +} |
| 181 | +export function getRelativeTimeUnit(duration, opts) { |
| 182 | + const rounded = roundToSingleUnit(duration, opts); |
| 183 | + if (rounded.blank) |
| 184 | + return [0, 'second']; |
| 185 | + for (const unit of unitNames) { |
| 186 | + if (unit === 'millisecond') |
| 187 | + continue; |
| 188 | + const val = rounded[`${unit}s`]; |
| 189 | + if (val) |
| 190 | + return [val, unit]; |
| 191 | + } |
| 192 | + return [0, 'second']; |
| 193 | +} |
0 commit comments