// DragonX Wallet - ImGui Edition // Copyright 2024-2026 The Hush Developers // Released under the GPLv3 #include "text_format.h" #include "i18n.h" #include #include #include namespace dragonx { namespace util { namespace { std::int64_t secondsAgo(std::int64_t timestamp) { std::int64_t now = static_cast(std::time(nullptr)); std::int64_t diff = now - timestamp; return diff < 0 ? 0 : diff; } } // namespace std::string formatTimeAgo(std::int64_t timestamp) { if (timestamp <= 0) return {}; const std::int64_t diff = secondsAgo(timestamp); char buf[48]; if (diff < 60) std::snprintf(buf, sizeof(buf), tr("time_seconds_ago"), static_cast(diff)); else if (diff < 3600) std::snprintf(buf, sizeof(buf), tr("time_minutes_ago"), static_cast(diff / 60)); else if (diff < 86400) std::snprintf(buf, sizeof(buf), tr("time_hours_ago"), static_cast(diff / 3600)); else std::snprintf(buf, sizeof(buf), tr("time_days_ago"), static_cast(diff / 86400)); return buf; } std::string formatTimeAgoShort(std::int64_t timestamp) { if (timestamp <= 0) return {}; const std::int64_t diff = secondsAgo(timestamp); char buf[32]; if (diff < 60) std::snprintf(buf, sizeof(buf), "%llds ago", static_cast(diff)); else if (diff < 3600) std::snprintf(buf, sizeof(buf), "%lldm ago", static_cast(diff / 60)); else if (diff < 86400) std::snprintf(buf, sizeof(buf), "%lldh ago", static_cast(diff / 3600)); else std::snprintf(buf, sizeof(buf), "%lldd ago", static_cast(diff / 86400)); return buf; } std::string truncateMiddle(const std::string& s, int maxLen) { // Guard maxLen <= 3 before the unsigned (maxLen - 3): otherwise it wraps and the // trailing substr(length - half) throws std::out_of_range on short inputs. if (maxLen <= 3 || s.length() <= static_cast(maxLen)) return s; int half = (maxLen - 3) / 2; return s.substr(0, half) + "..." + s.substr(s.length() - half); } std::string truncateMiddle(const std::string& s, int front, int back) { if (s.length() <= static_cast(front + back + 3)) return s; return s.substr(0, front) + "..." + s.substr(s.length() - back); } bool containsIgnoreCase(std::string_view haystack, std::string_view needle) { if (needle.empty()) return true; if (needle.size() > haystack.size()) return false; const auto low = [](char c) { return static_cast(std::tolower(static_cast(c))); }; const std::size_t last = haystack.size() - needle.size(); for (std::size_t i = 0; i <= last; ++i) { std::size_t j = 0; for (; j < needle.size(); ++j) { if (low(haystack[i + j]) != low(needle[j])) break; } if (j == needle.size()) return true; } return false; } } // namespace util } // namespace dragonx