source: mainline/uspace/lib/posix/source/time.c@ 79ea5af

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 79ea5af 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
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#include "posix/errno.h"
44#include "posix/signal.h"
45#include "posix/assert.h"
46
47#include "libc/malloc.h"
48#include "libc/task.h"
49#include "libc/stats.h"
50#include "libc/stdbool.h"
51#include "libc/sys/time.h"
52
53// TODO: test everything in this file
54
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
62int posix_daylight;
63long posix_timezone;
64char *posix_tzname[2];
65
66/**
67 * Set timezone conversion information.
68 */
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
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
91/**
92 * Converts a time value to a broken-down UTC time.
93 *
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.
97 */
98struct tm *posix_gmtime_r(const time_t *restrict timer,
99 struct tm *restrict result)
100{
101 int rc = time_utc2tm(*timer, result);
102 if (rc != EOK) {
103 errno = rc;
104 return NULL;
105 }
106
107 return result;
108}
109
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
125/**
126 * Converts a time value to a broken-down local time.
127 *
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.
131 */
132struct tm *posix_localtime_r(const time_t *restrict timer,
133 struct tm *restrict result)
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
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
155/**
156 * Converts broken-down time to a string in format
157 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
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.
163 */
164char *posix_asctime_r(const struct tm *restrict timeptr,
165 char *restrict buf)
166{
167 time_tm2str(timeptr, buf);
168 return buf;
169}
170
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];
183
184 return posix_asctime_r(timeptr, buf);
185}
186
187/**
188 * Converts the calendar time to a string in format
189 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
190 *
191 * @param timer Time to convert.
192 * @param buf Buffer to store string to. Must be at least ASCTIME_BUF_LEN
193 * bytes long.
194 * @return Pointer to buf on success, NULL on failure.
195 */
196char *posix_ctime_r(const time_t *timer, char *buf)
197{
198 int r = time_local2str(*timer, buf);
199 if (r != EOK) {
200 errno = r;
201 return NULL;
202 }
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);
221}
222
223/**
224 * Get clock resolution. Only CLOCK_REALTIME is supported.
225 *
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.
229 */
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
245/**
246 * Get time. Only CLOCK_REALTIME is supported.
247 *
248 * @param clock_id ID of the clock to query.
249 * @param tp Pointer to the variable where the time is to be written.
250 * @return 0 on success, -1 with errno on failure.
251 */
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
270/**
271 * Set time on a specified clock. As HelenOS doesn't support this yet,
272 * this function always fails.
273 *
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.
277 */
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
296/**
297 * Sleep on a specified clock.
298 *
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.
304 */
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
327/**
328 * Get CPU time used since the process invocation.
329 *
330 * @return Consumed CPU cycles by this process or -1 if not available.
331 */
332posix_clock_t posix_clock(void)
333{
334 posix_clock_t total_cycles = -1;
335 stats_task_t *task_stats = stats_get_task(task_get_id());
336 if (task_stats) {
337 total_cycles = (posix_clock_t) (task_stats->kcycles +
338 task_stats->ucycles);
339 free(task_stats);
340 task_stats = 0;
341 }
342
343 return total_cycles;
344}
345
346/** @}
347 */
Note: See TracBrowser for help on using the repository browser.