// SPDX-License-Identifier: MIT #pragma once #include #include #include #include namespace openpu { /// "8.39 GB" (decimal units, as storage vendors and the official app use). inline std::string formatSize(std::uint64_t bytes) { const char* units[] = {"B", "KB", "MB", "GB", "TB"}; auto value = static_cast(bytes); std::size_t unit = 0; while (value >= 1000.0 && unit + 1 < sizeof units / sizeof units[0]) { value /= 1000.0; ++unit; } char buf[32]; (void)std::snprintf(buf, sizeof buf, unit == 0 ? "%.0f %s" : "%.2f %s", value, units[unit]); return buf; } /// "1:02:03" or "02:03". inline std::string formatDuration(std::chrono::seconds s) { const auto total = static_cast(s.count()); char buf[32]; if (total >= 3600) (void)std::snprintf(buf, sizeof buf, "%lld:%02lld:%02lld", total / 3600, (total / 60) % 60, total % 60); else (void)std::snprintf(buf, sizeof buf, "%02lld:%02lld", total / 60, total % 60); return buf; } } // namespace openpu