[Box Backup-commit] COMMIT r2203 - box/trunk/lib/common

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Sun, 27 Jul 2008 21:09:01 +0100 (BST)


Author: chris
Date: 2008-07-27 21:09:00 +0100 (Sun, 27 Jul 2008)
New Revision: 2203

Modified:
   box/trunk/lib/common/BoxTime.cpp
   box/trunk/lib/common/BoxTime.h
Log:
Add a function to format a BoxTime as a human-readable time only
(for use in logging).


Modified: box/trunk/lib/common/BoxTime.cpp
===================================================================
--- box/trunk/lib/common/BoxTime.cpp	2008-07-26 15:03:27 UTC (rev 2202)
+++ box/trunk/lib/common/BoxTime.cpp	2008-07-27 20:09:00 UTC (rev 2203)
@@ -52,3 +52,37 @@
 	
 	return SecondsToBoxTime(time(0));
 }
+
+std::string FormatTime(box_time_t time, bool showMicros)
+{
+	std::ostringstream buf;
+
+	time_t seconds = BoxTimeToSeconds(time);
+	int micros = BoxTimeToMicroSeconds(time) % MICRO_SEC_IN_SEC;
+
+	struct tm tm_now, *tm_ptr = &tm_now;
+
+	#ifdef WIN32
+		if ((tm_ptr = localtime(&seconds)) != NULL)
+	#else
+		if (localtime_r(&seconds, &tm_now) != NULL)
+	#endif
+	{
+		buf << std::setfill('0') <<
+			std::setw(2) << tm_ptr->tm_hour << ":" << 
+			std::setw(2) << tm_ptr->tm_min  << ":" <<
+			std::setw(2) << tm_ptr->tm_sec;
+
+		if (showMicros)
+		{
+			buf << "." << std::setw(6) << micros;
+		}
+	}
+	else
+	{
+		buf << strerror(errno);
+	}
+
+	return buf.str();
+}
+

Modified: box/trunk/lib/common/BoxTime.h
===================================================================
--- box/trunk/lib/common/BoxTime.h	2008-07-26 15:03:27 UTC (rev 2202)
+++ box/trunk/lib/common/BoxTime.h	2008-07-27 20:09:00 UTC (rev 2203)
@@ -40,4 +40,6 @@
 	return Time;
 }
 
+std::string FormatTime(box_time_t time, bool showMicros = false);
+
 #endif // BOXTIME__H