71 std::lock_guard<std::mutex>
const tm_lock(
tm_mutex_ );
73 std::time_t now = std::time(
nullptr );
74 std::tm* ltm = std::localtime( &now );
77 throw std::runtime_error(
"Cannot get current date." );
82 out, 12,
"%u-%02u-%02u",
83 ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday
91 std::lock_guard<std::mutex>
const tm_lock(
tm_mutex_ );
93 std::time_t now = std::time(
nullptr );
94 std::tm* ltm = std::localtime( &now );
97 throw std::runtime_error(
"Cannot get current time." );
102 out, 10,
"%02u:%02u:%02u",
103 ltm->tm_hour, ltm->tm_min, ltm->tm_sec
114 if( use_local_time ) {
115 ret = std::mktime( &time );
122 ::setenv(
"TZ",
"", 1);
126 ret = std::mktime( &time );
130 ::setenv(
"TZ", tz, 1);
137 throw std::invalid_argument(
"Cannot convert std::tm object to std::time." );
142 std::vector<std::time_t>
tm_to_time( std::vector<std::tm>
const& times,
bool use_local_time )
144 return tm_to_time( times.begin(), times.end(), use_local_time, times.size() );
147 std::tm
time_to_tm( std::time_t
const& time,
bool use_local_time )
150 std::lock_guard<std::mutex>
const tm_lock(
tm_mutex_ );
153 if( use_local_time ) {
154 ret = std::localtime( &time );
156 ret = std::gmtime( &time );
158 if( ret ==
nullptr ) {
159 if( errno == EOVERFLOW ) {
160 throw std::invalid_argument(
161 "Cannot convert std::time object to std::tm, because the argument is too large."
164 throw std::invalid_argument(
"Cannot convert std::time object to std::tm." );
170 std::vector<std::tm>
time_to_tm( std::vector<std::time_t>
const& times,
bool use_local_time )
172 return time_to_tm( times.begin(), times.end(), use_local_time, times.size() );
179 std::string
tm_to_string( std::tm
const& time, std::string
const& format, std::string
const& locale )
187 auto const loc = std::locale( locale.c_str() );
188 std::ostringstream oss{};
192 std::time_put<char>
const& tmput = std::use_facet<std::time_put<char>>( loc );
193 tmput.put( oss, oss,
' ', &time, &format[0], &format[0] + format.size() );
203 std::string
tm_to_string( std::tm
const& time, std::string
const& format )
228 static const std::array<std::string, 3>
locales_ = {{
"C",
"en_US.UTF-8",
"" }};
231 static const std::array<std::string, 9>
formats_ = {{
232 "%Y-%m-%d",
"%Y%m%d",
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%d %H:%M:%S",
"%Y%m%dT%H%M%S",
233 "%Y%m%d %H%M%S",
"%Y%m%d%H%M%S",
"%H:%M:%S",
"%H%M%S"
243 std::string
const& str, std::string
const& format, std::string
const& locale, std::tm& t
251 #if !( defined(__GNUC__) && (__GNUC__ < 5) && !defined(__clang__) && !defined(__INTEL_COMPILER) )
266 auto loc = std::locale( locale.c_str() );
267 std::istringstream iss(
trim( str ));
272 std::time_get<char>
const& tmget = std::use_facet<std::time_get<char>>( loc );
273 std::ios::iostate state = std::ios_base::goodbit;
277 iss, std::time_get<char>::iter_type(), iss, state, &t, &format[0], &format[0] + format.size()
285 return ! iss.fail() && state == std::ios_base::eofbit;
294 throw std::runtime_error(
295 "You compiled with " + info_compiler_family() +
" " + info_compiler_version() +
296 ", which does not support time conversion functions std::get_time and std::time_get::get. " +
297 "Please upgrade to a newer compiler."
314 std::tm
convert_to_tm( std::string
const& str, std::string
const& format, std::string
const& locale )
318 throw std::invalid_argument(
319 "Cannot convert string '" + str +
"' to tm date/time object."
330 for(
auto const& locale :
locales_ ) {
336 throw std::invalid_argument(
337 "Cannot convert string '" + str +
"' to tm date/time object with given format."
350 for(
auto const& format :
formats_ ) {
356 throw std::invalid_argument(
357 "Cannot convert string '" + str +
"' to tm date/time object with guessed formats."
373 for(
auto const& locale :
locales_ ) {
386 for(
auto const& format :
formats_ ) {