From 4239a31c5928981b1582a7762458b080b8fda5ff Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Wed, 1 May 2024 16:42:35 +0500 Subject: feat: method for converting a string to time point --- src/schemas/channel.hpp | 27 +++------------------------ src/schemas/user.hpp | 28 ++++------------------------ 2 files changed, 7 insertions(+), 48 deletions(-) (limited to 'src/schemas') 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 -#include -#include #include #include -#include -#include #include #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(); this->alias_name = row[2].as(); - std::tm tm = {}; - std::stringstream ss(row[3].as()); - - 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()); if (!row[4].is_null()) { - tm = {}; - ss = std::stringstream(row[4].as()); - - 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()); } } 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 -#include -#include #include #include -#include -#include #include +#include "../utils/chrono.hpp" + namespace bot::schemas { class User { public: @@ -17,30 +15,12 @@ namespace bot::schemas { this->alias_id = row[1].as(); this->alias_name = row[2].as(); - std::tm tm = {}; - std::stringstream ss(row[3].as()); - - 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()); if (!row[4].is_null()) { - tm = {}; - ss = std::stringstream(row[4].as()); - - 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()); } } -- cgit v1.2.3