/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

  1. // http://mwultong.blogspot.com/2006/12/c-time-to-string-function-vc.html
  2. char* timeToString(const time_t& tt) {
  3. static char s[20];
  4. #ifdef NPACK_PLATFORM_WINDOWS
  5. struct tm t;
  6. if( localtime_s(&t, &tt) == 0 )
  7. {
  8. sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d",
  9. t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
  10. t.tm_hour, t.tm_min, t.tm_sec
  11. );
  12. }
  13. else
  14. {
  15. sprintf(s, "can't convert time." );
  16. }
  17. #else
  18. struct tm* t;
  19. t = localtime( &tt );
  20. if( t != NULL )
  21. {
  22. sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d",
  23. t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
  24. t->tm_hour, t->tm_min, t->tm_sec
  25. );
  26. }
  27. else
  28. {
  29. sprintf(s, "can't convert time." );
  30. }
  31. #endif
  32. return s;
  33. }