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