1 | #include "globals.h"
|
---|
2 | #include "oscam-time.h"
|
---|
3 |
|
---|
4 | int32_t comp_timeb(struct timeb *tpa, struct timeb *tpb)
|
---|
5 | {
|
---|
6 | return ((tpa->time - tpb->time) * 1000) + (tpa->millitm - tpb->millitm);
|
---|
7 | }
|
---|
8 |
|
---|
9 | /* Checks if year is a leap year. If so, 1 is returned, else 0. */
|
---|
10 | static int8_t is_leap(unsigned int y)
|
---|
11 | {
|
---|
12 | return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
|
---|
13 | }
|
---|
14 |
|
---|
15 | /* Drop-in replacement for timegm function as some plattforms strip the function from their libc.. */
|
---|
16 | time_t cs_timegm(struct tm *tm)
|
---|
17 | {
|
---|
18 | time_t result = 0;
|
---|
19 | int32_t i;
|
---|
20 | if(tm->tm_mon > 12 || tm->tm_mon < 0 || tm->tm_mday > 31 || tm->tm_min > 60 || tm->tm_sec > 60 || tm->tm_hour > 24)
|
---|
21 | { return 0; }
|
---|
22 | for(i = 70; i < tm->tm_year; ++i)
|
---|
23 | {
|
---|
24 | result += is_leap(i + 1900) ? 366 : 365;
|
---|
25 | }
|
---|
26 | for(i = 0; i < tm->tm_mon; ++i)
|
---|
27 | {
|
---|
28 | if(i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11) { result += 31; }
|
---|
29 | else if(i == 3 || i == 5 || i == 8 || i == 10) { result += 30; }
|
---|
30 | else if(is_leap(tm->tm_year + 1900)) { result += 29; }
|
---|
31 | else { result += 28; }
|
---|
32 | }
|
---|
33 | result += tm->tm_mday - 1;
|
---|
34 | result *= 24;
|
---|
35 | result += tm->tm_hour;
|
---|
36 | result *= 60;
|
---|
37 | result += tm->tm_min;
|
---|
38 | result *= 60;
|
---|
39 | result += tm->tm_sec;
|
---|
40 | return result;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /* Drop-in replacement for gmtime_r as some plattforms strip the function from their libc. */
|
---|
44 | struct tm *cs_gmtime_r(const time_t *timep, struct tm *r)
|
---|
45 | {
|
---|
46 | static const int16_t daysPerMonth[13] = { 0,
|
---|
47 | 31,
|
---|
48 | 31 + 28,
|
---|
49 | 31 + 28 + 31,
|
---|
50 | 31 + 28 + 31 + 30,
|
---|
51 | 31 + 28 + 31 + 30 + 31,
|
---|
52 | 31 + 28 + 31 + 30 + 31 + 30,
|
---|
53 | 31 + 28 + 31 + 30 + 31 + 30 + 31,
|
---|
54 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
|
---|
55 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
|
---|
56 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
|
---|
57 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
|
---|
58 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31
|
---|
59 | };
|
---|
60 | time_t i;
|
---|
61 | time_t work = * timep % 86400;
|
---|
62 | r->tm_sec = work % 60;
|
---|
63 | work /= 60;
|
---|
64 | r->tm_min = work % 60;
|
---|
65 | r->tm_hour = work / 60;
|
---|
66 | work = * timep / 86400;
|
---|
67 | r->tm_wday = (4 + work) % 7;
|
---|
68 | for(i = 1970; ; ++i)
|
---|
69 | {
|
---|
70 | time_t k = is_leap(i) ? 366 : 365;
|
---|
71 | if(work >= k)
|
---|
72 | { work -= k; }
|
---|
73 | else
|
---|
74 | { break; }
|
---|
75 | }
|
---|
76 | r->tm_year = i - 1900;
|
---|
77 | r->tm_yday = work;
|
---|
78 | r->tm_mday = 1;
|
---|
79 | if(is_leap(i) && work > 58)
|
---|
80 | {
|
---|
81 | if(work == 59)
|
---|
82 | { r->tm_mday = 2; } /* 29.2. */
|
---|
83 | work -= 1;
|
---|
84 | }
|
---|
85 | for(i = 11; i && daysPerMonth[i] > work; --i)
|
---|
86 | { ; }
|
---|
87 | r->tm_mon = i;
|
---|
88 | r->tm_mday += work - daysPerMonth[i];
|
---|
89 | return r;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* Drop-in replacement for ctime_r as some plattforms strip the function from their libc. */
|
---|
93 | char *cs_ctime_r(const time_t *timep, char *buf)
|
---|
94 | {
|
---|
95 | struct tm t;
|
---|
96 | localtime_r(timep, &t);
|
---|
97 | strftime(buf, 26, "%c\n", &t);
|
---|
98 | return buf;
|
---|
99 | }
|
---|
100 |
|
---|
101 | void cs_ftime(struct timeb *tp)
|
---|
102 | {
|
---|
103 | struct timeval tv;
|
---|
104 | gettimeofday(&tv, NULL);
|
---|
105 | tp->time = tv.tv_sec;
|
---|
106 | tp->millitm = tv.tv_usec / 1000;
|
---|
107 | }
|
---|
108 |
|
---|
109 | void cs_sleepms(uint32_t msec)
|
---|
110 | {
|
---|
111 | //does not interfere with signals like sleep and usleep do
|
---|
112 | struct timespec req_ts;
|
---|
113 | req_ts.tv_sec = msec / 1000;
|
---|
114 | req_ts.tv_nsec = (msec % 1000) * 1000000L;
|
---|
115 | int32_t olderrno = errno; // Some OS (especially MacOSX) seem to set errno to ETIMEDOUT when sleeping
|
---|
116 | while (1)
|
---|
117 | {
|
---|
118 | /* Sleep for the time specified in req_ts. If interrupted by a
|
---|
119 | signal, place the remaining time left to sleep back into req_ts. */
|
---|
120 | int rval = nanosleep (&req_ts, &req_ts);
|
---|
121 | if (rval == 0)
|
---|
122 | return; // Completed the entire sleep time; all done.
|
---|
123 | else if (errno == EINTR)
|
---|
124 | continue; // Interrupted by a signal. Try again.
|
---|
125 | else
|
---|
126 | return; // Some other error; bail out.
|
---|
127 | }
|
---|
128 | errno = olderrno;
|
---|
129 | }
|
---|
130 |
|
---|
131 | void cs_sleepus(uint32_t usec)
|
---|
132 | {
|
---|
133 | //does not interfere with signals like sleep and usleep do
|
---|
134 | struct timespec req_ts;
|
---|
135 | req_ts.tv_sec = usec / 1000000;
|
---|
136 | req_ts.tv_nsec = (usec % 1000000) * 1000L;
|
---|
137 | int32_t olderrno = errno; // Some OS (especially MacOSX) seem to set errno to ETIMEDOUT when sleeping
|
---|
138 | while (1)
|
---|
139 | {
|
---|
140 | /* Sleep for the time specified in req_ts. If interrupted by a
|
---|
141 | signal, place the remaining time left to sleep back into req_ts. */
|
---|
142 | int rval = nanosleep (&req_ts, &req_ts);
|
---|
143 | if (rval == 0)
|
---|
144 | return; // Completed the entire sleep time; all done.
|
---|
145 | else if (errno == EINTR)
|
---|
146 | continue; // Interrupted by a signal. Try again.
|
---|
147 | else
|
---|
148 | return; // Some other error; bail out.
|
---|
149 | }
|
---|
150 | errno = olderrno;
|
---|
151 | }
|
---|
152 |
|
---|
153 | void add_ms_to_timespec(struct timespec *timeout, int32_t msec)
|
---|
154 | {
|
---|
155 | struct timeval now;
|
---|
156 | gettimeofday(&now, NULL);
|
---|
157 | int32_t nano_secs = ((now.tv_usec * 1000) + ((msec % 1000) * 1000 * 1000));
|
---|
158 | timeout->tv_sec = now.tv_sec + (msec / 1000) + (nano_secs / 1000000000);
|
---|
159 | timeout->tv_nsec = nano_secs % 1000000000;
|
---|
160 | }
|
---|
161 |
|
---|
162 | int32_t add_ms_to_timeb(struct timeb *tb, int32_t ms)
|
---|
163 | {
|
---|
164 | struct timeb tb_now;
|
---|
165 | tb->time += ms / 1000;
|
---|
166 | tb->millitm += ms % 1000;
|
---|
167 | if(tb->millitm >= 1000)
|
---|
168 | {
|
---|
169 | tb->millitm -= 1000;
|
---|
170 | tb->time++;
|
---|
171 | }
|
---|
172 | cs_ftime(&tb_now);
|
---|
173 | return comp_timeb(tb, &tb_now);
|
---|
174 | }
|
---|
175 |
|
---|
176 | void sleepms_on_cond(pthread_cond_t *cond, pthread_mutex_t *mutex, uint32_t msec)
|
---|
177 | {
|
---|
178 | struct timespec ts;
|
---|
179 | add_ms_to_timespec(&ts, msec);
|
---|
180 | pthread_mutex_lock(mutex);
|
---|
181 | pthread_cond_timedwait(cond, mutex, &ts); // sleep on sleep_cond
|
---|
182 | pthread_mutex_unlock(mutex);
|
---|
183 | }
|
---|