source: mainline/uspace/lib/posix/source/time.c@ 3e6a98c5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e6a98c5 was 3e6a98c5, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Standards-compliant boolean type.

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