diff --git a/src/commons/temporal.c b/src/commons/temporal.c index 1fa24495..81c5e19b 100644 --- a/src/commons/temporal.c +++ b/src/commons/temporal.c @@ -17,8 +17,6 @@ #include "error.h" #include "string.h" -#include -#include #include #include #include @@ -28,37 +26,37 @@ static int64_t calculate_delta_ms(t_temporal* temporal); char *temporal_get_string_time(const char* format) { char* str_time = strdup(format); - struct timespec* log_timespec = malloc(sizeof(struct timespec)); - struct tm* log_tm = malloc(sizeof(struct tm)); + struct timespec log_timespec; + struct tm log_tm; char* milisec; - if(clock_gettime(CLOCK_REALTIME, log_timespec) == -1) { + if (clock_gettime(CLOCK_REALTIME, &log_timespec) == -1) { + error_show("Error getting date!"); + free(str_time); return NULL; } - milisec = string_from_format("%03ld", log_timespec->tv_nsec / 1000000); + milisec = string_from_format("%03ld", log_timespec.tv_nsec / 1000000); - for(char* ms = strstr(str_time, "%MS"); ms != NULL; ms = strstr(ms + 3, "%MS")) { + for (char* ms = strstr(str_time, "%MS"); ms != NULL; ms = strstr(ms + 3, "%MS")) { memcpy(ms, milisec, 3); } - localtime_r(&log_timespec->tv_sec, log_tm); - strftime(str_time, strlen(format) + 1, str_time, log_tm); + localtime_r(&log_timespec.tv_sec, &log_tm); + strftime(str_time, strlen(format) + 1, str_time, &log_tm); free(milisec); - free(log_tm); - free(log_timespec); return str_time; } t_temporal* temporal_create(void) { t_temporal* self = malloc(sizeof(t_temporal)); - + self->elapsed_ms = 0; - self->state = TEMPORAL_STATUS_RUNNING; - + self->status = TEMPORAL_STATUS_RUNNING; + clock_gettime(CLOCK_MONOTONIC_RAW, &self->current); - + return self; } @@ -67,30 +65,30 @@ void temporal_destroy(t_temporal* temporal) { } int64_t temporal_gettime(t_temporal* temporal) { - if (temporal->state == TEMPORAL_STATUS_STOPPED) { + if (temporal->status == TEMPORAL_STATUS_STOPPED) { return temporal->elapsed_ms; } - + int64_t delta_ms = calculate_delta_ms(temporal); return delta_ms + temporal->elapsed_ms; } void temporal_stop(t_temporal* temporal) { - if (temporal->state == TEMPORAL_STATUS_STOPPED) { + if (temporal->status == TEMPORAL_STATUS_STOPPED) { return; } temporal->elapsed_ms += calculate_delta_ms(temporal); - temporal->state = TEMPORAL_STATUS_STOPPED; + temporal->status = TEMPORAL_STATUS_STOPPED; } void temporal_resume(t_temporal* temporal) { - if (temporal->state == TEMPORAL_STATUS_RUNNING) { + if (temporal->status == TEMPORAL_STATUS_RUNNING) { return; } - temporal->state = TEMPORAL_STATUS_RUNNING; + temporal->status = TEMPORAL_STATUS_RUNNING; clock_gettime(CLOCK_MONOTONIC_RAW, &temporal->current); } @@ -101,8 +99,8 @@ int64_t temporal_diff(t_temporal* temporal_1, t_temporal* temporal_2) { static int64_t calculate_delta_ms(t_temporal* temporal) { struct timespec now; clock_gettime(CLOCK_MONOTONIC_RAW, &now); - + int64_t delta_ms = (now.tv_sec - temporal->current.tv_sec) * 1000 + (now.tv_nsec - temporal->current.tv_nsec) / 1000000; - + return delta_ms; -} \ No newline at end of file +} diff --git a/src/commons/temporal.h b/src/commons/temporal.h index b2bfd9f4..1e3cea69 100644 --- a/src/commons/temporal.h +++ b/src/commons/temporal.h @@ -20,24 +20,13 @@ #include /** - * @NAME: temporal_get_string_time - * @DESC: Retorna un string con la hora actual, - * con el formato recibido por parámetro. - * Ejemplos: - * temporal_get_string_time("%d/%m/%y") => "30/09/20" - * temporal_get_string_time("%H:%M:%S:%MS") => "12:51:59:331" - * temporal_get_string_time("%d/%m/%y %H:%M:%S") => "30/09/20 12:51:59" - */ - char *temporal_get_string_time(const char* format); - - /** - * @NAME: t_state + * @NAME: t_temporal_status * @DESC: Estado de una variable temporal. */ typedef enum { TEMPORAL_STATUS_STOPPED, TEMPORAL_STATUS_RUNNING - } t_state; + } t_temporal_status; /** * @NAME: t_temporal @@ -46,9 +35,20 @@ typedef struct { struct timespec current; int64_t elapsed_ms; - t_state state; + t_temporal_status status; } t_temporal; + /** + * @NAME: temporal_get_string_time + * @DESC: Retorna un string con la hora actual, + * con el formato recibido por parámetro. + * Ejemplos: + * temporal_get_string_time("%d/%m/%y") => "30/09/20" + * temporal_get_string_time("%H:%M:%S:%MS") => "12:51:59:331" + * temporal_get_string_time("%d/%m/%y %H:%M:%S") => "30/09/20 12:51:59" + */ + char *temporal_get_string_time(const char* format); + /** * @NAME: temporal_create * @DESC: Crea una variable temporal e inicia su cronómetro. diff --git a/tests/integration-tests/temporal/main.c b/tests/integration-tests/temporal/main.c index b8022e39..4ef67118 100644 --- a/tests/integration-tests/temporal/main.c +++ b/tests/integration-tests/temporal/main.c @@ -106,7 +106,7 @@ void test_temporal_diff() { temporal_destroy(temporal3); } -void test_temporal_state() { +void test_temporal_status() { /** * @DESC: Verificar el comportamiento del temporal en sus diferentes estados. * En este test se verifica que no ocurra ningún error al usar el temporal de cualquier forma. @@ -128,7 +128,7 @@ void test_temporal_state() { temporal_resume(temporal3); sleep(1); - printf("test_temporal_state: %lums (expected: ~1000ms)\n\n", temporal_gettime(temporal3)); + printf("test_temporal_status: %lums (expected: ~1000ms)\n\n", temporal_gettime(temporal3)); temporal_destroy(temporal3); } @@ -156,7 +156,7 @@ int main(int argc, char** argv) { test_temporal_stop(); test_temporal_resume(); test_temporal_diff(); - test_temporal_state(); + test_temporal_status(); return (EXIT_SUCCESS); } \ No newline at end of file