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