source: mainline/uspace/lib/posix/src/time.c@ e0f47f5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e0f47f5 was df2e5514, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Rename uspace/lib/posix/source to src, for consistency with other libraries.

  • Property mode set to 100644
File size: 8.8 KB
Line 
1/*
2 * Copyright (c) 2011 Petr Koupy
3 * Copyright (c) 2011 Jiri Zarevucky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libposix
31 * @{
32 */
33/** @file Time measurement support.
34 */
35
36#define LIBPOSIX_INTERNAL
37#define __POSIX_DEF__(x) posix_##x
38
39#include "internal/common.h"
40#include "posix/time.h"
41
42#include "posix/ctype.h"
43
44#include <errno.h>
45
46#include "posix/signal.h"
47#include <assert.h>
48
49#include "libc/async.h"
50#include "libc/malloc.h"
51#include "libc/task.h"
52#include "libc/stats.h"
53#include "libc/stddef.h"
54#include "libc/sys/time.h"
55
56// TODO: test everything in this file
57
58/* In some places in this file, phrase "normalized broken-down time" is used.
59 * This means time broken down to components (year, month, day, hour, min, sec),
60 * in which every component is in its proper bounds. Non-normalized time could
61 * e.g. be 2011-54-5 29:13:-5, which would semantically mean start of year 2011
62 * + 53 months + 4 days + 29 hours + 13 minutes - 5 seconds.
63 */
64
65int posix_daylight;
66long posix_timezone;
67char *posix_tzname[2];
68
69/**
70 * Set timezone conversion information.
71 */
72void posix_tzset(void)
73{
74 // TODO: read environment
75 posix_tzname[0] = (char *) "GMT";
76 posix_tzname[1] = (char *) "GMT";
77 posix_daylight = 0;
78 posix_timezone = 0;
79}
80
81/**
82 * Get the time in seconds
83 *
84 * @param t If t is non-NULL, the return value is also stored in the memory
85 * pointed to by t.
86 * @return On success, the value of time in seconds since the Epoch
87 * is returned. On error, (time_t)-1 is returned.
88 */
89time_t posix_time(time_t *t)
90{
91 return time(t);
92}
93
94/**
95 * Converts a time value to a broken-down UTC time.
96 *
97 * @param timer Time to convert.
98 * @param result Structure to store the result to.
99 * @return Value of result on success, NULL on overflow.
100 */
101struct tm *posix_gmtime_r(const time_t *restrict timer,
102 struct tm *restrict result)
103{
104 if (failed(time_utc2tm(*timer, result))) {
105 return NULL;
106 }
107
108 return result;
109}
110
111/**
112 * Converts a time value to a broken-down UTC time.
113 * (non reentrant version)
114 *
115 * @param timep Time to convert
116 * @return Pointer to a statically allocated structure that stores
117 * the result, NULL in case of error.
118 */
119struct tm *posix_gmtime(const time_t *restrict timep)
120{
121 static struct tm result;
122
123 return posix_gmtime_r(timep, &result);
124}
125
126/**
127 * Converts a time value to a broken-down local time.
128 *
129 * @param timer Time to convert.
130 * @param result Structure to store the result to.
131 * @return Value of result on success, NULL on overflow.
132 */
133struct tm *posix_localtime_r(const time_t *restrict timer,
134 struct tm *restrict result)
135{
136 // TODO: deal with timezone
137 // currently assumes system and all times are in GMT
138 return posix_gmtime_r(timer, result);
139}
140
141/**
142 * Converts a time value to a broken-down local time.
143 * (non reentrant version)
144 *
145 * @param timep Time to convert.
146 * @return Pointer to a statically allocated structure that stores
147 * the result, NULL in case of error.
148 */
149struct tm *posix_localtime(const time_t *restrict timep)
150{
151 static struct tm result;
152
153 return posix_localtime_r(timep, &result);
154}
155
156/**
157 * Converts broken-down time to a string in format
158 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
159 *
160 * @param timeptr Broken-down time structure.
161 * @param buf Buffer to store string to, must be at least ASCTIME_BUF_LEN
162 * bytes long.
163 * @return Value of buf.
164 */
165char *posix_asctime_r(const struct tm *restrict timeptr,
166 char *restrict buf)
167{
168 time_tm2str(timeptr, buf);
169 return buf;
170}
171
172/**
173 * Convers broken-down time to a string in format
174 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
175 * (non reentrant version)
176 *
177 * @param timeptr Broken-down time structure.
178 * @return Pointer to a statically allocated buffer that stores
179 * the result, NULL in case of error.
180 */
181char *posix_asctime(const struct tm *restrict timeptr)
182{
183 static char buf[ASCTIME_BUF_LEN];
184
185 return posix_asctime_r(timeptr, buf);
186}
187
188/**
189 * Converts the calendar time to a string in format
190 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
191 *
192 * @param timer Time to convert.
193 * @param buf Buffer to store string to. Must be at least ASCTIME_BUF_LEN
194 * bytes long.
195 * @return Pointer to buf on success, NULL on failure.
196 */
197char *posix_ctime_r(const time_t *timer, char *buf)
198{
199 if (failed(time_local2str(*timer, buf))) {
200 return NULL;
201 }
202
203 return buf;
204}
205
206/**
207 * Converts the calendar time to a string in format
208 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
209 * (non reentrant version)
210 *
211 * @param timep Time to convert.
212 * @return Pointer to a statically allocated buffer that stores
213 * the result, NULL in case of error.
214 */
215char *posix_ctime(const time_t *timep)
216{
217 static char buf[ASCTIME_BUF_LEN];
218
219 return posix_ctime_r(timep, buf);
220}
221
222/**
223 * Get clock resolution. Only CLOCK_REALTIME is supported.
224 *
225 * @param clock_id Clock ID.
226 * @param res Pointer to the variable where the resolution is to be written.
227 * @return 0 on success, -1 with errno set on failure.
228 */
229int posix_clock_getres(posix_clockid_t clock_id, struct posix_timespec *res)
230{
231 assert(res != NULL);
232
233 switch (clock_id) {
234 case CLOCK_REALTIME:
235 res->tv_sec = 0;
236 res->tv_nsec = 1000; /* Microsecond resolution. */
237 return 0;
238 default:
239 errno = EINVAL;
240 return -1;
241 }
242}
243
244/**
245 * Get time. Only CLOCK_REALTIME is supported.
246 *
247 * @param clock_id ID of the clock to query.
248 * @param tp Pointer to the variable where the time is to be written.
249 * @return 0 on success, -1 with errno on failure.
250 */
251int posix_clock_gettime(posix_clockid_t clock_id, struct posix_timespec *tp)
252{
253 assert(tp != NULL);
254
255 switch (clock_id) {
256 case CLOCK_REALTIME:
257 ;
258 struct timeval tv;
259 gettimeofday(&tv, NULL);
260 tp->tv_sec = tv.tv_sec;
261 tp->tv_nsec = tv.tv_usec * 1000;
262 return 0;
263 default:
264 errno = EINVAL;
265 return -1;
266 }
267}
268
269/**
270 * Set time on a specified clock. As HelenOS doesn't support this yet,
271 * this function always fails.
272 *
273 * @param clock_id ID of the clock to set.
274 * @param tp Time to set.
275 * @return 0 on success, -1 with errno on failure.
276 */
277int posix_clock_settime(posix_clockid_t clock_id,
278 const struct posix_timespec *tp)
279{
280 assert(tp != NULL);
281
282 switch (clock_id) {
283 case CLOCK_REALTIME:
284 // TODO: setting clock
285 // FIXME: HelenOS doesn't actually support hardware
286 // clock yet
287 errno = EPERM;
288 return -1;
289 default:
290 errno = EINVAL;
291 return -1;
292 }
293}
294
295/**
296 * Sleep on a specified clock.
297 *
298 * @param clock_id ID of the clock to sleep on (only CLOCK_REALTIME supported).
299 * @param flags Flags (none supported).
300 * @param rqtp Sleep time.
301 * @param rmtp Remaining time is written here if sleep is interrupted.
302 * @return 0 on success, -1 with errno set on failure.
303 */
304int posix_clock_nanosleep(posix_clockid_t clock_id, int flags,
305 const struct posix_timespec *rqtp, struct posix_timespec *rmtp)
306{
307 assert(rqtp != NULL);
308 assert(rmtp != NULL);
309
310 switch (clock_id) {
311 case CLOCK_REALTIME:
312 // TODO: interruptible sleep
313 if (rqtp->tv_sec != 0) {
314 async_sleep(rqtp->tv_sec);
315 }
316 if (rqtp->tv_nsec != 0) {
317 async_usleep(rqtp->tv_nsec / 1000);
318 }
319 return 0;
320 default:
321 errno = EINVAL;
322 return -1;
323 }
324}
325
326/**
327 * Get CPU time used since the process invocation.
328 *
329 * @return Consumed CPU cycles by this process or -1 if not available.
330 */
331posix_clock_t posix_clock(void)
332{
333 posix_clock_t total_cycles = -1;
334 stats_task_t *task_stats = stats_get_task(task_get_id());
335 if (task_stats) {
336 total_cycles = (posix_clock_t) (task_stats->kcycles +
337 task_stats->ucycles);
338 free(task_stats);
339 task_stats = 0;
340 }
341
342 return total_cycles;
343}
344
345/** @}
346 */
Note: See TracBrowser for help on using the repository browser.