C++ Utils  0.1
utils/timeutils.h
Go to the documentation of this file.
00001 
00037 #ifndef UTILS_TIMEUTILS_H_
00038 #define UTILS_TIMEUTILS_H_
00039 
00040 #include <ctime>
00041 #include <string>
00042 
00046 namespace utils
00047 {
00048 
00052 class TimeUtils
00053 {
00054 public:
00062         static std::string timeAsString(const std::string& formatString, time_t time)
00063         {
00064                 const struct tm *timeinfo = localtime(&time);
00065 
00066                 std::string buffer;
00067                 buffer.resize(formatString.size()*2);
00068                 size_t len = strftime(&buffer[0], buffer.size(), formatString.c_str(), timeinfo);
00069                 while (len == 0) {
00070                         buffer.resize(buffer.size()*2);
00071                         len = strftime(&buffer[0], buffer.size(), formatString.c_str(), timeinfo);
00072                 }
00073                 buffer.resize(len);
00074                 return buffer;
00075         }
00076 
00082         static std::string timeAsString(const std::string& formatString)
00083         {
00084                 return timeAsString(formatString, time(0L));
00085         }
00086 };
00087 
00088 }
00089 
00090 #endif /* UTILS_TIMEUTILS_H_ */
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines