[Quest API] Add Time String to Seconds Method to Perl/Lua. (#2580)

* [Quest API] Add Time String to Seconds Method to Perl/Lua.

# Perl
- Add `quest::timetoseconds(time_string)`.

# Lua
- Add `eq.time_to_seconds(time_string)`.

# Notes
- Allows operators to use this method in place of hardcoded values like `3600`.

* Remove unused method.
This commit is contained in:
Alex King
2022-11-26 19:28:21 -05:00
committed by GitHub
parent ea9a02bec4
commit f7ae5850f0
8 changed files with 53 additions and 42 deletions
+38
View File
@@ -678,3 +678,41 @@ bool Strings::Contains(const std::string& subject, const std::string& search)
{
return subject.find(search) != std::string::npos;
}
uint32 Strings::TimeToSeconds(std::string time_string)
{
if (time_string.empty()) {
return 0;
}
uint32 duration = 0;
std::transform(time_string.begin(), time_string.end(), time_string.begin(), ::tolower);
std::string time_unit = time_string;
time_unit.erase(remove_if(time_unit.begin(), time_unit.end(), [](char c) { return !isdigit(c); }), time_unit.end());
auto unit = std::stoul(time_unit);
if (time_string.find('s') != std::string::npos) {
duration = unit;
}
if (time_string.find('m') != std::string::npos) {
duration = unit * 60;
}
if (time_string.find('h') != std::string::npos) {
duration = unit * 3600;
}
if (time_string.find('d') != std::string::npos) {
duration = unit * 86400;
}
if (time_string.find('y') != std::string::npos) {
duration = unit * 31556926;
}
return duration;
}
+1
View File
@@ -109,6 +109,7 @@ public:
static std::vector<std::string> Split(const std::string& s, const std::string& delimiter);
static std::vector<std::string> Wrap(std::vector<std::string> &src, std::string character);
static void FindReplace(std::string &string_subject, const std::string &search_string, const std::string &replace_string);
static uint32 TimeToSeconds(std::string time_string);
template<typename T>
static std::string