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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7570a95f was bd41ac52, checked in by Jakub Jermar <jakub@…>, 7 years ago

Get rid of sys/time.h

This commit moves the POSIX-like time functionality from libc's
sys/time.h to libposix and introduces C11-like or HelenOS-specific
interfaces to libc.

Specifically, use of sys/time.h, struct timeval, suseconds_t and
gettimeofday is replaced by time.h (C11), struct timespec (C11), usec_t
(HelenOS) and getuptime / getrealtime (HelenOS).

Also attempt to fix the implementation of clock() to return microseconds
(clocks) rather than processor cycles and move it to libc.

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