CS2-Kit
C++23 library for CS2 Metamod:Source plugin development
Loading...
Searching...
No Matches
TimeUtils.cpp
Go to the documentation of this file.
1#include <CS2Kit/Utils/TimeUtils.hpp>
2#include <algorithm>
3#include <cctype>
4#include <chrono>
5#include <format>
6#include <regex>
7
8namespace CS2Kit::Utils
9{
10
11int64_t TimeUtils::Now()
12{
13 auto now = std::chrono::system_clock::now();
14 return std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
15}
16
17int64_t TimeUtils::ParseDuration(const std::string& duration)
18{
19 std::string lower = duration;
20 std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
21
22 if (lower.empty() || lower == "0" || lower == "perm" || lower == "permanent")
23 return 0;
24
25 std::regex pattern(R"((\d+)([smhdw]))");
26 std::smatch matches;
27 if (std::regex_match(lower, matches, pattern))
28 {
29 try
30 {
31 int64_t value = std::stoll(matches[1].str());
32 switch (matches[2].str()[0])
33 {
34 case 's':
35 return value;
36 case 'm':
37 return value * SecondsPerMinute;
38 case 'h':
39 return value * SecondsPerHour;
40 case 'd':
41 return value * SecondsPerDay;
42 case 'w':
43 return value * SecondsPerWeek;
44 default:
45 return 0;
46 }
47 }
48 catch (...)
49 {
50 return 0;
51 }
52 }
53
54 try
55 {
56 return std::stoll(duration);
57 }
58 catch (...)
59 {
60 return 0;
61 }
62}
63
64std::string TimeUtils::FormatDuration(int64_t seconds)
65{
66 if (seconds == 0)
67 return "Permanent";
68
69 if (seconds % SecondsPerWeek == 0 && seconds >= SecondsPerWeek)
70 {
71 int64_t w = seconds / SecondsPerWeek;
72 return std::format("{} week{}", w, w > 1 ? "s" : "");
73 }
74 if (seconds % SecondsPerDay == 0 && seconds >= SecondsPerDay)
75 {
76 int64_t d = seconds / SecondsPerDay;
77 return std::format("{} day{}", d, d > 1 ? "s" : "");
78 }
79 if (seconds % SecondsPerHour == 0 && seconds >= SecondsPerHour)
80 {
81 int64_t h = seconds / SecondsPerHour;
82 return std::format("{} hour{}", h, h > 1 ? "s" : "");
83 }
84 if (seconds % SecondsPerMinute == 0 && seconds >= SecondsPerMinute)
85 {
86 int64_t m = seconds / SecondsPerMinute;
87 return std::format("{} minute{}", m, m > 1 ? "s" : "");
88 }
89 return std::format("{} second{}", seconds, seconds > 1 ? "s" : "");
90}
91
92std::string TimeUtils::FormatTimestamp(int64_t timestamp)
93{
94 if (timestamp == 0)
95 return "Never";
96
97 std::time_t time = static_cast<std::time_t>(timestamp);
98 std::tm tm;
99#ifdef _WIN32
100 localtime_s(&tm, &time);
101#else
102 localtime_r(&time, &tm);
103#endif
104 return std::format("{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
105 tm.tm_hour, tm.tm_min, tm.tm_sec);
106}
107
108bool TimeUtils::IsExpired(int64_t expiresAt)
109{
110 if (expiresAt == 0)
111 return false;
112 return Now() >= expiresAt;
113}
114
115int64_t TimeUtils::GetExpirationTime(int64_t durationSeconds)
116{
117 if (durationSeconds == 0)
118 return 0;
119 return Now() + durationSeconds;
120}
121
122} // namespace CS2Kit::Utils