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