Browse Source

Abstract runtime counter into new format_elapsed function

Luke Dashjr 12 years ago
parent
commit
6ec038fb53
3 changed files with 26 additions and 17 deletions
  1. 4 17
      miner.c
  2. 21 0
      util.c
  3. 1 0
      util.h

+ 4 - 17
miner.c

@@ -2869,7 +2869,7 @@ static int menu_attr = A_REVERSE;
 static void curses_print_status(void)
 {
 	struct pool *pool = currentpool;
-	struct timeval now, tv;
+	struct timeval now;
 	float efficiency;
 	double utility;
 	int logdiv;
@@ -2880,22 +2880,9 @@ static void curses_print_status(void)
 	mvwprintw(statuswin, 0, 0, " " PACKAGE " version " VERSION " - Started: %s", datestamp);
 	timer_set_now(&now);
 	{
-		unsigned int days, hours;
-		div_t d;
-		
-		timersub(&now, &miner_started, &tv);
-		d = div(tv.tv_sec, 86400);
-		days = d.quot;
-		d = div(d.rem, 3600);
-		hours = d.quot;
-		d = div(d.rem, 60);
-		wprintw(statuswin, " - [%3u day%c %02d:%02d:%02d]"
-			, days
-			, (days == 1) ? ' ' : 's'
-			, hours
-			, d.quot
-			, d.rem
-		);
+		char timerstr[20];
+		format_elapsed(timerstr, BTF_BRACKETS | BTF_DATE | BTF_TIME, timer_elapsed(&miner_started, &now));
+		wprintw(statuswin, " - %s", timerstr);
 	}
 	wattroff(statuswin, A_BOLD);
 	wmove(statuswin, 5, 1);

+ 21 - 0
util.c

@@ -1239,6 +1239,27 @@ void bfg_init_time()
 #endif
 }
 
+// NOTE: Only supports (BTF_BRACKETS | BTF_DATE | BTF_TIME) for now
+int format_elapsed(char * const buf, const int fmt, int elapsed)
+{
+	unsigned int days, hours;
+	div_t d;
+	
+	d = div(elapsed, 86400);
+	days = d.quot;
+	d = div(d.rem, 3600);
+	hours = d.quot;
+	d = div(d.rem, 60);
+	return
+	sprintf(buf, "[%3u day%c %02d:%02d:%02d]"
+		, days
+		, (days == 1) ? ' ' : 's'
+		, hours
+		, d.quot
+		, d.rem
+	);
+}
+
 int format_tm(char * const buf, const int fmt, const struct tm * const tm, const long usecs)
 {
 	char *s = buf;

+ 1 - 0
util.h

@@ -308,6 +308,7 @@ enum timestamp_format {
 	BTF_BRACKETS = 0x10,
 };
 
+extern int format_elapsed(char * const buf, const int fmt, int elapsed);
 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 } })