cwTime.h/cpp: Added formatDateTime()

This commit is contained in:
kevin 2023-05-19 21:21:03 -04:00
parent e6412b2538
commit c93a9670f9
2 changed files with 40 additions and 0 deletions

View File

@ -41,6 +41,7 @@ void cw::time::get( spec_t& t )
#endif
#ifdef OS_LINUX
#include <sys/time.h> // gettimeofday()
void cw::time::get( spec_t& t )
{
// NOTcw::mutex::lock(h,timeout) relies on using
@ -306,6 +307,42 @@ void cw::time::microsecondsToSpec( spec_t& ts, unsigned us )
ts.tv_nsec = ns;
}
unsigned cw::time::formatDateTime( char* buffer, unsigned bufN, bool includeDateFl )
{
// from here: https://stackoverflow.com/questions/3673226/how-to-print-time-in-format-2009-08-10-181754-811
int millisec;
struct tm* tm_info;
struct timeval tv;
int n = 0;
gettimeofday(&tv, NULL);
millisec = lrint(tv.tv_usec/1000.0); // Round to nearest millisec
// Allow for rounding up to nearest second
if (millisec>=1000)
{
millisec -=1000;
tv.tv_sec++;
}
tm_info = localtime(&tv.tv_sec);
const char* fmt = "%H:%M:%S";
if( includeDateFl )
fmt = "%Y:%m:%d %H:%M:%S";
n = strftime(buffer, bufN, fmt, tm_info);
if( n < (int)bufN && bufN-n >= 5 )
n = snprintf(buffer + n, bufN-n,".%03d", millisec);
return (unsigned)n;
}
cw::rc_t cw::time::test()
{

View File

@ -78,6 +78,9 @@ namespace cw
void millisecondsToSpec( spec_t& ts, unsigned ms );
void microsecondsToSpec( spec_t& ts, unsigned us );
// Return count of bytes in in buf[]
unsigned formatDateTime( char* buf, unsigned bufN, bool includeDateFl=false );
rc_t test();
//)