Skip to content

Commit

Permalink
Added time management
Browse files Browse the repository at this point in the history
  • Loading branch information
ariannagavioli committed Dec 5, 2018
1 parent 714d12a commit 41709ba
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/timemanagement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#ifndef TIME_MANAGEMENT_H
#define TIME_MANAGEMENT_H
#endif

/*Copies time struct ts into td*/
void time_copy(struct timespec *td, struct timespec ts);

/*Adds ms milliseconds to the time variable pointed by t*/
void time_add_ms(struct timespec *t, int ms);

/* Returns 0 if t1 == t2
* Returns 1 if t1 > t2
* Returns -1 if t1 < 2
*/
int time_cmp(struct timespec t1,struct timespec t2);
31 changes: 31 additions & 0 deletions src/timemanagement.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <time.h>
#include <timemanagement.h>

/*Copies time struct ts into td*/
void time_copy(struct timespec *td, struct timespec ts){
td->tv_sec = ts.tv_sec;
td->tv_nsec = ts.tv_nsec;
}

/*Adds ms milliseconds to the time variable pointed by t*/
void time_add_ms(struct timespec *t, int ms){
t->tv_sec += ms/1000;
t->tv_nsec += (ms%1000)*1000000;
if (t->tv_nsec > 1000000000) {
t->tv_nsec -= 1000000000;
t->tv_sec += 1;
}
}

/* Returns 0 if t1 == t2
* Returns 1 if t1 > t2
* Returns -1 if t1 < 2
*/
int time_cmp(struct timespec t1,struct timespec t2){
if (t1.tv_sec > t2.tv_sec) return 1;
if (t1.tv_sec < t2.tv_sec) return -1;
if (t1.tv_nsec > t2.tv_nsec) return 1;
if (t1.tv_nsec < t2.tv_nsec) return -1;
return 0;
}

0 comments on commit 41709ba

Please sign in to comment.