From c32dd469d05f43c26fd9fcac5260f3a184b3804e Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 17 Nov 2018 13:34:25 -0300 Subject: [PATCH] make timer display show hours and minutes --- src/timer.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/timer.c b/src/timer.c index f7611fc..9783992 100644 --- a/src/timer.c +++ b/src/timer.c @@ -15,7 +15,6 @@ #include #include -#include "timer.h" /** * Cuando empezó el timer @@ -57,7 +56,26 @@ void print_timer() { fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n"); } else { - fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", (long int) (stop_time - start_time)); + long int seconds = (long int) (stop_time - start_time); + long int minutes = 0; + long int hours = 0; + if (seconds >= 3600) { + hours = seconds / 3600; + seconds = seconds - (hours * 3600); + if (seconds >= 60) { + minutes = seconds / 60; + seconds = seconds - (minutes * 60); + } + fprintf(stdout, "Tiempo de ejecución: %ld horas, %ld minutos y %ld segundos\n", hours, minutes, seconds); + } + else if (seconds >= 60) { + minutes = seconds / 60; + seconds = seconds - (minutes * 60); + fprintf(stdout, "Tiempo de ejecución: %ld minutos y %ld segundos\n", minutes, seconds); + } + else { + fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", seconds); + } } } @@ -66,5 +84,11 @@ void print_timer() { * @return El tiempo que demoró */ long int get_timer() { + if (start_time == 0) { + fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n"); + } + else if (stop_time == 0) { + fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n"); + } return (long int) (stop_time - start_time); }