Fdisk needs to unmount partition before trying to delete it
[helenos.git] / uspace / lib / posix / src / time.c
blob1e7e2eeeddcd845474806ca6b92ec19c00d8c8c8
1 /*
2 * Copyright (c) 2011 Petr Koupy
3 * Copyright (c) 2011 Jiri Zarevucky
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
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.
30 /** @addtogroup libposix
31 * @{
33 /** @file Time measurement support.
36 #include "internal/common.h"
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #include <time.h>
41 #include <ctype.h>
43 #include <errno.h>
45 #include <signal.h>
46 #include <assert.h>
48 #include <fibril.h>
49 #include <stdlib.h>
50 #include <task.h>
51 #include <stddef.h>
53 // TODO: test everything in this file
56 * In some places in this file, phrase "normalized broken-down time" is used.
57 * This means time broken down to components (year, month, day, hour, min, sec),
58 * in which every component is in its proper bounds. Non-normalized time could
59 * e.g. be 2011-54-5 29:13:-5, which would semantically mean start of year 2011
60 * + 53 months + 4 days + 29 hours + 13 minutes - 5 seconds.
63 int posix_daylight;
64 long posix_timezone;
65 char *posix_tzname[2];
67 /**
68 * Set timezone conversion information.
70 void tzset(void)
72 // TODO: read environment
73 posix_tzname[0] = (char *) "GMT";
74 posix_tzname[1] = (char *) "GMT";
75 posix_daylight = 0;
76 posix_timezone = 0;
79 /**
80 * Converts a time value to a broken-down UTC time.
82 * @param timer Time to convert.
83 * @param result Structure to store the result to.
84 * @return Value of result on success, NULL on overflow.
86 struct tm *gmtime_r(const time_t *restrict timer,
87 struct tm *restrict result)
89 if (failed(time_utc2tm(*timer, result))) {
90 return NULL;
93 return result;
96 /**
97 * Converts a time value to a broken-down UTC time.
98 * (non reentrant version)
100 * @param timep Time to convert
101 * @return Pointer to a statically allocated structure that stores
102 * the result, NULL in case of error.
104 struct tm *gmtime(const time_t *restrict timep)
106 static struct tm result;
108 return gmtime_r(timep, &result);
112 * Converts a time value to a broken-down local time.
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.
118 struct tm *localtime_r(const time_t *restrict timer,
119 struct tm *restrict result)
121 // TODO: deal with timezone
122 // currently assumes system and all times are in GMT
123 return gmtime_r(timer, result);
127 * Converts a time value to a broken-down local time.
128 * (non reentrant version)
130 * @param timep Time to convert.
131 * @return Pointer to a statically allocated structure that stores
132 * the result, NULL in case of error.
134 struct tm *localtime(const time_t *restrict timep)
136 static struct tm result;
138 return localtime_r(timep, &result);
142 * Converts broken-down time to a string in format
143 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
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.
150 char *asctime_r(const struct tm *restrict timeptr,
151 char *restrict buf)
153 time_tm2str(timeptr, buf);
154 return buf;
158 * Convers broken-down time to a string in format
159 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
160 * (non reentrant version)
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.
166 char *asctime(const struct tm *restrict timeptr)
168 static char buf[ASCTIME_BUF_LEN];
170 return asctime_r(timeptr, buf);
174 * Converts the calendar time to a string in format
175 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
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.
182 char *ctime_r(const time_t *timer, char *buf)
184 if (failed(time_local2str(*timer, buf))) {
185 return NULL;
188 return buf;
192 * Converts the calendar time to a string in format
193 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
194 * (non reentrant version)
196 * @param timep Time to convert.
197 * @return Pointer to a statically allocated buffer that stores
198 * the result, NULL in case of error.
200 char *ctime(const time_t *timep)
202 static char buf[ASCTIME_BUF_LEN];
204 return ctime_r(timep, buf);
208 * Get clock resolution. Only CLOCK_REALTIME is supported.
210 * @param clock_id Clock ID.
211 * @param res Pointer to the variable where the resolution is to be written.
212 * @return 0 on success, -1 with errno set on failure.
214 int clock_getres(clockid_t clock_id, struct timespec *res)
216 assert(res != NULL);
218 switch (clock_id) {
219 case CLOCK_REALTIME:
220 res->tv_sec = 0;
221 res->tv_nsec = USEC2NSEC(1); /* Microsecond resolution. */
222 return 0;
223 default:
224 errno = EINVAL;
225 return -1;
230 * Get time. Only CLOCK_REALTIME is supported.
232 * @param clock_id ID of the clock to query.
233 * @param tp Pointer to the variable where the time is to be written.
234 * @return 0 on success, -1 with errno on failure.
236 int clock_gettime(clockid_t clock_id, struct timespec *tp)
238 struct timeval tv;
240 assert(tp != NULL);
242 switch (clock_id) {
243 case CLOCK_REALTIME:
244 gettimeofday(&tv, NULL);
245 tp->tv_sec = tv.tv_sec;
246 tp->tv_nsec = USEC2NSEC(tv.tv_usec);
247 return 0;
248 default:
249 errno = EINVAL;
250 return -1;
255 * Set time on a specified clock. As HelenOS doesn't support this yet,
256 * this function always fails.
258 * @param clock_id ID of the clock to set.
259 * @param tp Time to set.
260 * @return 0 on success, -1 with errno on failure.
262 int clock_settime(clockid_t clock_id,
263 const struct timespec *tp)
265 assert(tp != NULL);
267 switch (clock_id) {
268 case CLOCK_REALTIME:
269 // TODO: setting clock
270 // FIXME: HelenOS doesn't actually support hardware
271 // clock yet
272 errno = EPERM;
273 return -1;
274 default:
275 errno = EINVAL;
276 return -1;
281 * Sleep on a specified clock.
283 * @param clock_id ID of the clock to sleep on (only CLOCK_REALTIME supported).
284 * @param flags Flags (none supported).
285 * @param rqtp Sleep time.
286 * @param rmtp Remaining time is written here if sleep is interrupted.
287 * @return 0 on success, -1 with errno set on failure.
289 int clock_nanosleep(clockid_t clock_id, int flags,
290 const struct timespec *rqtp, struct timespec *rmtp)
292 assert(rqtp != NULL);
293 assert(rmtp != NULL);
295 switch (clock_id) {
296 case CLOCK_REALTIME:
297 // TODO: interruptible sleep
298 if (rqtp->tv_sec != 0) {
299 fibril_sleep(rqtp->tv_sec);
301 if (rqtp->tv_nsec != 0) {
302 fibril_usleep(NSEC2USEC(rqtp->tv_nsec));
304 return 0;
305 default:
306 errno = EINVAL;
307 return -1;
311 int gettimeofday(struct timeval *tv, void *tz)
313 struct timespec ts;
315 getrealtime(&ts);
316 tv->tv_sec = ts.tv_sec;
317 tv->tv_usec = NSEC2USEC(ts.tv_nsec);
319 return 0;
322 /** @}