source: mainline/uspace/lib/posix/src/time.c@ 013e5d32

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

More cstyle-related fixes.

  • Property mode set to 100644
File size: 8.3 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"
[a3da2b2]37#include "posix/time.h"
[a6d908c1]38
[a3da2b2]39#include "posix/ctype.h"
[0d0b319]40
41#include <errno.h>
42
[a3da2b2]43#include "posix/signal.h"
[e8d3c6f5]44#include <assert.h>
[324d46b]45
[39026d7c]46#include "libc/async.h"
[06cb827]47#include "libc/malloc.h"
[a6d908c1]48#include "libc/task.h"
49#include "libc/stats.h"
[582a0b8]50#include "libc/stddef.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 */
[7f9df7b9]69void tzset(void)
[324d46b]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
[55b1efd]78/**
79 * Converts a time value to a broken-down UTC time.
[1b20da0]80 *
[e6165be]81 * @param timer Time to convert.
82 * @param result Structure to store the result to.
83 * @return Value of result on success, NULL on overflow.
[4cf8ca6]84 */
[7f9df7b9]85struct tm *gmtime_r(const time_t *restrict timer,
[f8b6d34c]86 struct tm *restrict result)
[324d46b]87{
[0d0b319]88 if (failed(time_utc2tm(*timer, result))) {
[324d46b]89 return NULL;
90 }
91
92 return result;
[2fc5072]93}
94
[f7ea5400]95/**
96 * Converts a time value to a broken-down UTC time.
97 * (non reentrant version)
98 *
99 * @param timep Time to convert
100 * @return Pointer to a statically allocated structure that stores
101 * the result, NULL in case of error.
102 */
[7f9df7b9]103struct tm *gmtime(const time_t *restrict timep)
[f7ea5400]104{
105 static struct tm result;
106
[7f9df7b9]107 return gmtime_r(timep, &result);
[f7ea5400]108}
109
[55b1efd]110/**
111 * Converts a time value to a broken-down local time.
[1b20da0]112 *
[e6165be]113 * @param timer Time to convert.
114 * @param result Structure to store the result to.
115 * @return Value of result on success, NULL on overflow.
[4cf8ca6]116 */
[7f9df7b9]117struct tm *localtime_r(const time_t *restrict timer,
[f8b6d34c]118 struct tm *restrict result)
[3f466c33]119{
120 // TODO: deal with timezone
121 // currently assumes system and all times are in GMT
[7f9df7b9]122 return gmtime_r(timer, result);
[3f466c33]123}
124
[f7ea5400]125/**
126 * Converts a time value to a broken-down local time.
127 * (non reentrant version)
128 *
129 * @param timep Time to convert.
130 * @return Pointer to a statically allocated structure that stores
131 * the result, NULL in case of error.
132 */
[7f9df7b9]133struct tm *localtime(const time_t *restrict timep)
[f7ea5400]134{
135 static struct tm result;
136
[7f9df7b9]137 return localtime_r(timep, &result);
[f7ea5400]138}
139
[55b1efd]140/**
141 * Converts broken-down time to a string in format
142 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
[e6165be]143 *
144 * @param timeptr Broken-down time structure.
145 * @param buf Buffer to store string to, must be at least ASCTIME_BUF_LEN
146 * bytes long.
147 * @return Value of buf.
[4cf8ca6]148 */
[7f9df7b9]149char *asctime_r(const struct tm *restrict timeptr,
[324d46b]150 char *restrict buf)
151{
[664fc031]152 time_tm2str(timeptr, buf);
[f7ea5400]153 return buf;
154}
[324d46b]155
[f7ea5400]156/**
157 * Convers broken-down time to a string in format
158 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
159 * (non reentrant version)
160 *
161 * @param timeptr Broken-down time structure.
162 * @return Pointer to a statically allocated buffer that stores
163 * the result, NULL in case of error.
164 */
[7f9df7b9]165char *asctime(const struct tm *restrict timeptr)
[f7ea5400]166{
167 static char buf[ASCTIME_BUF_LEN];
[324d46b]168
[7f9df7b9]169 return asctime_r(timeptr, buf);
[2fc5072]170}
171
[55b1efd]172/**
[f7ea5400]173 * Converts the calendar time to a string in format
174 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
[1b20da0]175 *
[e6165be]176 * @param timer Time to convert.
177 * @param buf Buffer to store string to. Must be at least ASCTIME_BUF_LEN
178 * bytes long.
[f7ea5400]179 * @return Pointer to buf on success, NULL on failure.
[4cf8ca6]180 */
[7f9df7b9]181char *ctime_r(const time_t *timer, char *buf)
[3f466c33]182{
[0d0b319]183 if (failed(time_local2str(*timer, buf))) {
[3f466c33]184 return NULL;
185 }
[f7ea5400]186
187 return buf;
188}
189
190/**
191 * Converts the calendar time to a string in format
192 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
193 * (non reentrant version)
194 *
195 * @param timep Time to convert.
196 * @return Pointer to a statically allocated buffer that stores
197 * the result, NULL in case of error.
198 */
[7f9df7b9]199char *ctime(const time_t *timep)
[f7ea5400]200{
201 static char buf[ASCTIME_BUF_LEN];
202
[7f9df7b9]203 return ctime_r(timep, buf);
[2fc5072]204}
205
[55b1efd]206/**
207 * Get clock resolution. Only CLOCK_REALTIME is supported.
[4cf8ca6]208 *
[e6165be]209 * @param clock_id Clock ID.
210 * @param res Pointer to the variable where the resolution is to be written.
211 * @return 0 on success, -1 with errno set on failure.
[4cf8ca6]212 */
[7f9df7b9]213int clock_getres(clockid_t clock_id, struct timespec *res)
[3f466c33]214{
215 assert(res != NULL);
216
217 switch (clock_id) {
218 case CLOCK_REALTIME:
219 res->tv_sec = 0;
220 res->tv_nsec = 1000; /* Microsecond resolution. */
221 return 0;
222 default:
223 errno = EINVAL;
224 return -1;
225 }
226}
227
[55b1efd]228/**
229 * Get time. Only CLOCK_REALTIME is supported.
[1b20da0]230 *
[e6165be]231 * @param clock_id ID of the clock to query.
232 * @param tp Pointer to the variable where the time is to be written.
[55b1efd]233 * @return 0 on success, -1 with errno on failure.
[4cf8ca6]234 */
[7f9df7b9]235int clock_gettime(clockid_t clock_id, struct timespec *tp)
[3f466c33]236{
[013e5d32]237 struct timeval tv;
238
[3f466c33]239 assert(tp != NULL);
240
241 switch (clock_id) {
242 case CLOCK_REALTIME:
243 gettimeofday(&tv, NULL);
244 tp->tv_sec = tv.tv_sec;
245 tp->tv_nsec = tv.tv_usec * 1000;
246 return 0;
247 default:
248 errno = EINVAL;
249 return -1;
250 }
251}
252
[55b1efd]253/**
254 * Set time on a specified clock. As HelenOS doesn't support this yet,
255 * this function always fails.
[1b20da0]256 *
[e6165be]257 * @param clock_id ID of the clock to set.
258 * @param tp Time to set.
259 * @return 0 on success, -1 with errno on failure.
[4cf8ca6]260 */
[7f9df7b9]261int clock_settime(clockid_t clock_id,
262 const struct timespec *tp)
[3f466c33]263{
264 assert(tp != NULL);
265
266 switch (clock_id) {
267 case CLOCK_REALTIME:
268 // TODO: setting clock
269 // FIXME: HelenOS doesn't actually support hardware
270 // clock yet
271 errno = EPERM;
272 return -1;
273 default:
274 errno = EINVAL;
275 return -1;
276 }
277}
278
[55b1efd]279/**
280 * Sleep on a specified clock.
[1b20da0]281 *
[e6165be]282 * @param clock_id ID of the clock to sleep on (only CLOCK_REALTIME supported).
283 * @param flags Flags (none supported).
284 * @param rqtp Sleep time.
285 * @param rmtp Remaining time is written here if sleep is interrupted.
286 * @return 0 on success, -1 with errno set on failure.
[4cf8ca6]287 */
[7f9df7b9]288int clock_nanosleep(clockid_t clock_id, int flags,
289 const struct timespec *rqtp, struct timespec *rmtp)
[3f466c33]290{
291 assert(rqtp != NULL);
292 assert(rmtp != NULL);
293
294 switch (clock_id) {
295 case CLOCK_REALTIME:
296 // TODO: interruptible sleep
297 if (rqtp->tv_sec != 0) {
[39026d7c]298 async_sleep(rqtp->tv_sec);
[3f466c33]299 }
300 if (rqtp->tv_nsec != 0) {
[39026d7c]301 async_usleep(rqtp->tv_nsec / 1000);
[3f466c33]302 }
303 return 0;
304 default:
305 errno = EINVAL;
306 return -1;
307 }
308}
309
[55b1efd]310/**
311 * Get CPU time used since the process invocation.
[06cb827]312 *
313 * @return Consumed CPU cycles by this process or -1 if not available.
[823a929]314 */
[7f9df7b9]315clock_t clock(void)
[823a929]316{
[7f9df7b9]317 clock_t total_cycles = -1;
[06cb827]318 stats_task_t *task_stats = stats_get_task(task_get_id());
319 if (task_stats) {
[7f9df7b9]320 total_cycles = (clock_t) (task_stats->kcycles +
[cb948777]321 task_stats->ucycles);
[a12f7f1]322 free(task_stats);
323 task_stats = 0;
[06cb827]324 }
325
326 return total_cycles;
[823a929]327}
328
[2fc5072]329/** @}
330 */
Note: See TracBrowser for help on using the repository browser.