/npk/cli/helper_timetostring.hpp
http://npk.googlecode.com/ · C++ Header · 36 lines · 32 code · 3 blank · 1 comment · 4 complexity · 8223fd913f87fee2ccc3566da5255660 MD5 · raw file
- // http://mwultong.blogspot.com/2006/12/c-time-to-string-function-vc.html
- char* timeToString(const time_t& tt) {
- static char s[20];
- #ifdef NPACK_PLATFORM_WINDOWS
- struct tm t;
- if( localtime_s(&t, &tt) == 0 )
- {
- sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d",
- t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
- t.tm_hour, t.tm_min, t.tm_sec
- );
- }
- else
- {
- sprintf(s, "can't convert time." );
- }
- #else
- struct tm* t;
- t = localtime( &tt );
- if( t != NULL )
- {
- sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d",
- t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
- t->tm_hour, t->tm_min, t->tm_sec
- );
- }
- else
- {
- sprintf(s, "can't convert time." );
- }
- #endif
- return s;
- }