PageRenderTime 20ms CodeModel.GetById 11ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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