Changeset 2203

Show
Ignore:
Timestamp:
27/07/2008 21:09:00 (5 months ago)
Author:
chris
Message:

Add a function to format a BoxTime? as a human-readable time only
(for use in logging).

Location:
box/trunk/lib/common
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • box/trunk/lib/common/BoxTime.cpp

    r2127 r2203  
    5353        return SecondsToBoxTime(time(0)); 
    5454} 
     55 
     56std::string FormatTime(box_time_t time, bool showMicros) 
     57{ 
     58        std::ostringstream buf; 
     59 
     60        time_t seconds = BoxTimeToSeconds(time); 
     61        int micros = BoxTimeToMicroSeconds(time) % MICRO_SEC_IN_SEC; 
     62 
     63        struct tm tm_now, *tm_ptr = &tm_now; 
     64 
     65        #ifdef WIN32 
     66                if ((tm_ptr = localtime(&seconds)) != NULL) 
     67        #else 
     68                if (localtime_r(&seconds, &tm_now) != NULL) 
     69        #endif 
     70        { 
     71                buf << std::setfill('0') << 
     72                        std::setw(2) << tm_ptr->tm_hour << ":" <<  
     73                        std::setw(2) << tm_ptr->tm_min  << ":" << 
     74                        std::setw(2) << tm_ptr->tm_sec; 
     75 
     76                if (showMicros) 
     77                { 
     78                        buf << "." << std::setw(6) << micros; 
     79                } 
     80        } 
     81        else 
     82        { 
     83                buf << strerror(errno); 
     84        } 
     85 
     86        return buf.str(); 
     87} 
     88 
  • box/trunk/lib/common/BoxTime.h

    r1141 r2203  
    4141} 
    4242 
     43std::string FormatTime(box_time_t time, bool showMicros = false); 
     44 
    4345#endif // BOXTIME__H