1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ 2 3 #include "System/TimeUtil.h" 4 5 #include "System/Util.h" 6 #include "System/Exceptions.h" 7 8 #include <string> 9 #include <ctime> 10 GetCurrentTimeStr()11std::string CTimeUtil::GetCurrentTimeStr() 12 { 13 struct tm* lt; 14 // Get time as long integer 15 __time64_t long_time = GetCurrentTime(); 16 // Convert to local time 17 lt = _localtime64(&long_time); 18 19 // Don't see how this can happen (according to docs _localtime64 only returns 20 // NULL if long_time is before 1/1/1970...) but a user's stacktrace indicated 21 // NULL newtime in the snprintf line... 22 if (lt == NULL) { 23 throw content_error("error: _localtime64 returned NULL"); 24 } 25 26 const size_t str_maxSize = 512; 27 char str[str_maxSize]; 28 SNPRINTF(str, str_maxSize, "%04i%02i%02i_%02i%02i%02i", 29 lt->tm_year + 1900, 30 lt->tm_mon + 1, 31 lt->tm_mday, 32 lt->tm_hour, 33 lt->tm_min, 34 lt->tm_sec); 35 36 return str; 37 } 38