aboutsummaryrefslogtreecommitdiff
path: root/src/helperfuncs.cpp
diff options
context:
space:
mode:
authorGravatar Sadie Powell2025-03-01 16:24:05 +0000
committerGravatar Sadie Powell2025-03-01 16:26:53 +0000
commita27defd72f82627993a059683c8399beb477a800 (patch)
treea926b5cab8249832c7d147c508f7f098cc763ff7 /src/helperfuncs.cpp
parentReplace insp::substring_view with the C++20 iterator constructor. (diff)
parentAvoid the use of ConvToStr in string concatenation. (diff)
Merge branch 'insp4' into master.
Diffstat (limited to 'src/helperfuncs.cpp')
-rw-r--r--src/helperfuncs.cpp71
1 files changed, 50 insertions, 21 deletions
diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp
index a4524c1e3..48feb9b75 100644
--- a/src/helperfuncs.cpp
+++ b/src/helperfuncs.cpp
@@ -4,7 +4,7 @@
* Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
* Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
* Copyright (C) 2012-2013 Attila Molnar <attilamolnar@hush.com>
- * Copyright (C) 2012, 2014, 2017-2018, 2020-2024 Sadie Powell <sadie@witchery.services>
+ * Copyright (C) 2012, 2014, 2017-2018, 2020-2025 Sadie Powell <sadie@witchery.services>
* Copyright (C) 2012 Robby <robby@chatbelgie.be>
* Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
* Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
@@ -357,6 +357,21 @@ bool InspIRCd::IsSID(const std::string_view& str)
((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
}
+namespace
+{
+ constexpr const auto SECONDS_PER_MINUTE = 60;
+
+ constexpr const auto SECONDS_PER_HOUR = SECONDS_PER_MINUTE * 60;
+
+ constexpr const auto SECONDS_PER_DAY = SECONDS_PER_HOUR * 24;
+
+ constexpr const auto SECONDS_PER_WEEK = SECONDS_PER_DAY * 7;
+
+ constexpr const auto SECONDS_PER_YEAR = (SECONDS_PER_DAY * 365);
+
+ constexpr const auto SECONDS_PER_AVG_YEAR = SECONDS_PER_YEAR + (SECONDS_PER_HOUR * 6);
+}
+
/** A lookup table of values for multiplier characters used by
* Duration::{Try,}From(). In this lookup table, the indexes for
* the ascii values 'm' and 'M' have the value '60', the indexes
@@ -368,10 +383,10 @@ static constexpr unsigned int duration_multi[] =
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 86400, 0, 0, 0, 3600, 0, 0, 0, 0, 60, 0, 0,
- 0, 0, 0, 1, 0, 0, 0, 604800, 0, 31557600, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 86400, 0, 0, 0, 3600, 0, 0, 0, 0, 60, 0, 0,
- 0, 0, 0, 1, 0, 0, 0, 604800, 0, 31557600, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, SECONDS_PER_DAY, 0, 0, 0, SECONDS_PER_HOUR, 0, 0, 0, 0, SECONDS_PER_MINUTE, 0, 0,
+ 0, 0, 0, 1, 0, 0, 0, SECONDS_PER_WEEK, 0, SECONDS_PER_AVG_YEAR, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, SECONDS_PER_DAY, 0, 0, 0, SECONDS_PER_HOUR, 0, 0, 0, 0, SECONDS_PER_MINUTE, 0, 0,
+ 0, 0, 0, 1, 0, 0, 0, SECONDS_PER_WEEK, 0, SECONDS_PER_AVG_YEAR, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -382,7 +397,7 @@ static constexpr unsigned int duration_multi[] =
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
-bool Duration::TryFrom(const std::string& str, unsigned long& duration)
+bool Duration::TryFrom(const std::string& str, unsigned long& duration, time_t base)
{
unsigned long total = 0;
unsigned long subtotal = 0;
@@ -416,10 +431,10 @@ bool Duration::TryFrom(const std::string& str, unsigned long& duration)
return true;
}
-unsigned long Duration::From(const std::string& str)
+unsigned long Duration::From(const std::string& str, time_t base)
{
unsigned long out = 0;
- Duration::TryFrom(str, out);
+ Duration::TryFrom(str, out, base);
return out;
}
@@ -443,29 +458,43 @@ std::string Duration::ToString(unsigned long duration)
std::string ret;
- unsigned long years = duration / 31449600;
+ const auto years = (duration / SECONDS_PER_YEAR);
if (years)
- ret += ConvToStr(years) + "y";
+ {
+ ret = FMT::format("{}y", years);
+ duration -= (years * SECONDS_PER_YEAR);
+ }
- unsigned long weeks = (duration / 604800) % 52;
+ const auto weeks = (duration / SECONDS_PER_WEEK);
if (weeks)
- ret += ConvToStr(weeks) + "w";
+ {
+ ret += FMT::format("{}w", weeks);
+ duration -= (weeks * SECONDS_PER_WEEK);
+ }
- unsigned long days = (duration / 86400) % 7;
+ const auto days = (duration / SECONDS_PER_DAY);
if (days)
- ret += ConvToStr(days) + "d";
+ {
+ ret += FMT::format("{}d", days);
+ duration -= (days * SECONDS_PER_DAY);
+ }
- unsigned long hours = (duration / 3600) % 24;
+ const auto hours = (duration / SECONDS_PER_HOUR);
if (hours)
- ret += ConvToStr(hours) + "h";
+ {
+ ret += FMT::format("{}h", hours);
+ duration -= (hours * SECONDS_PER_HOUR);
+ }
- unsigned long minutes = (duration / 60) % 60;
+ const auto minutes = (duration / SECONDS_PER_MINUTE);
if (minutes)
- ret += ConvToStr(minutes) + "m";
+ {
+ ret += FMT::format("{}m", minutes);
+ duration -= (minutes * SECONDS_PER_MINUTE);
+ }
- unsigned long seconds = duration % 60;
- if (seconds)
- ret += ConvToStr(seconds) + "s";
+ if (duration)
+ ret += FMT::format("{}s", duration);
return ret;
}