Skip to content

Commit

Permalink
Merge pull request #161 from julian-salinas/master
Browse files Browse the repository at this point in the history
feat: api para manejo de timestamps (#156)
  • Loading branch information
RaniAgus authored Aug 24, 2022
2 parents ff6a429 + 8c5881f commit 820bfbb
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 27 deletions.
59 changes: 59 additions & 0 deletions src/commons/temporal.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
#include "error.h"
#include "string.h"

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

static int64_t calculate_delta_ms(t_temporal* temporal);

char *temporal_get_string_time(const char* format) {
char* str_time = strdup(format);

Expand All @@ -47,3 +50,59 @@ char *temporal_get_string_time(const char* format) {

return str_time;
}

t_temporal* temporal_create(void) {
t_temporal* self = malloc(sizeof(t_temporal));

self->elapsed_ms = 0;
self->state = TEMPORAL_STATUS_RUNNING;

clock_gettime(CLOCK_MONOTONIC_RAW, &self->current);

return self;
}

void temporal_destroy(t_temporal* temporal) {
free(temporal);
}

int64_t temporal_gettime(t_temporal* temporal) {
if (temporal->state == 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) {
return;
}

temporal->elapsed_ms += calculate_delta_ms(temporal);
temporal->state = TEMPORAL_STATUS_STOPPED;
}

void temporal_resume(t_temporal* temporal) {
if (temporal->state == TEMPORAL_STATUS_RUNNING) {
return;
}

temporal->state = TEMPORAL_STATUS_RUNNING;
clock_gettime(CLOCK_MONOTONIC_RAW, &temporal->current);
}

int64_t temporal_diff(t_temporal* temporal_1, t_temporal* temporal_2) {
return temporal_gettime(temporal_1) - temporal_gettime(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;
}
71 changes: 70 additions & 1 deletion src/commons/temporal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#ifndef TEMPORAL_H_
#define TEMPORAL_H_

#include <stdint.h>
#include <time.h>

/**
* @NAME: temporal_get_string_time
* @DESC: Retorna un string con la hora actual,
Expand All @@ -27,4 +30,70 @@
*/
char *temporal_get_string_time(const char* format);

#endif /* TEMPORAL_H_ */
/**
* @NAME: t_state
* @DESC: Estado de una variable temporal.
*/
typedef enum {
TEMPORAL_STATUS_STOPPED,
TEMPORAL_STATUS_RUNNING
} t_state;

/**
* @NAME: t_temporal
* @DESC: Estructura de una Variable temporal.
*/
typedef struct {
struct timespec current;
int64_t elapsed_ms;
t_state state;
} t_temporal;

/**
* @NAME: temporal_create
* @DESC: Crea una variable temporal e inicia su cronómetro.
*/
t_temporal* temporal_create(void);

/**
* @NAME: temporal_destroy
* @DESC: Destruye una variable temporal.
* @PARAMS:
* temporal - Variable temporal a destruir.
*/
void temporal_destroy(t_temporal* temporal);

/**
* @NAME: temporal_gettime
* @DESC: Retorna el tiempo total transcurrido mientras el cronómetro estuvo activo en milisegundos.
* @PARAMS:
* temporal - Variable temporal.
*/
int64_t temporal_gettime(t_temporal* temporal);

/**
* @NAME: temporal_stop
* @DESC: Detiene el cronómetro de una variable temporal.
* @PARAMS:
* temporal - Variable temporal a frenar.
*/
void temporal_stop(t_temporal* temporal);

/**
* @NAME: temporal_resume
* @DESC: Reanuda el cronómetro de una variable temporal.
* @PARAMS:
* temporal - Variable temporal a reanudar.
*/
void temporal_resume(t_temporal* temporal);

/**
* @NAME: temporal_diff
* @DESC: Retorna la diferencia del tiempo total transcurrido entre dos variables temporales en milisegundos
* @PARAMS:
* temporal_1 - Primera variable temporal.
* temporal_2 - Segunda variable temporal.
*/
int64_t temporal_diff(t_temporal* temporal_1, t_temporal* temporal_2);

#endif /* TEMPORAL_H_ */
167 changes: 141 additions & 26 deletions tests/integration-tests/temporal/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,152 @@
#include <commons/temporal.h>
#include <commons/string.h>
#include <pthread.h>
#include <unistd.h>

static void print_time() {
char* date = temporal_get_string_time("day/month/year -- %d/%m/%y\n");
char* time = temporal_get_string_time("hour:min:sec:milisec -- %H:%M:%S:%MS\n");
char* date = temporal_get_string_time("day/month/year -- %d/%m/%y\n");
char* time = temporal_get_string_time("hour:min:sec:milisec -- %H:%M:%S:%MS\n");

txt_write_in_stdout(date);
txt_write_in_stdout(time);
txt_write_in_stdout(date);
txt_write_in_stdout(time);

free(date);
free(time);
free(date);
free(time);
}

int main(int argc, char** argv) {
pthread_t th1, th2, th3, th4, th5, th6;

printf("Verificar que fecha y hora sean correctas: \n");

pthread_create(&th1, NULL, (void*) print_time, NULL);
pthread_create(&th2, NULL, (void*) print_time, NULL);
pthread_create(&th3, NULL, (void*) print_time, NULL);
pthread_create(&th4, NULL, (void*) print_time, NULL);
pthread_create(&th5, NULL, (void*) print_time, NULL);
pthread_create(&th6, NULL, (void*) print_time, NULL);

pthread_join(th1, NULL);
pthread_join(th2, NULL);
pthread_join(th3, NULL);
pthread_join(th4, NULL);
pthread_join(th5, NULL);
pthread_join(th6, NULL);

return (EXIT_SUCCESS);
void test_temporal_gettime() {
/**
* @DESC: El tiempo que se obtiene es correcto.
*/
t_temporal *temporal = temporal_create();
sleep(2);

printf("\ntest_temporal_gettime: %ldms (expected: ~2000ms)\n\n", temporal_gettime(temporal));

temporal_destroy(temporal);
}

void test_temporal_stop() {
/**
* @DESC: Al detener el temporal, deja de sumar tiempo.
*/
t_temporal *temporal = temporal_create();
sleep(1);
temporal_stop(temporal);
sleep(2);

printf("test_temporal_stop: %ldms (expected: ~1000ms)\n\n", temporal_gettime(temporal));

temporal_destroy(temporal);
}

void test_temporal_resume() {
/**
* @DESC: Al detener y reanudar el temporal varias veces, el tiempo que este suma es correcto.
*/
t_temporal* temporal = temporal_create();

temporal_stop(temporal);
sleep(3); // +0

printf("test_temporal_resume: %ldms (expected: ~0ms)\n", temporal_gettime(temporal));

temporal_resume(temporal);
sleep(2); // +2
temporal_stop(temporal);

printf("test_temporal_resume: %ldms (expected: ~2000ms)\n", temporal_gettime(temporal));

temporal_resume(temporal);
sleep(2); // +2

printf("test_temporal_resume: %ldms (expected: ~4000ms)\n", temporal_gettime(temporal));

temporal_stop(temporal);
sleep(2); // +0
temporal_resume(temporal);
sleep(1); // +1

printf("test_temporal_resume: %ldms (expected: ~5000ms)\n\n", temporal_gettime(temporal));

temporal_destroy(temporal);
}

void test_temporal_diff() {
/**
* @DESC: Comparar dos variables temporales.
*/

t_temporal* temporal1 = temporal_create();
t_temporal* temporal2 = temporal_create();

sleep(1);
temporal_stop(temporal2);
sleep(1);

t_temporal* temporal3 = temporal_create();

printf("test_temporal_diff: %ldms (expected: ~1000ms)\n", temporal_diff(temporal1, temporal2));
printf("test_temporal_diff: %ldms (expected: ~2000ms)\n", temporal_diff(temporal1, temporal3));
printf("test_temporal_diff: %ldms (expected: ~1000ms)\n", temporal_diff(temporal2, temporal3));
printf("test_temporal_diff: %ldms (expected: ~-1000ms)\n\n", temporal_diff(temporal3, temporal2));

temporal_destroy(temporal1);
temporal_destroy(temporal2);
temporal_destroy(temporal3);
}

void test_temporal_state() {
/**
* @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.
*/

t_temporal* temporal1 = temporal_create();
temporal_destroy(temporal1);

t_temporal* temporal2 = temporal_create();
temporal_stop(temporal2);
temporal_destroy(temporal2);

t_temporal* temporal3 = temporal_create();
temporal_resume(temporal3);
temporal_resume(temporal3);
temporal_stop(temporal3);
temporal_stop(temporal3);
sleep(1);
temporal_resume(temporal3);
sleep(1);

printf("test_temporal_state: %lums (expected: ~1000ms)\n\n", temporal_gettime(temporal3));

temporal_destroy(temporal3);
}

int main(int argc, char** argv) {
pthread_t th1, th2, th3, th4, th5, th6;

printf("Verificar que fecha y hora sean correctas: \n");

pthread_create(&th1, NULL, (void*) print_time, NULL);
pthread_create(&th2, NULL, (void*) print_time, NULL);
pthread_create(&th3, NULL, (void*) print_time, NULL);
pthread_create(&th4, NULL, (void*) print_time, NULL);
pthread_create(&th5, NULL, (void*) print_time, NULL);
pthread_create(&th6, NULL, (void*) print_time, NULL);

pthread_join(th1, NULL);
pthread_join(th2, NULL);
pthread_join(th3, NULL);
pthread_join(th4, NULL);
pthread_join(th5, NULL);
pthread_join(th6, NULL);

test_temporal_gettime();
test_temporal_stop();
test_temporal_resume();
test_temporal_diff();
test_temporal_state();

return (EXIT_SUCCESS);
}

0 comments on commit 820bfbb

Please sign in to comment.