-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiempo.c
148 lines (122 loc) · 3.1 KB
/
tiempo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "tiempo.h"
void timeblock_init(unsigned long secs)
{
timeblock_start = timeblock_now();
timeblock_block = secs;
}
int timeblock_check()
{
unsigned long dife;
dife = timeblock_now() - timeblock_start;
if (dife > timeblock_block) {
timeblock_start = timeblock_now();
return 0;
}
return -1;
}
unsigned long timeblock_now()
{
struct timeval timeval;
gettimeofday(&timeval, NULL);
return (unsigned long)timeval.tv_sec;
}
int tiempo_init(struct tiempo_t **tiempo_ptr)
{
struct tiempo_t *tiempo;
tiempo = malloc(sizeof(struct tiempo_t));
if (tiempo==NULL)
return -1;
*tiempo_ptr = tiempo;
return 0;
}
void tiempo_get(struct tiempo_t *tiempo)
{
struct timeval timeval;
gettimeofday(&timeval, NULL);
tiempo->sec = timeval.tv_sec;
tiempo->usec = timeval.tv_usec;
}
void tiempo_print(struct tiempo_t *tiempo)
{
tiempo_print_hour_usec(tiempo);
}
void tiempo_print_date(struct tiempo_t *tiempo)
{
time_t t = tiempo->sec;
struct tm tm = *localtime(&t);
printf("%02d:%02d:%02d %02d/%02d/%d\n", tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_mday, tm.tm_mon+1, tm.tm_year + 1900);
}
void tiempo_print_hour_usec(struct tiempo_t *tiempo)
{
time_t t = tiempo->sec;
struct tm tm = *localtime(&t);
printf("%02d:%02d:%02d.%06d", tm.tm_hour, tm.tm_min, tm.tm_sec, tiempo->usec);
}
void tiempo_resta(struct tiempo_t *tiempo_des, struct tiempo_t *tiempo1, struct tiempo_t *tiempo2)
{
tiempo_des->sec = tiempo1->sec - tiempo2->sec;
if (tiempo_des->sec == 0) {
tiempo_des->usec = tiempo1->usec - tiempo2->usec;
} else {
if (tiempo1->usec >= tiempo2->usec) {
tiempo_des->usec = tiempo1->usec - tiempo2->usec;
} else {
tiempo_des->sec -= 1;
tiempo_des->usec = (1000000 - tiempo2->usec) + tiempo1->usec;
}
}
}
void tiempo_add(struct tiempo_t *tiempo_des, struct tiempo_t *tiempo_add)
{
tiempo_des->sec = tiempo_des->sec + tiempo_add->sec;
tiempo_des->usec = tiempo_des->usec + tiempo_add->usec;
if (tiempo_des->usec >= 1000000) {
(tiempo_des->sec)++;
tiempo_des->usec = 1000000 - tiempo_des->usec;
}
}
void tiempo_copy(struct tiempo_t *tiempo_des, struct tiempo_t *tiempo_src)
{
tiempo_des->sec = tiempo_src->sec;
tiempo_des->usec = tiempo_src->usec;
}
void crono_init()
{
crono_block = 40000;
tiempo_get(&crono_t1);
}
int crono_set()
{
int diferencia;
tiempo_get(&crono_t2);
tiempo_resta(&crono_resta, &crono_t2, &crono_t1);
if ( (crono_resta.sec > 0 ) || (crono_resta.usec > crono_block) ) {
tiempo_copy(&crono_t1, &crono_t2);
return 0;
}
diferencia = crono_block - crono_resta.usec;
usleep(diferencia);
return -1;
}
void cronoabs_init()
{
cronoabs_block = 40000;
cronoabs_tblock.sec = 0;
cronoabs_tblock.usec = cronoabs_block;
tiempo_get(&cronoabs_t0);
tiempo_get(&cronoabs_t1);
}
int cronoabs_set()
{
int diferencia;
tiempo_get(&cronoabs_t2);
tiempo_resta(&cronoabs_resta, &cronoabs_t2, &cronoabs_t1);
if ( (cronoabs_resta.sec > 0 ) || (cronoabs_resta.usec > cronoabs_block) ) {
//tiempo_copy(&cronoabs_t1, &cronoabs_t2);
tiempo_add(&cronoabs_t1, &cronoabs_tblock);
return 0;
}
diferencia = cronoabs_block - cronoabs_resta.usec;
usleep(diferencia);
return -1;
}