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
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
[9b1503e]36#include "internal/common.h"
[bd41ac52]37#include "posix/sys/types.h"
38#include "posix/sys/time.h"
[a3da2b2]39#include "posix/time.h"
[a6d908c1]40
[a3da2b2]41#include "posix/ctype.h"
[0d0b319]42
43#include <errno.h>
44
[a3da2b2]45#include "posix/signal.h"
[e8d3c6f5]46#include <assert.h>
[324d46b]47
[39026d7c]48#include "libc/async.h"
[06cb827]49#include "libc/malloc.h"
[a6d908c1]50#include "libc/task.h"
[582a0b8]51#include "libc/stddef.h"
[bd41ac52]52#include "libc/time.h"
[2fc5072]53
[324d46b]54// TODO: test everything in this file
55
[7c3fb9b]56/*
57 * In some places in this file, phrase "normalized broken-down time" is used.
[e6165be]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
[324d46b]64int posix_daylight;
65long posix_timezone;
66char *posix_tzname[2];
67
[55b1efd]68/**
69 * Set timezone conversion information.
[4cf8ca6]70 */
[7f9df7b9]71void tzset(void)
[324d46b]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
[55b1efd]80/**
81 * Converts a time value to a broken-down UTC time.
[1b20da0]82 *
[e6165be]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.
[4cf8ca6]86 */
[7f9df7b9]87struct tm *gmtime_r(const time_t *restrict timer,
[f8b6d34c]88 struct tm *restrict result)
[324d46b]89{
[0d0b319]90 if (failed(time_utc2tm(*timer, result))) {
[324d46b]91 return NULL;
92 }
93
94 return result;
[2fc5072]95}
96
[f7ea5400]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 */
[7f9df7b9]105struct tm *gmtime(const time_t *restrict timep)
[f7ea5400]106{
107 static struct tm result;
108
[7f9df7b9]109 return gmtime_r(timep, &result);
[f7ea5400]110}
111
[55b1efd]112/**
113 * Converts a time value to a broken-down local time.
[1b20da0]114 *
[e6165be]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.
[4cf8ca6]118 */
[7f9df7b9]119struct tm *localtime_r(const time_t *restrict timer,
[f8b6d34c]120 struct tm *restrict result)
[3f466c33]121{
122 // TODO: deal with timezone
123 // currently assumes system and all times are in GMT
[7f9df7b9]124 return gmtime_r(timer, result);
[3f466c33]125}
126
[f7ea5400]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 */
[7f9df7b9]135struct tm *localtime(const time_t *restrict timep)
[f7ea5400]136{
137 static struct tm result;
138
[7f9df7b9]139 return localtime_r(timep, &result);
[f7ea5400]140}
141
[55b1efd]142/**
143 * Converts broken-down time to a string in format
144 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
[e6165be]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.
[4cf8ca6]150 */
[7f9df7b9]151char *asctime_r(const struct tm *restrict timeptr,
[324d46b]152 char *restrict buf)
153{
[664fc031]154 time_tm2str(timeptr, buf);
[f7ea5400]155 return buf;
156}
[324d46b]157
[f7ea5400]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 */
[7f9df7b9]167char *asctime(const struct tm *restrict timeptr)
[f7ea5400]168{
169 static char buf[ASCTIME_BUF_LEN];
[324d46b]170
[7f9df7b9]171 return asctime_r(timeptr, buf);
[2fc5072]172}
173
[55b1efd]174/**
[f7ea5400]175 * Converts the calendar time to a string in format
176 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
[1b20da0]177 *
[e6165be]178 * @param timer Time to convert.
179 * @param buf Buffer to store string to. Must be at least ASCTIME_BUF_LEN
180 * bytes long.
[f7ea5400]181 * @return Pointer to buf on success, NULL on failure.
[4cf8ca6]182 */
[7f9df7b9]183char *ctime_r(const time_t *timer, char *buf)
[3f466c33]184{
[0d0b319]185 if (failed(time_local2str(*timer, buf))) {
[3f466c33]186 return NULL;
187 }
[f7ea5400]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 */
[7f9df7b9]201char *ctime(const time_t *timep)
[f7ea5400]202{
203 static char buf[ASCTIME_BUF_LEN];
204
[7f9df7b9]205 return ctime_r(timep, buf);
[2fc5072]206}
207
[55b1efd]208/**
209 * Get clock resolution. Only CLOCK_REALTIME is supported.
[4cf8ca6]210 *
[e6165be]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.
[4cf8ca6]214 */
[7f9df7b9]215int clock_getres(clockid_t clock_id, struct timespec *res)
[3f466c33]216{
217 assert(res != NULL);
218
219 switch (clock_id) {
[3fafe5e0]220 case CLOCK_REALTIME:
221 res->tv_sec = 0;
[bd41ac52]222 res->tv_nsec = USEC2NSEC(1); /* Microsecond resolution. */
[3fafe5e0]223 return 0;
224 default:
225 errno = EINVAL;
226 return -1;
[3f466c33]227 }
228}
229
[55b1efd]230/**
231 * Get time. Only CLOCK_REALTIME is supported.
[1b20da0]232 *
[e6165be]233 * @param clock_id ID of the clock to query.
234 * @param tp Pointer to the variable where the time is to be written.
[55b1efd]235 * @return 0 on success, -1 with errno on failure.
[4cf8ca6]236 */
[7f9df7b9]237int clock_gettime(clockid_t clock_id, struct timespec *tp)
[3f466c33]238{
[013e5d32]239 struct timeval tv;
240
[3f466c33]241 assert(tp != NULL);
242
243 switch (clock_id) {
[3fafe5e0]244 case CLOCK_REALTIME:
245 gettimeofday(&tv, NULL);
246 tp->tv_sec = tv.tv_sec;
[bd41ac52]247 tp->tv_nsec = USEC2NSEC(tv.tv_usec);
[3fafe5e0]248 return 0;
249 default:
250 errno = EINVAL;
251 return -1;
[3f466c33]252 }
253}
254
[55b1efd]255/**
256 * Set time on a specified clock. As HelenOS doesn't support this yet,
257 * this function always fails.
[1b20da0]258 *
[e6165be]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.
[4cf8ca6]262 */
[7f9df7b9]263int clock_settime(clockid_t clock_id,
264 const struct timespec *tp)
[3f466c33]265{
266 assert(tp != NULL);
267
268 switch (clock_id) {
[3fafe5e0]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;
[3f466c33]278 }
279}
280
[55b1efd]281/**
282 * Sleep on a specified clock.
[1b20da0]283 *
[e6165be]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.
[4cf8ca6]289 */
[7f9df7b9]290int clock_nanosleep(clockid_t clock_id, int flags,
291 const struct timespec *rqtp, struct timespec *rmtp)
[3f466c33]292{
293 assert(rqtp != NULL);
294 assert(rmtp != NULL);
295
296 switch (clock_id) {
[3fafe5e0]297 case CLOCK_REALTIME:
298 // TODO: interruptible sleep
299 if (rqtp->tv_sec != 0) {
[5f97ef44]300 fibril_sleep(rqtp->tv_sec);
[3fafe5e0]301 }
302 if (rqtp->tv_nsec != 0) {
[bd41ac52]303 fibril_usleep(NSEC2USEC(rqtp->tv_nsec));
[3fafe5e0]304 }
305 return 0;
306 default:
307 errno = EINVAL;
308 return -1;
[3f466c33]309 }
310}
311
[bd41ac52]312int gettimeofday(struct timeval *tv, void *tz)
[823a929]313{
[bd41ac52]314 struct timespec ts;
315
316 getrealtime(&ts);
317 tv->tv_sec = ts.tv_sec;
318 tv->tv_usec = NSEC2USEC(ts.tv_nsec);
[06cb827]319
[bd41ac52]320 return 0;
[823a929]321}
322
[2fc5072]323/** @}
324 */
Note: See TracBrowser for help on using the repository browser.