Last active
July 1, 2021 09:15
-
-
Save mr5z/74ed63ea1d4f0b65a56c9baa755f636d to your computer and use it in GitHub Desktop.
Various ways to print date time
This file contains 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
static std::string date_format(boost::posix_time::ptime const& datetime) { | |
using namespace boost; | |
std::ostringstream ss; | |
auto output_facet = new boost::posix_time::time_facet(); | |
ss.imbue(std::locale(std::locale::classic(), output_facet)); | |
output_facet->format("%Y-%m-%d %H:%M:%s%Q"); | |
ss.str(""); | |
ss << datetime; | |
return ss.str(); | |
} | |
static std::string date_format2() { | |
namespace bg = boost::gregorian; | |
static char const* const fmt = "%Y-%m-%d %H:%M:%S%Q"; | |
std::ostringstream ss; | |
// assumes std::cout's locale has been set appropriately for the entire app | |
ss.imbue(std::locale(std::cout.getloc(), new bg::date_facet(fmt))); | |
ss << bg::day_clock::universal_day(); | |
return ss.str(); | |
} | |
static std::string date_format3() { | |
auto now = std::chrono::system_clock::now(); | |
auto in_time_t = std::chrono::system_clock::to_time_t(now); | |
std::stringstream ss; | |
ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %H:%M:%S%.000%Ez"); | |
return ss.str(); | |
} | |
static boost::posix_time::time_duration get_utc_offset() { | |
using namespace boost::posix_time; | |
// boost::date_time::c_local_adjustor uses the C-API to adjust a | |
// moment given in utc to the same moment in the local time zone. | |
typedef boost::date_time::c_local_adjustor<ptime> local_adj; | |
const ptime utc_now = second_clock::universal_time(); | |
const ptime now = local_adj::utc_to_local(utc_now); | |
return now - utc_now; | |
} | |
static std::string get_utc_offset_string() { | |
std::stringstream out; | |
using namespace boost::posix_time; | |
auto tf = new time_facet(); | |
tf->time_duration_format("%+%H:%M"); | |
out.imbue(std::locale(out.getloc(), tf)); | |
out << get_utc_offset(); | |
return out.str(); | |
} | |
static std::string date_format4() { | |
auto now = std::chrono::system_clock::now(); | |
auto in_time_t = std::chrono::system_clock::to_time_t(now); | |
std::stringstream ss; | |
ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %H:%M:%S%.000") << get_utc_offset_string(); | |
return ss.str(); | |
} | |
static std::string date_format5() { | |
char output[200]; | |
const char* fmt = "%Y-%m-%d %H:%M:%S%.000"; | |
auto t = time(nullptr); | |
auto tmp = gmtime(&t); | |
if (tmp == nullptr) { | |
throw std::logic_error("gmtime error"); | |
} | |
if (strftime(output, sizeof(output), fmt, tmp) == 0) { | |
throw std::logic_error("strftime returned 0"); | |
} | |
return std::string(output) + get_utc_offset_string(); //"+00:00"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment