diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-01 16:42:35 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-01 16:42:35 +0500 |
| commit | 4239a31c5928981b1582a7762458b080b8fda5ff (patch) | |
| tree | f9b798dcedeac5f1751c6420b5572256a088fa33 /src | |
| parent | 0d22ee8a01e2822ed89480b0620ee082c223f0dd (diff) | |
feat: method for converting a string to time point
Diffstat (limited to 'src')
| -rw-r--r-- | src/schemas/channel.hpp | 27 | ||||
| -rw-r--r-- | src/schemas/user.hpp | 28 | ||||
| -rw-r--r-- | src/utils/chrono.cpp | 18 | ||||
| -rw-r--r-- | src/utils/chrono.hpp | 4 |
4 files changed, 29 insertions, 48 deletions
diff --git a/src/schemas/channel.hpp b/src/schemas/channel.hpp index a304149..2560331 100644 --- a/src/schemas/channel.hpp +++ b/src/schemas/channel.hpp @@ -1,15 +1,12 @@ #pragma once #include <chrono> -#include <ctime> -#include <iomanip> #include <optional> #include <pqxx/pqxx> -#include <sstream> -#include <stdexcept> #include <string> #include "../constants.hpp" +#include "../utils/chrono.hpp" namespace bot::schemas { class Channel { @@ -19,30 +16,12 @@ namespace bot::schemas { this->alias_id = row[1].as<int>(); this->alias_name = row[2].as<std::string>(); - std::tm tm = {}; - std::stringstream ss(row[3].as<std::string>()); - - ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S"); - - if (ss.fail()) { - throw std::invalid_argument("Invalid time format"); - } - this->joined_at = - std::chrono::system_clock::from_time_t(std::mktime(&tm)); + utils::chrono::string_to_time_point(row[3].as<std::string>()); if (!row[4].is_null()) { - tm = {}; - ss = std::stringstream(row[4].as<std::string>()); - - ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S"); - - if (ss.fail()) { - throw std::invalid_argument("Invalid time format"); - } - this->opted_out_at = - std::chrono::system_clock::from_time_t(std::mktime(&tm)); + utils::chrono::string_to_time_point(row[4].as<std::string>()); } } diff --git a/src/schemas/user.hpp b/src/schemas/user.hpp index 443dde9..0bd1368 100644 --- a/src/schemas/user.hpp +++ b/src/schemas/user.hpp @@ -1,14 +1,12 @@ #pragma once #include <chrono> -#include <ctime> -#include <iomanip> #include <optional> #include <pqxx/pqxx> -#include <sstream> -#include <stdexcept> #include <string> +#include "../utils/chrono.hpp" + namespace bot::schemas { class User { public: @@ -17,30 +15,12 @@ namespace bot::schemas { this->alias_id = row[1].as<int>(); this->alias_name = row[2].as<std::string>(); - std::tm tm = {}; - std::stringstream ss(row[3].as<std::string>()); - - ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S"); - - if (ss.fail()) { - throw std::invalid_argument("Invalid time format"); - } - this->joined_at = - std::chrono::system_clock::from_time_t(std::mktime(&tm)); + utils::chrono::string_to_time_point(row[3].as<std::string>()); if (!row[4].is_null()) { - tm = {}; - ss = std::stringstream(row[4].as<std::string>()); - - ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S"); - - if (ss.fail()) { - throw std::invalid_argument("Invalid time format"); - } - this->opted_out_at = - std::chrono::system_clock::from_time_t(std::mktime(&tm)); + utils::chrono::string_to_time_point(row[4].as<std::string>()); } } diff --git a/src/utils/chrono.cpp b/src/utils/chrono.cpp index 64ad32a..7a7f2c9 100644 --- a/src/utils/chrono.cpp +++ b/src/utils/chrono.cpp @@ -1,6 +1,10 @@ #include "chrono.hpp" +#include <chrono> #include <cmath> +#include <ctime> +#include <iomanip> +#include <sstream> #include <string> namespace bot::utils::chrono { @@ -27,4 +31,18 @@ namespace bot::utils::chrono { return std::to_string(d) + "d" + std::to_string(h) + "h"; } } + + std::chrono::system_clock::time_point string_to_time_point( + const std::string &value, const std::string &format) { + std::tm tm = {}; + std::stringstream ss(value); + + ss >> std::get_time(&tm, format.c_str()); + + if (ss.fail()) { + throw std::invalid_argument("Invalid time format"); + } + + return std::chrono::system_clock::from_time_t(std::mktime(&tm)); + } } diff --git a/src/utils/chrono.hpp b/src/utils/chrono.hpp index 5d0b3ed..7e85e70 100644 --- a/src/utils/chrono.hpp +++ b/src/utils/chrono.hpp @@ -1,7 +1,11 @@ #pragma once +#include <chrono> #include <string> namespace bot::utils::chrono { std::string format_timestamp(int seconds); + std::chrono::system_clock::time_point string_to_time_point( + const std::string &value, + const std::string &format = "%Y-%m-%d %H:%M:%S"); } |
