Skip to content

Instantly share code, notes, and snippets.

@kolodziej
Created August 26, 2014 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kolodziej/fb4f75e12fb76d9168fa to your computer and use it in GitHub Desktop.
Save kolodziej/fb4f75e12fb76d9168fa to your computer and use it in GitHub Desktop.
#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