[0b99e40] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[c2b0e10] | 3 | * Copyright (c) 2011 Petr Koupy
|
---|
| 4 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
[0b99e40] | 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[b2951e2] | 29 | */
|
---|
| 30 |
|
---|
[a46da63] | 31 | /** @addtogroup libc
|
---|
[b2951e2] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
[22e6802] | 35 | */
|
---|
[0b99e40] | 36 |
|
---|
[6119f24] | 37 | #include <time.h>
|
---|
[3e6a98c5] | 38 | #include <stdbool.h>
|
---|
[05882233] | 39 | #include <barrier.h>
|
---|
[2c577e0b] | 40 | #include <macros.h>
|
---|
[6119f24] | 41 | #include <errno.h>
|
---|
| 42 | #include <sysinfo.h>
|
---|
| 43 | #include <as.h>
|
---|
| 44 | #include <ddi.h>
|
---|
[d9ece1cb] | 45 | #include <libc.h>
|
---|
[c2b0e10] | 46 | #include <stdint.h>
|
---|
| 47 | #include <stdio.h>
|
---|
| 48 | #include <ctype.h>
|
---|
[f7e69f5] | 49 | #include <assert.h>
|
---|
[3a58347] | 50 | #include <loc.h>
|
---|
| 51 | #include <device/clock_dev.h>
|
---|
[c61d34b] | 52 |
|
---|
[1ab8539] | 53 | #define ASCTIME_BUF_LEN 26
|
---|
| 54 |
|
---|
| 55 | #define HOURS_PER_DAY 24
|
---|
| 56 | #define MINS_PER_HOUR 60
|
---|
| 57 | #define SECS_PER_MIN 60
|
---|
[205f1add] | 58 | #define NSECS_PER_SEC 1000000000ll
|
---|
[1ab8539] | 59 | #define MINS_PER_DAY (MINS_PER_HOUR * HOURS_PER_DAY)
|
---|
| 60 | #define SECS_PER_HOUR (SECS_PER_MIN * MINS_PER_HOUR)
|
---|
| 61 | #define SECS_PER_DAY (SECS_PER_HOUR * HOURS_PER_DAY)
|
---|
[8219eb9] | 62 |
|
---|
[2c577e0b] | 63 | /** Pointer to kernel shared variables with time */
|
---|
[0b99e40] | 64 | struct {
|
---|
[2d1fde3b] | 65 | volatile sysarg_t seconds1;
|
---|
[0b99e40] | 66 | volatile sysarg_t useconds;
|
---|
[2d1fde3b] | 67 | volatile sysarg_t seconds2;
|
---|
[0b99e40] | 68 | } *ktime = NULL;
|
---|
| 69 |
|
---|
[1ab8539] | 70 | static async_sess_t *clock_conn = NULL;
|
---|
[c2b0e10] | 71 |
|
---|
[1ab8539] | 72 | /** Check whether the year is a leap year.
|
---|
[c2b0e10] | 73 | *
|
---|
| 74 | * @param year Year since 1900 (e.g. for 1970, the value is 70).
|
---|
[1ab8539] | 75 | *
|
---|
[c2b0e10] | 76 | * @return true if year is a leap year, false otherwise
|
---|
[1ab8539] | 77 | *
|
---|
[c2b0e10] | 78 | */
|
---|
[1ab8539] | 79 | static bool is_leap_year(time_t year)
|
---|
[c2b0e10] | 80 | {
|
---|
| 81 | year += 1900;
|
---|
[a35b458] | 82 |
|
---|
[c2b0e10] | 83 | if (year % 400 == 0)
|
---|
| 84 | return true;
|
---|
[a35b458] | 85 |
|
---|
[c2b0e10] | 86 | if (year % 100 == 0)
|
---|
| 87 | return false;
|
---|
[a35b458] | 88 |
|
---|
[c2b0e10] | 89 | if (year % 4 == 0)
|
---|
| 90 | return true;
|
---|
[a35b458] | 91 |
|
---|
[c2b0e10] | 92 | return false;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[1ab8539] | 95 | /** How many days there are in the given month
|
---|
| 96 | *
|
---|
| 97 | * Return how many days there are in the given month of the given year.
|
---|
[c2b0e10] | 98 | * Note that year is only taken into account if month is February.
|
---|
| 99 | *
|
---|
| 100 | * @param year Year since 1900 (can be negative).
|
---|
[1ab8539] | 101 | * @param mon Month of the year. 0 for January, 11 for December.
|
---|
| 102 | *
|
---|
[c2b0e10] | 103 | * @return Number of days in the specified month.
|
---|
[1ab8539] | 104 | *
|
---|
[c2b0e10] | 105 | */
|
---|
[1ab8539] | 106 | static int days_in_month(time_t year, time_t mon)
|
---|
[c2b0e10] | 107 | {
|
---|
[1ab8539] | 108 | assert(mon >= 0);
|
---|
| 109 | assert(mon <= 11);
|
---|
[a35b458] | 110 |
|
---|
[1ab8539] | 111 | static int month_days[] = {
|
---|
| 112 | 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
---|
| 113 | };
|
---|
[a35b458] | 114 |
|
---|
[c2b0e10] | 115 | if (mon == 1) {
|
---|
[1ab8539] | 116 | /* February */
|
---|
[c2b0e10] | 117 | year += 1900;
|
---|
[1ab8539] | 118 | return is_leap_year(year) ? 29 : 28;
|
---|
[c2b0e10] | 119 | }
|
---|
[a35b458] | 120 |
|
---|
[1ab8539] | 121 | return month_days[mon];
|
---|
[c2b0e10] | 122 | }
|
---|
| 123 |
|
---|
[1ab8539] | 124 | /** Which day of that year it is.
|
---|
| 125 | *
|
---|
| 126 | * For specified year, month and day of month, return which day of that year
|
---|
[c2b0e10] | 127 | * it is.
|
---|
| 128 | *
|
---|
| 129 | * For example, given date 2011-01-03, the corresponding expression is:
|
---|
[1ab8539] | 130 | * day_of_year(111, 0, 3) == 2
|
---|
[c2b0e10] | 131 | *
|
---|
| 132 | * @param year Year (year 1900 = 0, can be negative).
|
---|
[1ab8539] | 133 | * @param mon Month (January = 0).
|
---|
[c2b0e10] | 134 | * @param mday Day of month (First day is 1).
|
---|
[1ab8539] | 135 | *
|
---|
[c2b0e10] | 136 | * @return Day of year (First day is 0).
|
---|
[1ab8539] | 137 | *
|
---|
[c2b0e10] | 138 | */
|
---|
[1ab8539] | 139 | static int day_of_year(time_t year, time_t mon, time_t mday)
|
---|
[c2b0e10] | 140 | {
|
---|
[1ab8539] | 141 | static int mdays[] = {
|
---|
| 142 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
|
---|
| 143 | };
|
---|
[a35b458] | 144 |
|
---|
[1ab8539] | 145 | static int leap_mdays[] = {
|
---|
| 146 | 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335
|
---|
| 147 | };
|
---|
[a35b458] | 148 |
|
---|
[1ab8539] | 149 | return (is_leap_year(year) ? leap_mdays[mon] : mdays[mon]) + mday - 1;
|
---|
[c2b0e10] | 150 | }
|
---|
| 151 |
|
---|
[1ab8539] | 152 | /** Integer division that rounds to negative infinity.
|
---|
| 153 | *
|
---|
| 154 | * Used by some functions in this module.
|
---|
[c2b0e10] | 155 | *
|
---|
| 156 | * @param op1 Dividend.
|
---|
| 157 | * @param op2 Divisor.
|
---|
[1ab8539] | 158 | *
|
---|
[c2b0e10] | 159 | * @return Rounded quotient.
|
---|
[1ab8539] | 160 | *
|
---|
[c2b0e10] | 161 | */
|
---|
[1ab8539] | 162 | static time_t floor_div(time_t op1, time_t op2)
|
---|
[c2b0e10] | 163 | {
|
---|
[1ab8539] | 164 | if ((op1 >= 0) || (op1 % op2 == 0))
|
---|
[c2b0e10] | 165 | return op1 / op2;
|
---|
[a35b458] | 166 |
|
---|
[1ab8539] | 167 | return op1 / op2 - 1;
|
---|
[c2b0e10] | 168 | }
|
---|
| 169 |
|
---|
[1ab8539] | 170 | /** Modulo that rounds to negative infinity.
|
---|
| 171 | *
|
---|
| 172 | * Used by some functions in this module.
|
---|
[c2b0e10] | 173 | *
|
---|
| 174 | * @param op1 Dividend.
|
---|
| 175 | * @param op2 Divisor.
|
---|
[1ab8539] | 176 | *
|
---|
[c2b0e10] | 177 | * @return Remainder.
|
---|
[1ab8539] | 178 | *
|
---|
[c2b0e10] | 179 | */
|
---|
[1ab8539] | 180 | static time_t floor_mod(time_t op1, time_t op2)
|
---|
[c2b0e10] | 181 | {
|
---|
[1ab8539] | 182 | time_t div = floor_div(op1, op2);
|
---|
[a35b458] | 183 |
|
---|
[1ab8539] | 184 | /*
|
---|
| 185 | * (a / b) * b + a % b == a
|
---|
| 186 | * Thus: a % b == a - (a / b) * b
|
---|
| 187 | */
|
---|
[a35b458] | 188 |
|
---|
[1ab8539] | 189 | time_t result = op1 - div * op2;
|
---|
[a35b458] | 190 |
|
---|
[1ab8539] | 191 | /* Some paranoid checking to ensure there is mistake here. */
|
---|
[c2b0e10] | 192 | assert(result >= 0);
|
---|
| 193 | assert(result < op2);
|
---|
| 194 | assert(div * op2 + result == op1);
|
---|
[a35b458] | 195 |
|
---|
[c2b0e10] | 196 | return result;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[1ab8539] | 199 | /** Number of days since the Epoch.
|
---|
| 200 | *
|
---|
[c2b0e10] | 201 | * Epoch is 1970-01-01, which is also equal to day 0.
|
---|
| 202 | *
|
---|
| 203 | * @param year Year (year 1900 = 0, may be negative).
|
---|
[1ab8539] | 204 | * @param mon Month (January = 0).
|
---|
[c2b0e10] | 205 | * @param mday Day of month (first day = 1).
|
---|
[1ab8539] | 206 | *
|
---|
[c2b0e10] | 207 | * @return Number of days since the Epoch.
|
---|
[1ab8539] | 208 | *
|
---|
[c2b0e10] | 209 | */
|
---|
[1ab8539] | 210 | static time_t days_since_epoch(time_t year, time_t mon, time_t mday)
|
---|
[c2b0e10] | 211 | {
|
---|
[1ab8539] | 212 | return (year - 70) * 365 + floor_div(year - 69, 4) -
|
---|
| 213 | floor_div(year - 1, 100) + floor_div(year + 299, 400) +
|
---|
| 214 | day_of_year(year, mon, mday);
|
---|
[c2b0e10] | 215 | }
|
---|
| 216 |
|
---|
[1ab8539] | 217 | /** Seconds since the Epoch.
|
---|
| 218 | *
|
---|
| 219 | * See also days_since_epoch().
|
---|
| 220 | *
|
---|
[c2b0e10] | 221 | * @param tm Normalized broken-down time.
|
---|
[1ab8539] | 222 | *
|
---|
[c2b0e10] | 223 | * @return Number of seconds since the epoch, not counting leap seconds.
|
---|
[1ab8539] | 224 | *
|
---|
[c2b0e10] | 225 | */
|
---|
[1ab8539] | 226 | static time_t secs_since_epoch(const struct tm *tm)
|
---|
[c2b0e10] | 227 | {
|
---|
[1ab8539] | 228 | return days_since_epoch(tm->tm_year, tm->tm_mon, tm->tm_mday) *
|
---|
[c2b0e10] | 229 | SECS_PER_DAY + tm->tm_hour * SECS_PER_HOUR +
|
---|
| 230 | tm->tm_min * SECS_PER_MIN + tm->tm_sec;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[1ab8539] | 233 | /** Which day of week the specified date is.
|
---|
| 234 | *
|
---|
[c2b0e10] | 235 | * @param year Year (year 1900 = 0).
|
---|
[1ab8539] | 236 | * @param mon Month (January = 0).
|
---|
[c2b0e10] | 237 | * @param mday Day of month (first = 1).
|
---|
[1ab8539] | 238 | *
|
---|
[c2b0e10] | 239 | * @return Day of week (Sunday = 0).
|
---|
[1ab8539] | 240 | *
|
---|
[c2b0e10] | 241 | */
|
---|
[1ab8539] | 242 | static time_t day_of_week(time_t year, time_t mon, time_t mday)
|
---|
[c2b0e10] | 243 | {
|
---|
| 244 | /* 1970-01-01 is Thursday */
|
---|
[1ab8539] | 245 | return floor_mod(days_since_epoch(year, mon, mday) + 4, 7);
|
---|
[c2b0e10] | 246 | }
|
---|
| 247 |
|
---|
[1ab8539] | 248 | /** Normalize the broken-down time.
|
---|
| 249 | *
|
---|
| 250 | * Optionally add specified amount of seconds.
|
---|
| 251 | *
|
---|
[7f9d97f3] | 252 | * @param tm Broken-down time to normalize.
|
---|
[205f1add] | 253 | * @param ts Timespec to add.
|
---|
[1ab8539] | 254 | *
|
---|
[c2b0e10] | 255 | * @return 0 on success, -1 on overflow
|
---|
[1ab8539] | 256 | *
|
---|
[c2b0e10] | 257 | */
|
---|
[205f1add] | 258 | static int normalize_tm_ts(struct tm *tm, const struct timespec *ts)
|
---|
[c2b0e10] | 259 | {
|
---|
| 260 | // TODO: DST correction
|
---|
[a35b458] | 261 |
|
---|
[c2b0e10] | 262 | /* Set initial values. */
|
---|
[205f1add] | 263 | time_t nsec = tm->tm_nsec + ts->tv_nsec;
|
---|
| 264 | time_t sec = tm->tm_sec + ts->tv_sec;
|
---|
[c2b0e10] | 265 | time_t min = tm->tm_min;
|
---|
| 266 | time_t hour = tm->tm_hour;
|
---|
| 267 | time_t day = tm->tm_mday - 1;
|
---|
| 268 | time_t mon = tm->tm_mon;
|
---|
| 269 | time_t year = tm->tm_year;
|
---|
[a35b458] | 270 |
|
---|
[c2b0e10] | 271 | /* Adjust time. */
|
---|
[205f1add] | 272 | sec += floor_div(nsec, NSECS_PER_SEC);
|
---|
| 273 | nsec = floor_mod(nsec, NSECS_PER_SEC);
|
---|
[1ab8539] | 274 | min += floor_div(sec, SECS_PER_MIN);
|
---|
| 275 | sec = floor_mod(sec, SECS_PER_MIN);
|
---|
| 276 | hour += floor_div(min, MINS_PER_HOUR);
|
---|
| 277 | min = floor_mod(min, MINS_PER_HOUR);
|
---|
| 278 | day += floor_div(hour, HOURS_PER_DAY);
|
---|
| 279 | hour = floor_mod(hour, HOURS_PER_DAY);
|
---|
[a35b458] | 280 |
|
---|
[c2b0e10] | 281 | /* Adjust month. */
|
---|
[1ab8539] | 282 | year += floor_div(mon, 12);
|
---|
| 283 | mon = floor_mod(mon, 12);
|
---|
[a35b458] | 284 |
|
---|
[c2b0e10] | 285 | /* Now the difficult part - days of month. */
|
---|
[a35b458] | 286 |
|
---|
[c2b0e10] | 287 | /* First, deal with whole cycles of 400 years = 146097 days. */
|
---|
[1ab8539] | 288 | year += floor_div(day, 146097) * 400;
|
---|
| 289 | day = floor_mod(day, 146097);
|
---|
[a35b458] | 290 |
|
---|
[c2b0e10] | 291 | /* Then, go in one year steps. */
|
---|
| 292 | if (mon <= 1) {
|
---|
| 293 | /* January and February. */
|
---|
| 294 | while (day > 365) {
|
---|
[1ab8539] | 295 | day -= is_leap_year(year) ? 366 : 365;
|
---|
[c2b0e10] | 296 | year++;
|
---|
| 297 | }
|
---|
| 298 | } else {
|
---|
| 299 | /* Rest of the year. */
|
---|
| 300 | while (day > 365) {
|
---|
[1ab8539] | 301 | day -= is_leap_year(year + 1) ? 366 : 365;
|
---|
[c2b0e10] | 302 | year++;
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
[a35b458] | 305 |
|
---|
[c2b0e10] | 306 | /* Finally, finish it off month per month. */
|
---|
[1ab8539] | 307 | while (day >= days_in_month(year, mon)) {
|
---|
| 308 | day -= days_in_month(year, mon);
|
---|
[c2b0e10] | 309 | mon++;
|
---|
[a35b458] | 310 |
|
---|
[c2b0e10] | 311 | if (mon >= 12) {
|
---|
| 312 | mon -= 12;
|
---|
| 313 | year++;
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
[a35b458] | 316 |
|
---|
[c2b0e10] | 317 | /* Calculate the remaining two fields. */
|
---|
[1ab8539] | 318 | tm->tm_yday = day_of_year(year, mon, day + 1);
|
---|
| 319 | tm->tm_wday = day_of_week(year, mon, day + 1);
|
---|
[a35b458] | 320 |
|
---|
[c2b0e10] | 321 | /* And put the values back to the struct. */
|
---|
[205f1add] | 322 | tm->tm_nsec = (int) nsec;
|
---|
[c2b0e10] | 323 | tm->tm_sec = (int) sec;
|
---|
| 324 | tm->tm_min = (int) min;
|
---|
| 325 | tm->tm_hour = (int) hour;
|
---|
| 326 | tm->tm_mday = (int) day + 1;
|
---|
| 327 | tm->tm_mon = (int) mon;
|
---|
[a35b458] | 328 |
|
---|
[1ab8539] | 329 | /* Casts to work around POSIX brain-damage. */
|
---|
| 330 | if (year > ((int) INT_MAX) || year < ((int) INT_MIN)) {
|
---|
| 331 | tm->tm_year = (year < 0) ? ((int) INT_MIN) : ((int) INT_MAX);
|
---|
[c2b0e10] | 332 | return -1;
|
---|
| 333 | }
|
---|
[a35b458] | 334 |
|
---|
[c2b0e10] | 335 | tm->tm_year = (int) year;
|
---|
| 336 | return 0;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
[7f9d97f3] | 339 | static int normalize_tm_time(struct tm *tm, time_t time)
|
---|
| 340 | {
|
---|
[205f1add] | 341 | struct timespec ts = {
|
---|
[7f9d97f3] | 342 | .tv_sec = time,
|
---|
[205f1add] | 343 | .tv_nsec = 0
|
---|
[7f9d97f3] | 344 | };
|
---|
| 345 |
|
---|
[205f1add] | 346 | return normalize_tm_ts(tm, &ts);
|
---|
[7f9d97f3] | 347 | }
|
---|
| 348 |
|
---|
| 349 |
|
---|
[1ab8539] | 350 | /** Which day the week-based year starts on.
|
---|
| 351 | *
|
---|
| 352 | * Relative to the first calendar day. E.g. if the year starts
|
---|
| 353 | * on December 31st, the return value is -1.
|
---|
[c2b0e10] | 354 | *
|
---|
| 355 | * @param Year since 1900.
|
---|
[1ab8539] | 356 | *
|
---|
[c2b0e10] | 357 | * @return Offset of week-based year relative to calendar year.
|
---|
[1ab8539] | 358 | *
|
---|
[c2b0e10] | 359 | */
|
---|
[1ab8539] | 360 | static int wbyear_offset(int year)
|
---|
[c2b0e10] | 361 | {
|
---|
[1ab8539] | 362 | int start_wday = day_of_week(year, 0, 1);
|
---|
[a35b458] | 363 |
|
---|
[1ab8539] | 364 | return floor_mod(4 - start_wday, 7) - 3;
|
---|
[c2b0e10] | 365 | }
|
---|
| 366 |
|
---|
[1ab8539] | 367 | /** Week-based year of the specified time.
|
---|
[c2b0e10] | 368 | *
|
---|
| 369 | * @param tm Normalized broken-down time.
|
---|
[1ab8539] | 370 | *
|
---|
[c2b0e10] | 371 | * @return Week-based year.
|
---|
[1ab8539] | 372 | *
|
---|
[c2b0e10] | 373 | */
|
---|
[1ab8539] | 374 | static int wbyear(const struct tm *tm)
|
---|
[c2b0e10] | 375 | {
|
---|
[1ab8539] | 376 | int day = tm->tm_yday - wbyear_offset(tm->tm_year);
|
---|
[a35b458] | 377 |
|
---|
[c2b0e10] | 378 | if (day < 0) {
|
---|
| 379 | /* Last week of previous year. */
|
---|
| 380 | return tm->tm_year - 1;
|
---|
| 381 | }
|
---|
[a35b458] | 382 |
|
---|
[1ab8539] | 383 | if (day > 364 + is_leap_year(tm->tm_year)) {
|
---|
[c2b0e10] | 384 | /* First week of next year. */
|
---|
| 385 | return tm->tm_year + 1;
|
---|
| 386 | }
|
---|
[a35b458] | 387 |
|
---|
[c2b0e10] | 388 | /* All the other days are in the calendar year. */
|
---|
| 389 | return tm->tm_year;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
[1ab8539] | 392 | /** Week number of the year (assuming weeks start on Sunday).
|
---|
| 393 | *
|
---|
[c2b0e10] | 394 | * The first Sunday of January is the first day of week 1;
|
---|
| 395 | * days in the new year before this are in week 0.
|
---|
| 396 | *
|
---|
| 397 | * @param tm Normalized broken-down time.
|
---|
[1ab8539] | 398 | *
|
---|
[c2b0e10] | 399 | * @return The week number (0 - 53).
|
---|
[1ab8539] | 400 | *
|
---|
[c2b0e10] | 401 | */
|
---|
[1ab8539] | 402 | static int sun_week_number(const struct tm *tm)
|
---|
[c2b0e10] | 403 | {
|
---|
[1ab8539] | 404 | int first_day = (7 - day_of_week(tm->tm_year, 0, 1)) % 7;
|
---|
[a35b458] | 405 |
|
---|
[c2b0e10] | 406 | return (tm->tm_yday - first_day + 7) / 7;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
[1ab8539] | 409 | /** Week number of the year (assuming weeks start on Monday).
|
---|
| 410 | *
|
---|
| 411 | * If the week containing January 1st has four or more days
|
---|
| 412 | * in the new year, then it is considered week 1. Otherwise,
|
---|
| 413 | * it is the last week of the previous year, and the next week
|
---|
| 414 | * is week 1. Both January 4th and the first Thursday
|
---|
[c2b0e10] | 415 | * of January are always in week 1.
|
---|
| 416 | *
|
---|
| 417 | * @param tm Normalized broken-down time.
|
---|
[1ab8539] | 418 | *
|
---|
[c2b0e10] | 419 | * @return The week number (1 - 53).
|
---|
[1ab8539] | 420 | *
|
---|
[c2b0e10] | 421 | */
|
---|
[1ab8539] | 422 | static int iso_week_number(const struct tm *tm)
|
---|
[c2b0e10] | 423 | {
|
---|
[1ab8539] | 424 | int day = tm->tm_yday - wbyear_offset(tm->tm_year);
|
---|
[a35b458] | 425 |
|
---|
[c2b0e10] | 426 | if (day < 0) {
|
---|
| 427 | /* Last week of previous year. */
|
---|
| 428 | return 53;
|
---|
| 429 | }
|
---|
[a35b458] | 430 |
|
---|
[1ab8539] | 431 | if (day > 364 + is_leap_year(tm->tm_year)) {
|
---|
[c2b0e10] | 432 | /* First week of next year. */
|
---|
| 433 | return 1;
|
---|
| 434 | }
|
---|
[a35b458] | 435 |
|
---|
[c2b0e10] | 436 | /* All the other days give correct answer. */
|
---|
| 437 | return (day / 7 + 1);
|
---|
| 438 | }
|
---|
| 439 |
|
---|
[1ab8539] | 440 | /** Week number of the year (assuming weeks start on Monday).
|
---|
| 441 | *
|
---|
[c2b0e10] | 442 | * The first Monday of January is the first day of week 1;
|
---|
[1ab8539] | 443 | * days in the new year before this are in week 0.
|
---|
[c2b0e10] | 444 | *
|
---|
| 445 | * @param tm Normalized broken-down time.
|
---|
[1ab8539] | 446 | *
|
---|
[c2b0e10] | 447 | * @return The week number (0 - 53).
|
---|
[1ab8539] | 448 | *
|
---|
[c2b0e10] | 449 | */
|
---|
[1ab8539] | 450 | static int mon_week_number(const struct tm *tm)
|
---|
[c2b0e10] | 451 | {
|
---|
[1ab8539] | 452 | int first_day = (1 - day_of_week(tm->tm_year, 0, 1)) % 7;
|
---|
[a35b458] | 453 |
|
---|
[c2b0e10] | 454 | return (tm->tm_yday - first_day + 7) / 7;
|
---|
| 455 | }
|
---|
| 456 |
|
---|
[205f1add] | 457 | static void ts_normalize(struct timespec *ts)
|
---|
[7f9d97f3] | 458 | {
|
---|
[205f1add] | 459 | while (ts->tv_nsec >= NSECS_PER_SEC) {
|
---|
| 460 | ts->tv_sec++;
|
---|
| 461 | ts->tv_nsec -= NSECS_PER_SEC;
|
---|
[7f9d97f3] | 462 | }
|
---|
[205f1add] | 463 | while (ts->tv_nsec < 0) {
|
---|
| 464 | ts->tv_sec--;
|
---|
| 465 | ts->tv_nsec += NSECS_PER_SEC;
|
---|
[7f9d97f3] | 466 | }
|
---|
| 467 | }
|
---|
| 468 |
|
---|
[205f1add] | 469 | /** Add nanoseconds to given timespec.
|
---|
[daa90e8] | 470 | *
|
---|
[205f1add] | 471 | * @param ts Destination timespec.
|
---|
| 472 | * @param nsecs Number of nanoseconds to add.
|
---|
[2c577e0b] | 473 | *
|
---|
[daa90e8] | 474 | */
|
---|
[205f1add] | 475 | void ts_add_diff(struct timespec *ts, nsec_t nsecs)
|
---|
[daa90e8] | 476 | {
|
---|
[205f1add] | 477 | ts->tv_sec += nsecs / NSECS_PER_SEC;
|
---|
| 478 | ts->tv_nsec += nsecs % NSECS_PER_SEC;
|
---|
| 479 | ts_normalize(ts);
|
---|
[7f9d97f3] | 480 | }
|
---|
| 481 |
|
---|
[205f1add] | 482 | /** Add two timespecs.
|
---|
[7f9d97f3] | 483 | *
|
---|
[205f1add] | 484 | * @param ts1 First timespec.
|
---|
| 485 | * @param ts2 Second timespec.
|
---|
[7f9d97f3] | 486 | */
|
---|
[205f1add] | 487 | void ts_add(struct timespec *ts1, const struct timespec *ts2)
|
---|
[7f9d97f3] | 488 | {
|
---|
[205f1add] | 489 | ts1->tv_sec += ts2->tv_sec;
|
---|
| 490 | ts1->tv_nsec += ts2->tv_nsec;
|
---|
| 491 | ts_normalize(ts1);
|
---|
[daa90e8] | 492 | }
|
---|
| 493 |
|
---|
[205f1add] | 494 | /** Subtract two timespecs.
|
---|
[daa90e8] | 495 | *
|
---|
[205f1add] | 496 | * @param ts1 First timespec.
|
---|
| 497 | * @param ts2 Second timespec.
|
---|
[2c577e0b] | 498 | *
|
---|
[205f1add] | 499 | * @return Difference between ts1 and ts2 (ts1 - ts2) in nanoseconds.
|
---|
[daa90e8] | 500 | *
|
---|
| 501 | */
|
---|
[205f1add] | 502 | nsec_t ts_sub_diff(const struct timespec *ts1, const struct timespec *ts2)
|
---|
[daa90e8] | 503 | {
|
---|
[205f1add] | 504 | return (nsec_t) (ts1->tv_nsec - ts2->tv_nsec) +
|
---|
| 505 | SEC2NSEC((ts1->tv_sec - ts2->tv_sec));
|
---|
[7f9d97f3] | 506 | }
|
---|
| 507 |
|
---|
[205f1add] | 508 | /** Subtract two timespecs.
|
---|
[7f9d97f3] | 509 | *
|
---|
[205f1add] | 510 | * @param ts1 First timespec.
|
---|
| 511 | * @param ts2 Second timespec.
|
---|
[7f9d97f3] | 512 | *
|
---|
| 513 | */
|
---|
[205f1add] | 514 | void ts_sub(struct timespec *ts1, const struct timespec *ts2)
|
---|
[7f9d97f3] | 515 | {
|
---|
[205f1add] | 516 | ts1->tv_sec -= ts2->tv_sec;
|
---|
| 517 | ts1->tv_nsec -= ts2->tv_nsec;
|
---|
| 518 | ts_normalize(ts1);
|
---|
[daa90e8] | 519 | }
|
---|
| 520 |
|
---|
[205f1add] | 521 | /** Decide if one timespec is greater than the other.
|
---|
[daa90e8] | 522 | *
|
---|
[205f1add] | 523 | * @param ts1 First timespec.
|
---|
| 524 | * @param ts2 Second timespec.
|
---|
[2c577e0b] | 525 | *
|
---|
[205f1add] | 526 | * @return True if ts1 is greater than ts2.
|
---|
| 527 | * @return False otherwise.
|
---|
[daa90e8] | 528 | *
|
---|
| 529 | */
|
---|
[205f1add] | 530 | bool ts_gt(const struct timespec *ts1, const struct timespec *ts2)
|
---|
[daa90e8] | 531 | {
|
---|
[205f1add] | 532 | if (ts1->tv_sec > ts2->tv_sec)
|
---|
[2c577e0b] | 533 | return true;
|
---|
[a35b458] | 534 |
|
---|
[205f1add] | 535 | if ((ts1->tv_sec == ts2->tv_sec) && (ts1->tv_nsec > ts2->tv_nsec))
|
---|
[2c577e0b] | 536 | return true;
|
---|
[a35b458] | 537 |
|
---|
[2c577e0b] | 538 | return false;
|
---|
[daa90e8] | 539 | }
|
---|
| 540 |
|
---|
[205f1add] | 541 | /** Decide if one timespec is greater than or equal to the other.
|
---|
[daa90e8] | 542 | *
|
---|
[205f1add] | 543 | * @param ts1 First timespec.
|
---|
| 544 | * @param ts2 Second timespec.
|
---|
[2c577e0b] | 545 | *
|
---|
[205f1add] | 546 | * @return True if ts1 is greater than or equal to ts2.
|
---|
| 547 | * @return False otherwise.
|
---|
[daa90e8] | 548 | *
|
---|
| 549 | */
|
---|
[205f1add] | 550 | bool ts_gteq(const struct timespec *ts1, const struct timespec *ts2)
|
---|
[daa90e8] | 551 | {
|
---|
[205f1add] | 552 | if (ts1->tv_sec > ts2->tv_sec)
|
---|
[2c577e0b] | 553 | return true;
|
---|
[a35b458] | 554 |
|
---|
[205f1add] | 555 | if ((ts1->tv_sec == ts2->tv_sec) && (ts1->tv_nsec >= ts2->tv_nsec))
|
---|
[2c577e0b] | 556 | return true;
|
---|
[a35b458] | 557 |
|
---|
[2c577e0b] | 558 | return false;
|
---|
[daa90e8] | 559 | }
|
---|
| 560 |
|
---|
[205f1add] | 561 | /** Get real time from a RTC service.
|
---|
[2c577e0b] | 562 | *
|
---|
[205f1add] | 563 | * @param[out] ts Timespec to hold time read from the RTC service (if
|
---|
| 564 | * available). If no such service exists, the returned time
|
---|
| 565 | * corresponds to system uptime.
|
---|
[0b99e40] | 566 | */
|
---|
[205f1add] | 567 | void getrealtime(struct timespec *ts)
|
---|
[3a58347] | 568 | {
|
---|
| 569 | if (clock_conn == NULL) {
|
---|
[1ab8539] | 570 | category_id_t cat_id;
|
---|
[b7fd2a0] | 571 | errno_t rc = loc_category_get_id("clock", &cat_id, IPC_FLAG_BLOCKING);
|
---|
[3a58347] | 572 | if (rc != EOK)
|
---|
[1ab8539] | 573 | goto fallback;
|
---|
[a35b458] | 574 |
|
---|
[1ab8539] | 575 | service_id_t *svc_ids;
|
---|
| 576 | size_t svc_cnt;
|
---|
[3a58347] | 577 | rc = loc_category_get_svcs(cat_id, &svc_ids, &svc_cnt);
|
---|
| 578 | if (rc != EOK)
|
---|
[1ab8539] | 579 | goto fallback;
|
---|
[a35b458] | 580 |
|
---|
[3a58347] | 581 | if (svc_cnt == 0)
|
---|
[1ab8539] | 582 | goto fallback;
|
---|
[a35b458] | 583 |
|
---|
[1ab8539] | 584 | char *svc_name;
|
---|
[3a58347] | 585 | rc = loc_service_get_name(svc_ids[0], &svc_name);
|
---|
[1ab8539] | 586 | free(svc_ids);
|
---|
[3a58347] | 587 | if (rc != EOK)
|
---|
[1ab8539] | 588 | goto fallback;
|
---|
[a35b458] | 589 |
|
---|
[1ab8539] | 590 | service_id_t svc_id;
|
---|
[3a58347] | 591 | rc = loc_service_get_id(svc_name, &svc_id, 0);
|
---|
[1ab8539] | 592 | free(svc_name);
|
---|
[3a58347] | 593 | if (rc != EOK)
|
---|
[1ab8539] | 594 | goto fallback;
|
---|
[a35b458] | 595 |
|
---|
[f9b2cb4c] | 596 | clock_conn = loc_service_connect(svc_id, INTERFACE_DDF,
|
---|
| 597 | IPC_FLAG_BLOCKING);
|
---|
[3a58347] | 598 | if (!clock_conn)
|
---|
[1ab8539] | 599 | goto fallback;
|
---|
[3a58347] | 600 | }
|
---|
[a35b458] | 601 |
|
---|
[1ab8539] | 602 | struct tm time;
|
---|
[b7fd2a0] | 603 | errno_t rc = clock_dev_time_get(clock_conn, &time);
|
---|
[3a58347] | 604 | if (rc != EOK)
|
---|
[1ab8539] | 605 | goto fallback;
|
---|
[a35b458] | 606 |
|
---|
[205f1add] | 607 | ts->tv_nsec = time.tm_nsec;
|
---|
| 608 | ts->tv_sec = mktime(&time);
|
---|
[a35b458] | 609 |
|
---|
[1ab8539] | 610 | return;
|
---|
[a35b458] | 611 |
|
---|
[1ab8539] | 612 | fallback:
|
---|
[205f1add] | 613 | getuptime(ts);
|
---|
[3a58347] | 614 | }
|
---|
| 615 |
|
---|
[205f1add] | 616 | /** Get system uptime.
|
---|
| 617 | *
|
---|
| 618 | * @param[out] ts Timespec to hold time current uptime.
|
---|
| 619 | *
|
---|
| 620 | * The time variables are memory mapped (read-only) from kernel which
|
---|
| 621 | * updates them periodically.
|
---|
| 622 | *
|
---|
| 623 | * As it is impossible to read 2 values atomically, we use a trick:
|
---|
| 624 | * First we read the seconds, then we read the microseconds, then we
|
---|
| 625 | * read the seconds again. If a second elapsed in the meantime, set
|
---|
| 626 | * the microseconds to zero.
|
---|
| 627 | *
|
---|
| 628 | * This assures that the values returned by two subsequent calls
|
---|
| 629 | * to getuptime() are monotonous.
|
---|
| 630 | *
|
---|
| 631 | */
|
---|
| 632 | void getuptime(struct timespec *ts)
|
---|
[0b99e40] | 633 | {
|
---|
[6119f24] | 634 | if (ktime == NULL) {
|
---|
| 635 | uintptr_t faddr;
|
---|
[b7fd2a0] | 636 | errno_t rc = sysinfo_get_value("clock.faddr", &faddr);
|
---|
[6119f24] | 637 | if (rc != EOK) {
|
---|
| 638 | errno = rc;
|
---|
[1ab8539] | 639 | goto fallback;
|
---|
[6119f24] | 640 | }
|
---|
[a35b458] | 641 |
|
---|
[bf9cb2f] | 642 | void *addr = AS_AREA_ANY;
|
---|
[8442d10] | 643 | rc = physmem_map(faddr, 1, AS_AREA_READ | AS_AREA_CACHEABLE,
|
---|
| 644 | &addr);
|
---|
[6119f24] | 645 | if (rc != EOK) {
|
---|
| 646 | as_area_destroy(addr);
|
---|
| 647 | errno = rc;
|
---|
[1ab8539] | 648 | goto fallback;
|
---|
[6119f24] | 649 | }
|
---|
[a35b458] | 650 |
|
---|
[6119f24] | 651 | ktime = addr;
|
---|
[0b99e40] | 652 | }
|
---|
[a35b458] | 653 |
|
---|
[2c577e0b] | 654 | sysarg_t s2 = ktime->seconds2;
|
---|
[a35b458] | 655 |
|
---|
[5bd03eb] | 656 | read_barrier();
|
---|
[205f1add] | 657 | ts->tv_nsec = USEC2NSEC(ktime->useconds);
|
---|
[a35b458] | 658 |
|
---|
[0b99e40] | 659 | read_barrier();
|
---|
[2c577e0b] | 660 | sysarg_t s1 = ktime->seconds1;
|
---|
[a35b458] | 661 |
|
---|
[2d1fde3b] | 662 | if (s1 != s2) {
|
---|
[205f1add] | 663 | ts->tv_sec = max(s1, s2);
|
---|
| 664 | ts->tv_nsec = 0;
|
---|
[2d1fde3b] | 665 | } else
|
---|
[205f1add] | 666 | ts->tv_sec = s1;
|
---|
[a35b458] | 667 |
|
---|
[1ab8539] | 668 | return;
|
---|
[a35b458] | 669 |
|
---|
[1ab8539] | 670 | fallback:
|
---|
[205f1add] | 671 | ts->tv_sec = 0;
|
---|
| 672 | ts->tv_nsec = 0;
|
---|
[0b99e40] | 673 | }
|
---|
[44c6d88d] | 674 |
|
---|
[813a703] | 675 | time_t time(time_t *tloc)
|
---|
| 676 | {
|
---|
[205f1add] | 677 | struct timespec ts;
|
---|
| 678 | getrealtime(&ts);
|
---|
[a35b458] | 679 |
|
---|
[813a703] | 680 | if (tloc)
|
---|
[205f1add] | 681 | *tloc = ts.tv_sec;
|
---|
[a35b458] | 682 |
|
---|
[205f1add] | 683 | return ts.tv_sec;
|
---|
[813a703] | 684 | }
|
---|
| 685 |
|
---|
[205f1add] | 686 | void udelay(usec_t time)
|
---|
[5fd3f2d] | 687 | {
|
---|
| 688 | (void) __SYSCALL1(SYS_THREAD_UDELAY, (sysarg_t) time);
|
---|
| 689 | }
|
---|
| 690 |
|
---|
[1ab8539] | 691 | /** Get time from broken-down time.
|
---|
| 692 | *
|
---|
| 693 | * First normalize the provided broken-down time
|
---|
| 694 | * (moves all values to their proper bounds) and
|
---|
| 695 | * then try to calculate the appropriate time_t
|
---|
| 696 | * representation.
|
---|
[c2b0e10] | 697 | *
|
---|
| 698 | * @param tm Broken-down time.
|
---|
[1ab8539] | 699 | *
|
---|
| 700 | * @return time_t representation of the time.
|
---|
| 701 | * @return Undefined value on overflow.
|
---|
| 702 | *
|
---|
[c2b0e10] | 703 | */
|
---|
| 704 | time_t mktime(struct tm *tm)
|
---|
| 705 | {
|
---|
| 706 | // TODO: take DST flag into account
|
---|
| 707 | // TODO: detect overflow
|
---|
[a35b458] | 708 |
|
---|
[7f9d97f3] | 709 | normalize_tm_time(tm, 0);
|
---|
[1ab8539] | 710 | return secs_since_epoch(tm);
|
---|
[c2b0e10] | 711 | }
|
---|
| 712 |
|
---|
[1ab8539] | 713 | /*
|
---|
| 714 | * FIXME: This requires POSIX-correct snprintf.
|
---|
| 715 | * Otherwise it won't work with non-ASCII chars.
|
---|
| 716 | */
|
---|
| 717 | #define APPEND(...) \
|
---|
| 718 | { \
|
---|
| 719 | consumed = snprintf(ptr, remaining, __VA_ARGS__); \
|
---|
| 720 | if (consumed >= remaining) \
|
---|
| 721 | return 0; \
|
---|
| 722 | \
|
---|
| 723 | ptr += consumed; \
|
---|
| 724 | remaining -= consumed; \
|
---|
| 725 | }
|
---|
| 726 |
|
---|
| 727 | #define RECURSE(fmt) \
|
---|
| 728 | { \
|
---|
| 729 | consumed = strftime(ptr, remaining, fmt, tm); \
|
---|
| 730 | if (consumed == 0) \
|
---|
| 731 | return 0; \
|
---|
| 732 | \
|
---|
| 733 | ptr += consumed; \
|
---|
| 734 | remaining -= consumed; \
|
---|
| 735 | }
|
---|
| 736 |
|
---|
| 737 | #define TO_12H(hour) \
|
---|
| 738 | (((hour) > 12) ? ((hour) - 12) : \
|
---|
| 739 | (((hour) == 0) ? 12 : (hour)))
|
---|
| 740 |
|
---|
| 741 | /** Convert time and date to a string.
|
---|
| 742 | *
|
---|
| 743 | * @param s Buffer to write string to.
|
---|
[c2b0e10] | 744 | * @param maxsize Size of the buffer.
|
---|
[1ab8539] | 745 | * @param format Format of the output.
|
---|
| 746 | * @param tm Broken-down time to format.
|
---|
| 747 | *
|
---|
[c2b0e10] | 748 | * @return Number of bytes written.
|
---|
[1ab8539] | 749 | *
|
---|
[c2b0e10] | 750 | */
|
---|
| 751 | size_t strftime(char *restrict s, size_t maxsize,
|
---|
| 752 | const char *restrict format, const struct tm *restrict tm)
|
---|
| 753 | {
|
---|
| 754 | assert(s != NULL);
|
---|
| 755 | assert(format != NULL);
|
---|
| 756 | assert(tm != NULL);
|
---|
[a35b458] | 757 |
|
---|
[c2b0e10] | 758 | // TODO: use locale
|
---|
[a35b458] | 759 |
|
---|
[c2b0e10] | 760 | static const char *wday_abbr[] = {
|
---|
| 761 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
---|
| 762 | };
|
---|
[a35b458] | 763 |
|
---|
[c2b0e10] | 764 | static const char *wday[] = {
|
---|
| 765 | "Sunday", "Monday", "Tuesday", "Wednesday",
|
---|
| 766 | "Thursday", "Friday", "Saturday"
|
---|
| 767 | };
|
---|
[a35b458] | 768 |
|
---|
[c2b0e10] | 769 | static const char *mon_abbr[] = {
|
---|
| 770 | "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
---|
| 771 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
---|
| 772 | };
|
---|
[a35b458] | 773 |
|
---|
[c2b0e10] | 774 | static const char *mon[] = {
|
---|
| 775 | "January", "February", "March", "April", "May", "June", "July",
|
---|
| 776 | "August", "September", "October", "November", "December"
|
---|
| 777 | };
|
---|
[a35b458] | 778 |
|
---|
[1ab8539] | 779 | if (maxsize < 1)
|
---|
[c2b0e10] | 780 | return 0;
|
---|
[a35b458] | 781 |
|
---|
[c2b0e10] | 782 | char *ptr = s;
|
---|
| 783 | size_t consumed;
|
---|
| 784 | size_t remaining = maxsize;
|
---|
[a35b458] | 785 |
|
---|
[c2b0e10] | 786 | while (*format != '\0') {
|
---|
| 787 | if (*format != '%') {
|
---|
[1ab8539] | 788 | APPEND("%c", *format);
|
---|
[c2b0e10] | 789 | format++;
|
---|
| 790 | continue;
|
---|
| 791 | }
|
---|
[a35b458] | 792 |
|
---|
[c2b0e10] | 793 | format++;
|
---|
[1ab8539] | 794 | if ((*format == '0') || (*format == '+')) {
|
---|
[c2b0e10] | 795 | // TODO: padding
|
---|
| 796 | format++;
|
---|
| 797 | }
|
---|
[a35b458] | 798 |
|
---|
[c2b0e10] | 799 | while (isdigit(*format)) {
|
---|
| 800 | // TODO: padding
|
---|
| 801 | format++;
|
---|
| 802 | }
|
---|
[a35b458] | 803 |
|
---|
[1ab8539] | 804 | if ((*format == 'O') || (*format == 'E')) {
|
---|
[c2b0e10] | 805 | // TODO: locale's alternative format
|
---|
| 806 | format++;
|
---|
| 807 | }
|
---|
[a35b458] | 808 |
|
---|
[c2b0e10] | 809 | switch (*format) {
|
---|
| 810 | case 'a':
|
---|
[1ab8539] | 811 | APPEND("%s", wday_abbr[tm->tm_wday]);
|
---|
| 812 | break;
|
---|
[c2b0e10] | 813 | case 'A':
|
---|
[1ab8539] | 814 | APPEND("%s", wday[tm->tm_wday]);
|
---|
| 815 | break;
|
---|
[c2b0e10] | 816 | case 'b':
|
---|
[1ab8539] | 817 | APPEND("%s", mon_abbr[tm->tm_mon]);
|
---|
| 818 | break;
|
---|
[c2b0e10] | 819 | case 'B':
|
---|
[1ab8539] | 820 | APPEND("%s", mon[tm->tm_mon]);
|
---|
| 821 | break;
|
---|
[c2b0e10] | 822 | case 'c':
|
---|
| 823 | // TODO: locale-specific datetime format
|
---|
[1ab8539] | 824 | RECURSE("%Y-%m-%d %H:%M:%S");
|
---|
| 825 | break;
|
---|
[c2b0e10] | 826 | case 'C':
|
---|
[1ab8539] | 827 | APPEND("%02d", (1900 + tm->tm_year) / 100);
|
---|
| 828 | break;
|
---|
[c2b0e10] | 829 | case 'd':
|
---|
[1ab8539] | 830 | APPEND("%02d", tm->tm_mday);
|
---|
| 831 | break;
|
---|
[c2b0e10] | 832 | case 'D':
|
---|
[1ab8539] | 833 | RECURSE("%m/%d/%y");
|
---|
| 834 | break;
|
---|
[c2b0e10] | 835 | case 'e':
|
---|
[1ab8539] | 836 | APPEND("%2d", tm->tm_mday);
|
---|
| 837 | break;
|
---|
[c2b0e10] | 838 | case 'F':
|
---|
[1ab8539] | 839 | RECURSE("%+4Y-%m-%d");
|
---|
| 840 | break;
|
---|
[c2b0e10] | 841 | case 'g':
|
---|
[1ab8539] | 842 | APPEND("%02d", wbyear(tm) % 100);
|
---|
| 843 | break;
|
---|
[c2b0e10] | 844 | case 'G':
|
---|
[1ab8539] | 845 | APPEND("%d", wbyear(tm));
|
---|
| 846 | break;
|
---|
[c2b0e10] | 847 | case 'h':
|
---|
[1ab8539] | 848 | RECURSE("%b");
|
---|
| 849 | break;
|
---|
[c2b0e10] | 850 | case 'H':
|
---|
[1ab8539] | 851 | APPEND("%02d", tm->tm_hour);
|
---|
| 852 | break;
|
---|
[c2b0e10] | 853 | case 'I':
|
---|
[1ab8539] | 854 | APPEND("%02d", TO_12H(tm->tm_hour));
|
---|
| 855 | break;
|
---|
[c2b0e10] | 856 | case 'j':
|
---|
[1ab8539] | 857 | APPEND("%03d", tm->tm_yday);
|
---|
| 858 | break;
|
---|
[c2b0e10] | 859 | case 'k':
|
---|
[1ab8539] | 860 | APPEND("%2d", tm->tm_hour);
|
---|
| 861 | break;
|
---|
[c2b0e10] | 862 | case 'l':
|
---|
[1ab8539] | 863 | APPEND("%2d", TO_12H(tm->tm_hour));
|
---|
| 864 | break;
|
---|
[c2b0e10] | 865 | case 'm':
|
---|
[1ab8539] | 866 | APPEND("%02d", tm->tm_mon);
|
---|
| 867 | break;
|
---|
[c2b0e10] | 868 | case 'M':
|
---|
[1ab8539] | 869 | APPEND("%02d", tm->tm_min);
|
---|
| 870 | break;
|
---|
[c2b0e10] | 871 | case 'n':
|
---|
[1ab8539] | 872 | APPEND("\n");
|
---|
| 873 | break;
|
---|
[c2b0e10] | 874 | case 'p':
|
---|
[1ab8539] | 875 | APPEND("%s", tm->tm_hour < 12 ? "AM" : "PM");
|
---|
| 876 | break;
|
---|
[c2b0e10] | 877 | case 'P':
|
---|
[1ab8539] | 878 | APPEND("%s", tm->tm_hour < 12 ? "am" : "PM");
|
---|
| 879 | break;
|
---|
[c2b0e10] | 880 | case 'r':
|
---|
[1ab8539] | 881 | RECURSE("%I:%M:%S %p");
|
---|
| 882 | break;
|
---|
[c2b0e10] | 883 | case 'R':
|
---|
[1ab8539] | 884 | RECURSE("%H:%M");
|
---|
| 885 | break;
|
---|
[c2b0e10] | 886 | case 's':
|
---|
[205f1add] | 887 | APPEND("%lld", secs_since_epoch(tm));
|
---|
[1ab8539] | 888 | break;
|
---|
[c2b0e10] | 889 | case 'S':
|
---|
[1ab8539] | 890 | APPEND("%02d", tm->tm_sec);
|
---|
| 891 | break;
|
---|
[c2b0e10] | 892 | case 't':
|
---|
[1ab8539] | 893 | APPEND("\t");
|
---|
| 894 | break;
|
---|
[c2b0e10] | 895 | case 'T':
|
---|
[1ab8539] | 896 | RECURSE("%H:%M:%S");
|
---|
| 897 | break;
|
---|
[c2b0e10] | 898 | case 'u':
|
---|
[1ab8539] | 899 | APPEND("%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
|
---|
[c2b0e10] | 900 | break;
|
---|
| 901 | case 'U':
|
---|
[1ab8539] | 902 | APPEND("%02d", sun_week_number(tm));
|
---|
| 903 | break;
|
---|
[c2b0e10] | 904 | case 'V':
|
---|
[1ab8539] | 905 | APPEND("%02d", iso_week_number(tm));
|
---|
| 906 | break;
|
---|
[c2b0e10] | 907 | case 'w':
|
---|
[1ab8539] | 908 | APPEND("%d", tm->tm_wday);
|
---|
| 909 | break;
|
---|
[c2b0e10] | 910 | case 'W':
|
---|
[1ab8539] | 911 | APPEND("%02d", mon_week_number(tm));
|
---|
| 912 | break;
|
---|
[c2b0e10] | 913 | case 'x':
|
---|
| 914 | // TODO: locale-specific date format
|
---|
[1ab8539] | 915 | RECURSE("%Y-%m-%d");
|
---|
| 916 | break;
|
---|
[c2b0e10] | 917 | case 'X':
|
---|
| 918 | // TODO: locale-specific time format
|
---|
[1ab8539] | 919 | RECURSE("%H:%M:%S");
|
---|
| 920 | break;
|
---|
[c2b0e10] | 921 | case 'y':
|
---|
[1ab8539] | 922 | APPEND("%02d", tm->tm_year % 100);
|
---|
| 923 | break;
|
---|
[c2b0e10] | 924 | case 'Y':
|
---|
[1ab8539] | 925 | APPEND("%d", 1900 + tm->tm_year);
|
---|
| 926 | break;
|
---|
[c2b0e10] | 927 | case 'z':
|
---|
| 928 | // TODO: timezone
|
---|
| 929 | break;
|
---|
| 930 | case 'Z':
|
---|
| 931 | // TODO: timezone
|
---|
| 932 | break;
|
---|
| 933 | case '%':
|
---|
[1ab8539] | 934 | APPEND("%%");
|
---|
[c2b0e10] | 935 | break;
|
---|
| 936 | default:
|
---|
| 937 | /* Invalid specifier, print verbatim. */
|
---|
[1ab8539] | 938 | while (*format != '%')
|
---|
[c2b0e10] | 939 | format--;
|
---|
[a35b458] | 940 |
|
---|
[1ab8539] | 941 | APPEND("%%");
|
---|
[c2b0e10] | 942 | break;
|
---|
| 943 | }
|
---|
[a35b458] | 944 |
|
---|
[c2b0e10] | 945 | format++;
|
---|
| 946 | }
|
---|
[a35b458] | 947 |
|
---|
[c2b0e10] | 948 | return maxsize - remaining;
|
---|
| 949 | }
|
---|
| 950 |
|
---|
[1ab8539] | 951 | /** Convert a time value to a broken-down UTC time/
|
---|
[f7ea5400] | 952 | *
|
---|
[1ab8539] | 953 | * @param time Time to convert
|
---|
| 954 | * @param result Structure to store the result to
|
---|
| 955 | *
|
---|
[cde999a] | 956 | * @return EOK or an error code
|
---|
[f7ea5400] | 957 | *
|
---|
| 958 | */
|
---|
[b7fd2a0] | 959 | errno_t time_utc2tm(const time_t time, struct tm *restrict result)
|
---|
[f7ea5400] | 960 | {
|
---|
| 961 | assert(result != NULL);
|
---|
[a35b458] | 962 |
|
---|
[5b3394c] | 963 | /* Set result to epoch. */
|
---|
[205f1add] | 964 | result->tm_nsec = 0;
|
---|
[f7ea5400] | 965 | result->tm_sec = 0;
|
---|
| 966 | result->tm_min = 0;
|
---|
| 967 | result->tm_hour = 0;
|
---|
| 968 | result->tm_mday = 1;
|
---|
| 969 | result->tm_mon = 0;
|
---|
| 970 | result->tm_year = 70; /* 1970 */
|
---|
[a35b458] | 971 |
|
---|
[7f9d97f3] | 972 | if (normalize_tm_time(result, time) == -1)
|
---|
[f7ea5400] | 973 | return EOVERFLOW;
|
---|
[a35b458] | 974 |
|
---|
[f7ea5400] | 975 | return EOK;
|
---|
[5b3394c] | 976 | }
|
---|
| 977 |
|
---|
[1ab8539] | 978 | /** Convert a time value to a NULL-terminated string.
|
---|
| 979 | *
|
---|
| 980 | * The format is "Wed Jun 30 21:49:08 1993\n" expressed in UTC.
|
---|
| 981 | *
|
---|
| 982 | * @param time Time to convert.
|
---|
| 983 | * @param buf Buffer to store the string to, must be at least
|
---|
| 984 | * ASCTIME_BUF_LEN bytes long.
|
---|
[f7ea5400] | 985 | *
|
---|
[cde999a] | 986 | * @return EOK or an error code.
|
---|
[f7ea5400] | 987 | *
|
---|
| 988 | */
|
---|
[b7fd2a0] | 989 | errno_t time_utc2str(const time_t time, char *restrict buf)
|
---|
[f7ea5400] | 990 | {
|
---|
[1ab8539] | 991 | struct tm tm;
|
---|
[b7fd2a0] | 992 | errno_t ret = time_utc2tm(time, &tm);
|
---|
[1ab8539] | 993 | if (ret != EOK)
|
---|
| 994 | return ret;
|
---|
[a35b458] | 995 |
|
---|
[1ab8539] | 996 | time_tm2str(&tm, buf);
|
---|
[f7ea5400] | 997 | return EOK;
|
---|
| 998 | }
|
---|
| 999 |
|
---|
[1ab8539] | 1000 | /** Convert broken-down time to a NULL-terminated string.
|
---|
| 1001 | *
|
---|
| 1002 | * The format is "Sun Jan 1 00:00:00 1970\n". (Obsolete)
|
---|
[8219eb9] | 1003 | *
|
---|
| 1004 | * @param timeptr Broken-down time structure.
|
---|
[1ab8539] | 1005 | * @param buf Buffer to store string to, must be at least
|
---|
| 1006 | * ASCTIME_BUF_LEN bytes long.
|
---|
| 1007 | *
|
---|
[8219eb9] | 1008 | */
|
---|
[664fc031] | 1009 | void time_tm2str(const struct tm *restrict timeptr, char *restrict buf)
|
---|
[8219eb9] | 1010 | {
|
---|
| 1011 | assert(timeptr != NULL);
|
---|
[f7ea5400] | 1012 | assert(buf != NULL);
|
---|
[a35b458] | 1013 |
|
---|
[8219eb9] | 1014 | static const char *wday[] = {
|
---|
| 1015 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
---|
| 1016 | };
|
---|
[a35b458] | 1017 |
|
---|
[8219eb9] | 1018 | static const char *mon[] = {
|
---|
| 1019 | "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
---|
| 1020 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
---|
| 1021 | };
|
---|
[a35b458] | 1022 |
|
---|
[8219eb9] | 1023 | snprintf(buf, ASCTIME_BUF_LEN, "%s %s %2d %02d:%02d:%02d %d\n",
|
---|
| 1024 | wday[timeptr->tm_wday],
|
---|
| 1025 | mon[timeptr->tm_mon],
|
---|
| 1026 | timeptr->tm_mday, timeptr->tm_hour,
|
---|
| 1027 | timeptr->tm_min, timeptr->tm_sec,
|
---|
| 1028 | 1900 + timeptr->tm_year);
|
---|
| 1029 | }
|
---|
| 1030 |
|
---|
[1ab8539] | 1031 | /** Converts a time value to a broken-down local time.
|
---|
| 1032 | *
|
---|
| 1033 | * Time is expressed relative to the user's specified timezone.
|
---|
[f7ea5400] | 1034 | *
|
---|
[7f9d97f3] | 1035 | * @param tv Timeval to convert.
|
---|
[1ab8539] | 1036 | * @param result Structure to store the result to.
|
---|
| 1037 | *
|
---|
[cde999a] | 1038 | * @return EOK on success or an error code.
|
---|
[f6cb995] | 1039 | *
|
---|
| 1040 | */
|
---|
[205f1add] | 1041 | errno_t time_ts2tm(const struct timespec *ts, struct tm *restrict result)
|
---|
[f6cb995] | 1042 | {
|
---|
[1ab8539] | 1043 | // TODO: Deal with timezones.
|
---|
| 1044 | // Currently assumes system and all times are in UTC
|
---|
[a35b458] | 1045 |
|
---|
[f6cb995] | 1046 | /* Set result to epoch. */
|
---|
[205f1add] | 1047 | result->tm_nsec = 0;
|
---|
[f7ea5400] | 1048 | result->tm_sec = 0;
|
---|
| 1049 | result->tm_min = 0;
|
---|
| 1050 | result->tm_hour = 0;
|
---|
| 1051 | result->tm_mday = 1;
|
---|
| 1052 | result->tm_mon = 0;
|
---|
| 1053 | result->tm_year = 70; /* 1970 */
|
---|
[a35b458] | 1054 |
|
---|
[205f1add] | 1055 | if (normalize_tm_ts(result, ts) == -1)
|
---|
[f7ea5400] | 1056 | return EOVERFLOW;
|
---|
[a35b458] | 1057 |
|
---|
[f7ea5400] | 1058 | return EOK;
|
---|
[f6cb995] | 1059 | }
|
---|
[c2b0e10] | 1060 |
|
---|
[7f9d97f3] | 1061 | /** Converts a time value to a broken-down local time.
|
---|
| 1062 | *
|
---|
| 1063 | * Time is expressed relative to the user's specified timezone.
|
---|
| 1064 | *
|
---|
| 1065 | * @param timer Time to convert.
|
---|
| 1066 | * @param result Structure to store the result to.
|
---|
| 1067 | *
|
---|
[cde999a] | 1068 | * @return EOK on success or an error code.
|
---|
[7f9d97f3] | 1069 | *
|
---|
| 1070 | */
|
---|
[b7fd2a0] | 1071 | errno_t time_local2tm(const time_t time, struct tm *restrict result)
|
---|
[7f9d97f3] | 1072 | {
|
---|
[205f1add] | 1073 | struct timespec ts = {
|
---|
[7f9d97f3] | 1074 | .tv_sec = time,
|
---|
[205f1add] | 1075 | .tv_nsec = 0
|
---|
[7f9d97f3] | 1076 | };
|
---|
| 1077 |
|
---|
[205f1add] | 1078 | return time_ts2tm(&ts, result);
|
---|
[7f9d97f3] | 1079 | }
|
---|
| 1080 |
|
---|
[1ab8539] | 1081 | /** Convert the calendar time to a NULL-terminated string.
|
---|
| 1082 | *
|
---|
| 1083 | * The format is "Wed Jun 30 21:49:08 1993\n" expressed relative to the
|
---|
[f7ea5400] | 1084 | * user's specified timezone.
|
---|
| 1085 | *
|
---|
[1ab8539] | 1086 | * @param timer Time to convert.
|
---|
| 1087 | * @param buf Buffer to store the string to. Must be at least
|
---|
| 1088 | * ASCTIME_BUF_LEN bytes long.
|
---|
| 1089 | *
|
---|
[cde999a] | 1090 | * @return EOK on success or an error code.
|
---|
[1ab8539] | 1091 | *
|
---|
[56b308e] | 1092 | */
|
---|
[b7fd2a0] | 1093 | errno_t time_local2str(const time_t time, char *buf)
|
---|
[56b308e] | 1094 | {
|
---|
[f7ea5400] | 1095 | struct tm loctime;
|
---|
[b7fd2a0] | 1096 | errno_t ret = time_local2tm(time, &loctime);
|
---|
[1ab8539] | 1097 | if (ret != EOK)
|
---|
| 1098 | return ret;
|
---|
[a35b458] | 1099 |
|
---|
[664fc031] | 1100 | time_tm2str(&loctime, buf);
|
---|
[f7ea5400] | 1101 | return EOK;
|
---|
[56b308e] | 1102 | }
|
---|
| 1103 |
|
---|
[1ab8539] | 1104 | /** Calculate the difference between two times, in seconds.
|
---|
| 1105 | *
|
---|
[d3e3a71] | 1106 | * @param time1 First time.
|
---|
| 1107 | * @param time0 Second time.
|
---|
[1ab8539] | 1108 | *
|
---|
| 1109 | * @return Time difference in seconds.
|
---|
| 1110 | *
|
---|
[d3e3a71] | 1111 | */
|
---|
| 1112 | double difftime(time_t time1, time_t time0)
|
---|
| 1113 | {
|
---|
| 1114 | return (double) (time1 - time0);
|
---|
| 1115 | }
|
---|
| 1116 |
|
---|
[a46da63] | 1117 | /** @}
|
---|
[b2951e2] | 1118 | */
|
---|