VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Durations.cpp
Go to the documentation of this file.
3#include <charconv>
4#include <chrono>
5#include <format>
6
7namespace VoltMod
8{
9
11{
12 auto now = std::chrono::system_clock::now();
13 return std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
14}
15
17{
18 return ::VoltMod::ParseDuration(duration);
19}
20
22{
23 if (seconds == 0)
24 {
25 return "Permanent";
26 }
27
28 if (seconds % SecondsPerWeek == 0 && seconds >= SecondsPerWeek)
29 {
30 int64_t w = seconds / SecondsPerWeek;
31 return std::format("{} week{}", w, w > 1 ? "s" : "");
32 }
33 if (seconds % SecondsPerDay == 0 && seconds >= SecondsPerDay)
34 {
35 int64_t d = seconds / SecondsPerDay;
36 return std::format("{} day{}", d, d > 1 ? "s" : "");
37 }
38 if (seconds % SecondsPerHour == 0 && seconds >= SecondsPerHour)
39 {
40 int64_t h = seconds / SecondsPerHour;
41 return std::format("{} hour{}", h, h > 1 ? "s" : "");
42 }
43 if (seconds % SecondsPerMinute == 0 && seconds >= SecondsPerMinute)
44 {
45 int64_t m = seconds / SecondsPerMinute;
46 return std::format("{} minute{}", m, m > 1 ? "s" : "");
47 }
48 return std::format("{} second{}", seconds, seconds > 1 ? "s" : "");
49}
50
52{
53 if (seconds <= 0)
54 {
55 return units.Permanent;
56 }
57
58 struct Unit
59 {
60 int64_t Divisor;
61 const std::string* Label;
62 };
63 const Unit unitTable[] = {
64 {SecondsPerDay, &units.Days},
65 {SecondsPerHour, &units.Hours},
66 {SecondsPerMinute, &units.Minutes},
67 };
68 for (const auto& unit : unitTable)
69 {
70 if (seconds % unit.Divisor == 0)
71 {
72 return std::format("{} {}", seconds / unit.Divisor, *unit.Label);
73 }
74 }
75 return std::format("{} {}", seconds, units.Seconds);
76}
77
79 std::string_view expiresInPrefix)
80{
81 if (expiresAt <= 0)
82 {
83 return std::string(permanentText);
84 }
85
87 if (remaining <= 0)
88 {
89 return std::string(permanentText);
90 }
91
92 return std::format("{} {}", expiresInPrefix, FormatDuration(remaining));
93}
94
96{
97 if (timestamp == 0)
98 {
99 return "Never";
100 }
101
102 std::time_t time = static_cast<std::time_t>(timestamp);
103 std::tm tm;
104#ifdef _WIN32
105 localtime_s(&tm, &time);
106#else
107 localtime_r(&time, &tm);
108#endif
109 return std::format("{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
110 tm.tm_hour, tm.tm_min, tm.tm_sec);
111}
112
114{
115 if (expiresAt == 0)
116 {
117 return false;
118 }
119 return Now() >= expiresAt;
120}
121
123{
124 if (durationSeconds == 0)
125 {
126 return 0;
127 }
128 return Now() + durationSeconds;
129}
130
131} // namespace VoltMod
static std::string FormatDurationLabel(int64_t seconds, const DurationUnitLabels &units)
Definition Durations.cpp:51
static int64_t ParseDuration(const std::string &duration)
Definition Durations.cpp:16
static bool IsExpired(int64_t expiresAt)
static std::string FormatDuration(int64_t seconds)
Definition Durations.cpp:21
static std::string FormatExpiry(int64_t expiresAt, int64_t nowSec, std::string_view permanentText, std::string_view expiresInPrefix)
Definition Durations.cpp:78
static int64_t GetExpirationTime(int64_t durationSeconds)
static std::string FormatTimestamp(int64_t timestamp)
Definition Durations.cpp:95
static int64_t Now()
Definition Durations.cpp:10
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56