From 9319dc22c97075648d6a0f32927f70dd94b026c0 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 21 Jun 2024 16:02:40 +0200 Subject: [PATCH] [backend/core] Refactor NumberExtensions --- .../Core/Extensions/NumberExtensions.cs | 35 +++++-------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/Iceshrimp.Backend/Core/Extensions/NumberExtensions.cs b/Iceshrimp.Backend/Core/Extensions/NumberExtensions.cs index baa637fb..7d5fa33f 100644 --- a/Iceshrimp.Backend/Core/Extensions/NumberExtensions.cs +++ b/Iceshrimp.Backend/Core/Extensions/NumberExtensions.cs @@ -20,39 +20,20 @@ public static class NumberExtensions return result.ToString(); } - public static string ToBase36(this int input) - { - if (input == 0) return "0"; - var result = new StringBuilder(); - - while (input >= 0) - { - result.Insert(0, Base36Charset[input % 36]); - input /= 36; - } - - return result.ToString(); - } + public static string ToBase36(this int input) => ToBase36((long)input); public static string ToDurationDisplayString(this long input) { + var ts = TimeSpan.FromMilliseconds(input); + return input switch { < 1000 => $"{input} ms", - < 1000 * 60 => $"{Math.Round(input / 1000d / 60d, 2)} s", - < 1000 * 60 * 60 => $"{Math.Round(input / 60000d / 60d, 2)} m", - _ => $"{Math.Round(input / 60000d / 60d / 60d, 2)} h" - }; - } - - public static string ToDurationDisplayString(this int input) - { - return input switch - { - < 1000 => $"{input} ms", - < 1000 * 60 => $"{Math.Round(input / 1000d / 60d, 2)} s", - < 1000 * 60 * 60 => $"{Math.Round(input / 60000d / 60d, 2)} m", - _ => $"{Math.Round(input / 60000d / 60d / 60d, 2)} h" + < 1000 * 60 => $"{Math.Round(input / 1000d, 2)} s", + < 1000 * 60 * 60 => $"{ts.Minutes}m {ts.Seconds}s", + _ => $"{Math.Truncate(ts.TotalHours)}h {ts.Minutes}m" }; } + + public static string ToDurationDisplayString(this int input) => ToDurationDisplayString((long)input); } \ No newline at end of file