std::string current_date_time()
{
std::ostringstream ss;
ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S")));
ss << boost::posix_time::second_clock::local_time();
return ss.str();
}
time_t date_time_to_time_t(const std::string &date_time)
{
boost::posix_time::ptime time;
std::istringstream is(date_time);
is.imbue(std::locale(is.getloc(), new boost::posix_time::time_input_facet("%Y-%m-%d %H:%M:%S")));
is >> time;
struct tm st = boost::posix_time::to_tm(time);
return mktime(&st);
}
time_t asn1_utc_time_to_time_t(const ASN1_UTCTIME *asn1_utc_time)
{
return date_time_to_time_t(asn1_utc_time_to_date_time(asn1_utc_time));
}
std::string asn1_utc_time_to_date_time(const ASN1_UTCTIME *asn1_utc_time)
{
if (nullptr == asn1_utc_time)
//exception here
return std::string();
struct tm st = {0};
ASN1_TIME_to_tm(asn1_utc_time, &st);
boost::posix_time::ptime ptime = boost::posix_time::ptime_from_tm(st);
std::ostringstream ss;
ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S")));
ss << boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(ptime);
return ss.str();
}
std::string time_t_to_date_time(const time_t &time)
{
boost::posix_time::ptime ptime = boost::posix_time::from_time_t(time);
std::ostringstream ss;
ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S")));
ss << boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(ptime);
return ss.str();
}