From 052e649012ecd950b7967bd58e8776b3403a6681 Mon Sep 17 00:00:00 2001 From: Radiquum Date: Sun, 24 Aug 2025 23:21:07 +0500 Subject: [PATCH] anix/fix: incorrect format of watched time statistic --- app/api/utils.ts | 58 +++++++++++-------- app/components/Profile/Profile.Stats.tsx | 2 +- .../ReleaseInfo/ReleaseInfo.Info.tsx | 2 +- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/app/api/utils.ts b/app/api/utils.ts index fabc2b7..0567993 100644 --- a/app/api/utils.ts +++ b/app/api/utils.ts @@ -248,31 +248,43 @@ export function sinceUnixDate(unixInSeconds: number) { ); } -export function minutesToTime( - min: number, - type?: "full" | "daysOnly" | "daysHours" -) { - const d = Math.floor(min / 1440); // 60*24 - const h = Math.floor((min - d * 1440) / 60); - const m = Math.round(min % 60); +export function minutesToTime(min: number) { + const seconds = min * 60; + const epoch = new Date(0); + const date = new Date(seconds * 1000); - var dDisplay = - d > 0 ? `${d} ${numberDeclension(d, "день", "дня", "дней")}` : ""; - var hDisplay = - h > 0 ? `${h} ${numberDeclension(h, "час", "часа", "часов")}` : ""; - var mDisplay = - m > 0 ? `${m} ${numberDeclension(m, "минута", "минуты", "минут")}` : ""; + const diffInMinutes = + new Date(date.getTime() - epoch.getTime()).getTime() / 1000 / 60; - if (type == "daysOnly") { - if (d > 0) return dDisplay; - return "? дней"; - } else if (type == "daysHours") { - if (d > 0 && h > 0) return dDisplay + ", " + hDisplay; - if (h > 0) return hDisplay; - if (m > 0) return mDisplay; - } else { - return `${d > 0 ? dDisplay : ""}${h > 0 ? ", " + hDisplay : ""}${m > 0 ? ", " + mDisplay : ""}`; - } + let days = Math.floor(diffInMinutes / 1440); + if (days < 0) days = 0; + const daysToMinutes = days * 1440; + + let hours = Math.floor((diffInMinutes - daysToMinutes) / 60); + if (hours < 0) hours = 0; + const hoursToMinutes = hours * 60; + + let minutes = diffInMinutes - daysToMinutes - hoursToMinutes; + if (minutes < 0) minutes = 0; + + const dayDisplay = + days > 0 ? `${days} ${numberDeclension(days, "день", "дня", "дней")}` : ""; + const hourDisplay = + hours > 0 ? + `${hours} ${numberDeclension(hours, "час", "часа", "часов")}` + : ""; + const minuteDisplay = + minutes > 0 ? + `${minutes} ${numberDeclension(minutes, "минута", "минуты", "минут")}` + : ""; + + if (days > 0 && hours > 0 && minutes > 0) return `${dayDisplay}, ${hourDisplay}, ${minuteDisplay}`; + if (days > 0 && hours > 0) return `${dayDisplay}, ${hourDisplay}`; + if (days > 0 && minutes > 0) return `${dayDisplay}, ${minuteDisplay}`; + if (hours > 0 && minutes > 0) return `${hourDisplay}, ${minuteDisplay}`; + if (days > 0) return dayDisplay; + if (hours > 0) return hourDisplay; + if (minutes > 0) return minuteDisplay; } const StatusList: Record = { diff --git a/app/components/Profile/Profile.Stats.tsx b/app/components/Profile/Profile.Stats.tsx index 214c1cb..e264905 100644 --- a/app/components/Profile/Profile.Stats.tsx +++ b/app/components/Profile/Profile.Stats.tsx @@ -112,7 +112,7 @@ export const ProfileStats = (props: {

Время просмотра:{" "} - ~{minutesToTime(props.watched_time, "daysHours")} + ~{minutesToTime(props.watched_time)}

diff --git a/app/components/ReleaseInfo/ReleaseInfo.Info.tsx b/app/components/ReleaseInfo/ReleaseInfo.Info.tsx index 631ca88..0ec3cd3 100644 --- a/app/components/ReleaseInfo/ReleaseInfo.Info.tsx +++ b/app/components/ReleaseInfo/ReleaseInfo.Info.tsx @@ -59,7 +59,7 @@ export const ReleaseInfoInfo = (props: { {"/"} {props.episodes.total ? props.episodes.total + " эп. " : "? эп. "} {props.duration != 0 && - `по ${minutesToTime(props.duration, "daysHours")}`} + `по ${minutesToTime(props.duration)}`}