Created
August 26, 2014 14:21
-
-
Save kolodziej/fb4f75e12fb76d9168fa to your computer and use it in GitHub Desktop.
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
#ifndef TIME_UTILITY_TPL_HPP | |
#define TIME_UTILITY_TPL_HPP | |
#include <string> | |
#include <sstream> | |
#include <chrono> | |
#include <ctime> | |
template <typename Clock, int BufSize> | |
std::string format_time(std::chrono::time_point<Clock> timepoint, std::string format, tm * (*func)(const time_t*)) | |
{ | |
char buffer[BufSize]; | |
time_t timepoint_t = Clock::to_time_t(timepoint); | |
tm * tm_struct = (*func)(&timepoint_t); | |
strftime(buffer, BufSize, format.data(), tm_struct); | |
return std::string(buffer); | |
} | |
template <typename Clock, int BufSize> | |
std::string format_localtime(std::chrono::time_point<Clock> timepoint, std::string format) | |
{ | |
return format_time<Clock, BufSize>(timepoint, format, &localtime); | |
} | |
template <typename Clock, int BufSize> | |
std::string format_utc_time(std::chrono::time_point<Clock> timepoint, std::string format) | |
{ | |
return format_time<Clock, BufSize>(timepoint, format, &gmtime); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment