Browse Source

Split format_tm out of format_timestamp

Luke Dashjr 12 years ago
parent
commit
1f436a5847
2 changed files with 19 additions and 12 deletions
  1. 18 12
      util.c
  2. 1 0
      util.h

+ 18 - 12
util.c

@@ -1239,13 +1239,9 @@ void bfg_init_time()
 #endif
 }
 
-int format_timestamp(char * const buf, const int fmt, const struct timeval * const tv)
+int format_tm(char * const buf, const int fmt, const struct tm * const tm, const long usecs)
 {
-	struct tm tm;
 	char *s = buf;
-	time_t tt = tv->tv_sec;
-	
-	localtime_r(&tt, &tm);
 	
 	if (fmt & BTF_BRACKETS)
 		(s++)[0] = '[';
@@ -1253,9 +1249,9 @@ int format_timestamp(char * const buf, const int fmt, const struct timeval * con
 	{
 		s +=
 		sprintf(s, "%d-%02d-%02d",
-		        tm.tm_year + 1900,
-		        tm.tm_mon + 1,
-		        tm.tm_mday);
+		        tm->tm_year + 1900,
+		        tm->tm_mon + 1,
+		        tm->tm_mday);
 		if (fmt & BTF_TIME)
 			(s++)[0] = ' ';
 	}
@@ -1263,12 +1259,12 @@ int format_timestamp(char * const buf, const int fmt, const struct timeval * con
 	{
 		s +=
 		sprintf(s, "%02d:%02d:%02d",
-		        tm.tm_hour,
-		        tm.tm_min,
-		        tm.tm_sec);
+		        tm->tm_hour,
+		        tm->tm_min,
+		        tm->tm_sec);
 		if (fmt & BTF_USEC)
 			s +=
-			sprintf(s, ".%06ld", (long)tv->tv_usec);
+			sprintf(s, ".%06ld", usecs);
 	}
 	if (fmt & BTF_BRACKETS)
 		s +=
@@ -1277,6 +1273,16 @@ int format_timestamp(char * const buf, const int fmt, const struct timeval * con
 	return (s - buf);
 }
 
+int format_timestamp(char * const buf, const int fmt, const struct timeval * const tv)
+{
+	struct tm tm;
+	time_t tt = tv->tv_sec;
+	
+	localtime_r(&tt, &tm);
+	
+	return format_tm(buf, fmt, &tm, tv->tv_usec);
+}
+
 void subtime(struct timeval *a, struct timeval *b)
 {
 	timersub(a, b, b);

+ 1 - 0
util.h

@@ -307,6 +307,7 @@ enum timestamp_format {
 	BTF_BRACKETS = 8,
 };
 
+extern int format_tm(char * const buf, const int fmt, const struct tm * const, const long usecs);
 extern int format_timestamp(char * const buf, const int fmt, const struct timeval * const);
 #define format_time_t(buf, fmt, tt)  format_timestamp(buf, fmt, (struct timeval[1]){ { .tv_sec = tt } })
 #define get_datestamp(buf, tt)  format_time_t(buf, BTF_DATE | BTF_TIME | BTF_BRACKETS, tt)