source: mainline/uspace/lib/posix/source/time.c@ b3cf946

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b3cf946 was ed29fe4, checked in by Maurizio Lombardi <m.lombardi85@…>, 12 years ago

add time() to the posix library

  • Property mode set to 100644
File size: 8.9 KB
RevLine 
[2fc5072]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 */
[4cf8ca6]33/** @file Time measurement support.
[2fc5072]34 */
35
[4d10fc8]36#define LIBPOSIX_INTERNAL
[fdf97f6]37#define __POSIX_DEF__(x) posix_##x
[2fc5072]38
[9b1503e]39#include "internal/common.h"
[a3da2b2]40#include "posix/time.h"
[a6d908c1]41
[a3da2b2]42#include "posix/ctype.h"
43#include "posix/errno.h"
44#include "posix/signal.h"
45#include "posix/assert.h"
[324d46b]46
[06cb827]47#include "libc/malloc.h"
[a6d908c1]48#include "libc/task.h"
49#include "libc/stats.h"
[3e6a98c5]50#include "libc/stdbool.h"
[3f466c33]51#include "libc/sys/time.h"
[2fc5072]52
[324d46b]53// TODO: test everything in this file
54
[e6165be]55/* In some places in this file, phrase "normalized broken-down time" is used.
56 * This means time broken down to components (year, month, day, hour, min, sec),
57 * in which every component is in its proper bounds. Non-normalized time could
58 * e.g. be 2011-54-5 29:13:-5, which would semantically mean start of year 2011
59 * + 53 months + 4 days + 29 hours + 13 minutes - 5 seconds.
60 */
61
[324d46b]62int posix_daylight;
63long posix_timezone;
64char *posix_tzname[2];
65
[55b1efd]66/**
67 * Set timezone conversion information.
[4cf8ca6]68 */
[324d46b]69void posix_tzset(void)
70{
71 // TODO: read environment
72 posix_tzname[0] = (char *) "GMT";
73 posix_tzname[1] = (char *) "GMT";
74 posix_daylight = 0;
75 posix_timezone = 0;
76}
77
[ed29fe4]78/**
79 * Get the time in seconds
80 *
81 * @param t If t is non-NULL, the return value is also stored in the memory
82 * pointed to by t.
83 * @return On success, the value of time in seconds since the Epoch
84 * is returned. On error, (time_t)-1 is returned.
85 */
86time_t posix_time(time_t *t)
87{
88 return time(t);
89}
90
[55b1efd]91/**
92 * Converts a time value to a broken-down UTC time.
[4cf8ca6]93 *
[e6165be]94 * @param timer Time to convert.
95 * @param result Structure to store the result to.
96 * @return Value of result on success, NULL on overflow.
[4cf8ca6]97 */
[f8b6d34c]98struct tm *posix_gmtime_r(const time_t *restrict timer,
99 struct tm *restrict result)
[324d46b]100{
[664fc031]101 int rc = time_utc2tm(*timer, result);
[f7ea5400]102 if (rc != EOK) {
103 errno = rc;
[324d46b]104 return NULL;
105 }
106
107 return result;
[2fc5072]108}
109
[f7ea5400]110/**
111 * Converts a time value to a broken-down UTC time.
112 * (non reentrant version)
113 *
114 * @param timep Time to convert
115 * @return Pointer to a statically allocated structure that stores
116 * the result, NULL in case of error.
117 */
118struct tm *posix_gmtime(const time_t *restrict timep)
119{
120 static struct tm result;
121
122 return posix_gmtime_r(timep, &result);
123}
124
[55b1efd]125/**
126 * Converts a time value to a broken-down local time.
[4cf8ca6]127 *
[e6165be]128 * @param timer Time to convert.
129 * @param result Structure to store the result to.
130 * @return Value of result on success, NULL on overflow.
[4cf8ca6]131 */
[f8b6d34c]132struct tm *posix_localtime_r(const time_t *restrict timer,
133 struct tm *restrict result)
[3f466c33]134{
135 // TODO: deal with timezone
136 // currently assumes system and all times are in GMT
137 return posix_gmtime_r(timer, result);
138}
139
[f7ea5400]140/**
141 * Converts a time value to a broken-down local time.
142 * (non reentrant version)
143 *
144 * @param timep Time to convert.
145 * @return Pointer to a statically allocated structure that stores
146 * the result, NULL in case of error.
147 */
148struct tm *posix_localtime(const time_t *restrict timep)
149{
150 static struct tm result;
151
152 return posix_localtime_r(timep, &result);
153}
154
[55b1efd]155/**
156 * Converts broken-down time to a string in format
157 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
[e6165be]158 *
159 * @param timeptr Broken-down time structure.
160 * @param buf Buffer to store string to, must be at least ASCTIME_BUF_LEN
161 * bytes long.
162 * @return Value of buf.
[4cf8ca6]163 */
[f8b6d34c]164char *posix_asctime_r(const struct tm *restrict timeptr,
[324d46b]165 char *restrict buf)
166{
[664fc031]167 time_tm2str(timeptr, buf);
[f7ea5400]168 return buf;
169}
[324d46b]170
[f7ea5400]171/**
172 * Convers broken-down time to a string in format
173 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
174 * (non reentrant version)
175 *
176 * @param timeptr Broken-down time structure.
177 * @return Pointer to a statically allocated buffer that stores
178 * the result, NULL in case of error.
179 */
180char *posix_asctime(const struct tm *restrict timeptr)
181{
182 static char buf[ASCTIME_BUF_LEN];
[324d46b]183
[f7ea5400]184 return posix_asctime_r(timeptr, buf);
[2fc5072]185}
186
[55b1efd]187/**
[f7ea5400]188 * Converts the calendar time to a string in format
189 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
[4cf8ca6]190 *
[e6165be]191 * @param timer Time to convert.
192 * @param buf Buffer to store string to. Must be at least ASCTIME_BUF_LEN
193 * bytes long.
[f7ea5400]194 * @return Pointer to buf on success, NULL on failure.
[4cf8ca6]195 */
[3f466c33]196char *posix_ctime_r(const time_t *timer, char *buf)
197{
[664fc031]198 int r = time_local2str(*timer, buf);
[f7ea5400]199 if (r != EOK) {
200 errno = r;
[3f466c33]201 return NULL;
202 }
[f7ea5400]203
204 return buf;
205}
206
207/**
208 * Converts the calendar time to a string in format
209 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
210 * (non reentrant version)
211 *
212 * @param timep Time to convert.
213 * @return Pointer to a statically allocated buffer that stores
214 * the result, NULL in case of error.
215 */
216char *posix_ctime(const time_t *timep)
217{
218 static char buf[ASCTIME_BUF_LEN];
219
220 return posix_ctime_r(timep, buf);
[2fc5072]221}
222
[55b1efd]223/**
224 * Get clock resolution. Only CLOCK_REALTIME is supported.
[4cf8ca6]225 *
[e6165be]226 * @param clock_id Clock ID.
227 * @param res Pointer to the variable where the resolution is to be written.
228 * @return 0 on success, -1 with errno set on failure.
[4cf8ca6]229 */
[3f466c33]230int posix_clock_getres(posix_clockid_t clock_id, struct posix_timespec *res)
231{
232 assert(res != NULL);
233
234 switch (clock_id) {
235 case CLOCK_REALTIME:
236 res->tv_sec = 0;
237 res->tv_nsec = 1000; /* Microsecond resolution. */
238 return 0;
239 default:
240 errno = EINVAL;
241 return -1;
242 }
243}
244
[55b1efd]245/**
246 * Get time. Only CLOCK_REALTIME is supported.
[4cf8ca6]247 *
[e6165be]248 * @param clock_id ID of the clock to query.
249 * @param tp Pointer to the variable where the time is to be written.
[55b1efd]250 * @return 0 on success, -1 with errno on failure.
[4cf8ca6]251 */
[3f466c33]252int posix_clock_gettime(posix_clockid_t clock_id, struct posix_timespec *tp)
253{
254 assert(tp != NULL);
255
256 switch (clock_id) {
257 case CLOCK_REALTIME:
258 ;
259 struct timeval tv;
260 gettimeofday(&tv, NULL);
261 tp->tv_sec = tv.tv_sec;
262 tp->tv_nsec = tv.tv_usec * 1000;
263 return 0;
264 default:
265 errno = EINVAL;
266 return -1;
267 }
268}
269
[55b1efd]270/**
271 * Set time on a specified clock. As HelenOS doesn't support this yet,
272 * this function always fails.
[4cf8ca6]273 *
[e6165be]274 * @param clock_id ID of the clock to set.
275 * @param tp Time to set.
276 * @return 0 on success, -1 with errno on failure.
[4cf8ca6]277 */
[3f466c33]278int posix_clock_settime(posix_clockid_t clock_id,
279 const struct posix_timespec *tp)
280{
281 assert(tp != NULL);
282
283 switch (clock_id) {
284 case CLOCK_REALTIME:
285 // TODO: setting clock
286 // FIXME: HelenOS doesn't actually support hardware
287 // clock yet
288 errno = EPERM;
289 return -1;
290 default:
291 errno = EINVAL;
292 return -1;
293 }
294}
295
[55b1efd]296/**
297 * Sleep on a specified clock.
[4cf8ca6]298 *
[e6165be]299 * @param clock_id ID of the clock to sleep on (only CLOCK_REALTIME supported).
300 * @param flags Flags (none supported).
301 * @param rqtp Sleep time.
302 * @param rmtp Remaining time is written here if sleep is interrupted.
303 * @return 0 on success, -1 with errno set on failure.
[4cf8ca6]304 */
[3f466c33]305int posix_clock_nanosleep(posix_clockid_t clock_id, int flags,
306 const struct posix_timespec *rqtp, struct posix_timespec *rmtp)
307{
308 assert(rqtp != NULL);
309 assert(rmtp != NULL);
310
311 switch (clock_id) {
312 case CLOCK_REALTIME:
313 // TODO: interruptible sleep
314 if (rqtp->tv_sec != 0) {
315 sleep(rqtp->tv_sec);
316 }
317 if (rqtp->tv_nsec != 0) {
318 usleep(rqtp->tv_nsec / 1000);
319 }
320 return 0;
321 default:
322 errno = EINVAL;
323 return -1;
324 }
325}
326
[55b1efd]327/**
328 * Get CPU time used since the process invocation.
[06cb827]329 *
330 * @return Consumed CPU cycles by this process or -1 if not available.
[823a929]331 */
332posix_clock_t posix_clock(void)
333{
[06cb827]334 posix_clock_t total_cycles = -1;
335 stats_task_t *task_stats = stats_get_task(task_get_id());
336 if (task_stats) {
[cb948777]337 total_cycles = (posix_clock_t) (task_stats->kcycles +
338 task_stats->ucycles);
[a12f7f1]339 free(task_stats);
340 task_stats = 0;
[06cb827]341 }
342
343 return total_cycles;
[823a929]344}
345
[2fc5072]346/** @}
347 */
Note: See TracBrowser for help on using the repository browser.