Created
October 17, 2020 16:01
-
-
Save ifknot/1458e16f0c526efad18a30f996159e1a to your computer and use it in GitHub Desktop.
convert int or string to dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "as_dates.h" | |
#include <iomanip> | |
#include <sstream> | |
#include <stdexcept> | |
namespace R { | |
variant_vector as_dates(const variant_vector& dates, std::string format) { | |
variant_vector tm_dates; | |
for (const auto& date : dates) { | |
r_string d; | |
switch (date.index()) { | |
case _str: | |
d = std::get<_str>(date); | |
break; | |
case _int: | |
d = std::to_string(std::get<_int>(date)); | |
break; | |
default: | |
throw std::invalid_argument(std::string(__func__) + " invalid argument " + index_to_string[date.index()]); | |
} | |
std::istringstream ss(d); | |
std:tm tm; | |
std::time_t t = std::time(nullptr); | |
tm = *std::localtime(&t); // ensure no tm elements are undefined, or mktime will fail | |
ss >> std::get_time(&tm, format.c_str()); | |
r_date tm_date; | |
tm_date.format = format; | |
tm_date.tm = tm; | |
tm_dates.push_back(tm_date); | |
} | |
return tm_dates; | |
} | |
variant_vector as_dates(variant_vector&& dates, std::string format) { | |
variant_vector tm_dates; | |
for (const auto& date : dates) { | |
r_string d; | |
switch (date.index()) { | |
case _str: | |
d = std::get<_str>(date); | |
break; | |
case _int: | |
d = std::to_string(std::get<_int>(date)); | |
break; | |
default: | |
throw std::invalid_argument(std::string(__func__) + " invalid argument " + index_to_string[date.index()]); | |
} | |
std::istringstream ss(d); | |
std:tm tm; | |
std::time_t t = std::time(nullptr); | |
tm = *std::localtime(&t); // ensure no tm elements are undefined, or mktime will fail | |
ss >> std::get_time(&tm, format.c_str()); | |
r_date tm_date; | |
tm_date.format = format; | |
tm_date.tm = tm; | |
tm_dates.push_back(tm_date); | |
} | |
return tm_dates; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment