Spaces:
Sleeping
Sleeping
File size: 1,614 Bytes
d9d8daf | 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 | function parseIsoDateOnly(value) {
const text = String(value ?? '').trim()
const isoMatch = text.match(/^(\d{4})-(\d{2})-(\d{2})$/)
if (!isoMatch) return null
const year = Number(isoMatch[1])
const month = Number(isoMatch[2])
const day = Number(isoMatch[3])
const date = new Date(Date.UTC(year, month - 1, day))
if (
Number.isNaN(date.getTime())
|| date.getUTCFullYear() !== year
|| date.getUTCMonth() !== month - 1
|| date.getUTCDate() !== day
) {
return null
}
return date
}
function subtractMonthsUtc(date, months) {
const year = date.getUTCFullYear()
const month = date.getUTCMonth()
const day = date.getUTCDate()
const base = new Date(Date.UTC(year, month - months, 1))
const lastDayOfTargetMonth = new Date(
Date.UTC(base.getUTCFullYear(), base.getUTCMonth() + 1, 0),
).getUTCDate()
base.setUTCDate(Math.min(day, lastDayOfTargetMonth))
return base
}
export function getFaixaDataRecencyInfo(periodo) {
const dataFinal = parseIsoDateOnly(periodo?.max ?? periodo?.data_final)
if (!dataFinal) return { tone: '', label: '' }
const now = new Date()
const hojeUtc = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate()))
if (dataFinal > hojeUtc) return { tone: '', label: '' }
if (dataFinal <= subtractMonthsUtc(hojeUtc, 24)) {
return { tone: '2y', label: '>= 2 anos' }
}
if (dataFinal <= subtractMonthsUtc(hojeUtc, 12)) {
return { tone: '1y', label: '>= 1 ano' }
}
if (dataFinal <= subtractMonthsUtc(hojeUtc, 6)) {
return { tone: '6m', label: '>= 6 meses' }
}
return { tone: '', label: '' }
}
|