1 1.1 christos /* $NetBSD: tm.c,v 1.8 2025/01/26 16:25:39 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 1.1 christos * 6 1.7 christos * SPDX-License-Identifier: MPL-2.0 AND BSD-2-Clause 7 1.7 christos * 8 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public 9 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this 10 1.5 christos * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 1.1 christos * 12 1.1 christos * See the COPYRIGHT file distributed with this work for additional 13 1.1 christos * information regarding copyright ownership. 14 1.1 christos */ 15 1.1 christos 16 1.1 christos /*- 17 1.1 christos * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 18 1.1 christos * All rights reserved. 19 1.1 christos * 20 1.1 christos * This code was contributed to The NetBSD Foundation by Klaus Klein. 21 1.1 christos * 22 1.1 christos * Redistribution and use in source and binary forms, with or without 23 1.1 christos * modification, are permitted provided that the following conditions 24 1.1 christos * are met: 25 1.1 christos * 1. Redistributions of source code must retain the above copyright 26 1.1 christos * notice, this list of conditions and the following disclaimer. 27 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 28 1.1 christos * notice, this list of conditions and the following disclaimer in the 29 1.1 christos * documentation and/or other materials provided with the distribution. 30 1.1 christos * 31 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 32 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 33 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 34 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 35 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 36 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 39 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 42 1.1 christos */ 43 1.1 christos 44 1.4 christos #include <ctype.h> 45 1.1 christos #include <stdio.h> 46 1.1 christos #include <stdlib.h> 47 1.1 christos #include <string.h> 48 1.1 christos #include <time.h> 49 1.1 christos 50 1.1 christos #include <isc/tm.h> 51 1.1 christos #include <isc/util.h> 52 1.1 christos 53 1.1 christos /* 54 1.1 christos * Portable conversion routines for struct tm, replacing 55 1.1 christos * timegm() and strptime(), which are not available on all 56 1.1 christos * platforms and don't always behave the same way when they 57 1.1 christos * are. 58 1.1 christos */ 59 1.1 christos 60 1.1 christos /* 61 1.1 christos * We do not implement alternate representations. However, we always 62 1.1 christos * check whether a given modifier is allowed for a certain conversion. 63 1.1 christos */ 64 1.4 christos #define ALT_E 0x01 65 1.4 christos #define ALT_O 0x02 66 1.4 christos #define LEGAL_ALT(x) \ 67 1.4 christos { \ 68 1.4 christos if ((alt_format & ~(x)) != 0) \ 69 1.4 christos return ((0)); \ 70 1.4 christos } 71 1.1 christos 72 1.1 christos #ifndef TM_YEAR_BASE 73 1.1 christos #define TM_YEAR_BASE 1900 74 1.4 christos #endif /* ifndef TM_YEAR_BASE */ 75 1.1 christos 76 1.4 christos static const char *day[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", 77 1.4 christos "Thursday", "Friday", "Saturday" }; 78 1.1 christos static const char *abday[7] = { 79 1.1 christos "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 80 1.1 christos }; 81 1.1 christos static const char *mon[12] = { 82 1.4 christos "January", "February", "March", "April", "May", "June", 83 1.4 christos "July", "August", "September", "October", "November", "December" 84 1.1 christos }; 85 1.4 christos static const char *abmon[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", 86 1.4 christos "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; 87 1.4 christos static const char *am_pm[2] = { "AM", "PM" }; 88 1.1 christos 89 1.1 christos static int 90 1.1 christos conv_num(const char **buf, int *dest, int llim, int ulim) { 91 1.1 christos int result = 0; 92 1.1 christos 93 1.1 christos /* The limit also determines the number of valid digits. */ 94 1.1 christos int rulim = ulim; 95 1.1 christos 96 1.6 christos if (!isdigit((unsigned char)**buf)) { 97 1.8 christos return 0; 98 1.4 christos } 99 1.1 christos 100 1.1 christos do { 101 1.8 christos result = 10 * result + *(*buf)++ - '0'; 102 1.1 christos rulim /= 10; 103 1.8 christos } while ((result * 10 <= ulim) && rulim && 104 1.8 christos isdigit((unsigned char)**buf)); 105 1.1 christos 106 1.4 christos if (result < llim || result > ulim) { 107 1.8 christos return 0; 108 1.4 christos } 109 1.1 christos 110 1.1 christos *dest = result; 111 1.8 christos return 1; 112 1.1 christos } 113 1.1 christos 114 1.1 christos time_t 115 1.1 christos isc_tm_timegm(struct tm *tm) { 116 1.1 christos time_t ret; 117 1.1 christos int i, yday = 0, leapday; 118 1.1 christos int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 }; 119 1.1 christos 120 1.4 christos leapday = ((((tm->tm_year + 1900) % 4) == 0 && 121 1.4 christos ((tm->tm_year + 1900) % 100) != 0) || 122 1.4 christos ((tm->tm_year + 1900) % 400) == 0) 123 1.4 christos ? 1 124 1.4 christos : 0; 125 1.1 christos mdays[1] += leapday; 126 1.1 christos 127 1.1 christos yday = tm->tm_mday - 1; 128 1.4 christos for (i = 1; i <= tm->tm_mon; i++) { 129 1.1 christos yday += mdays[i - 1]; 130 1.4 christos } 131 1.4 christos ret = tm->tm_sec + (60 * tm->tm_min) + (3600 * tm->tm_hour) + 132 1.4 christos (86400 * 133 1.4 christos (yday + ((tm->tm_year - 70) * 365) + ((tm->tm_year - 69) / 4) - 134 1.4 christos ((tm->tm_year - 1) / 100) + ((tm->tm_year + 299) / 400))); 135 1.8 christos return ret; 136 1.1 christos } 137 1.1 christos 138 1.1 christos char * 139 1.1 christos isc_tm_strptime(const char *buf, const char *fmt, struct tm *tm) { 140 1.8 christos char c; 141 1.1 christos const char *bp; 142 1.1 christos size_t len = 0; 143 1.1 christos int alt_format, i, split_year = 0; 144 1.1 christos 145 1.1 christos REQUIRE(buf != NULL); 146 1.1 christos REQUIRE(fmt != NULL); 147 1.1 christos REQUIRE(tm != NULL); 148 1.1 christos 149 1.1 christos memset(tm, 0, sizeof(struct tm)); 150 1.1 christos 151 1.1 christos bp = buf; 152 1.1 christos 153 1.1 christos while ((c = *fmt) != '\0') { 154 1.1 christos /* Clear `alternate' modifier prior to new conversion. */ 155 1.1 christos alt_format = 0; 156 1.1 christos 157 1.1 christos /* Eat up white-space. */ 158 1.4 christos if (isspace((unsigned char)c)) { 159 1.4 christos while (isspace((unsigned char)*bp)) { 160 1.1 christos bp++; 161 1.4 christos } 162 1.1 christos 163 1.1 christos fmt++; 164 1.1 christos continue; 165 1.1 christos } 166 1.1 christos 167 1.4 christos if ((c = *fmt++) != '%') { 168 1.1 christos goto literal; 169 1.4 christos } 170 1.1 christos 171 1.4 christos again: 172 1.4 christos switch (c = *fmt++) { 173 1.4 christos case '%': /* "%%" is converted to "%". */ 174 1.4 christos literal: 175 1.4 christos if (c != *bp++) { 176 1.8 christos return 0; 177 1.4 christos } 178 1.1 christos break; 179 1.1 christos 180 1.1 christos /* 181 1.1 christos * "Alternative" modifiers. Just set the appropriate flag 182 1.1 christos * and start over again. 183 1.1 christos */ 184 1.4 christos case 'E': /* "%E?" alternative conversion modifier. */ 185 1.1 christos LEGAL_ALT(0); 186 1.1 christos alt_format |= ALT_E; 187 1.1 christos goto again; 188 1.1 christos 189 1.4 christos case 'O': /* "%O?" alternative conversion modifier. */ 190 1.1 christos LEGAL_ALT(0); 191 1.1 christos alt_format |= ALT_O; 192 1.1 christos goto again; 193 1.1 christos 194 1.1 christos /* 195 1.1 christos * "Complex" conversion rules, implemented through recursion. 196 1.1 christos */ 197 1.4 christos case 'c': /* Date and time, using the locale's format. */ 198 1.1 christos LEGAL_ALT(ALT_E); 199 1.4 christos if (!(bp = isc_tm_strptime(bp, "%x %X", tm))) { 200 1.8 christos return 0; 201 1.4 christos } 202 1.1 christos break; 203 1.1 christos 204 1.4 christos case 'D': /* The date as "%m/%d/%y". */ 205 1.1 christos LEGAL_ALT(0); 206 1.4 christos if (!(bp = isc_tm_strptime(bp, "%m/%d/%y", tm))) { 207 1.8 christos return 0; 208 1.4 christos } 209 1.1 christos break; 210 1.1 christos 211 1.4 christos case 'R': /* The time as "%H:%M". */ 212 1.1 christos LEGAL_ALT(0); 213 1.4 christos if (!(bp = isc_tm_strptime(bp, "%H:%M", tm))) { 214 1.8 christos return 0; 215 1.4 christos } 216 1.1 christos break; 217 1.1 christos 218 1.4 christos case 'r': /* The time in 12-hour clock representation. */ 219 1.1 christos LEGAL_ALT(0); 220 1.4 christos if (!(bp = isc_tm_strptime(bp, "%I:%M:%S %p", tm))) { 221 1.8 christos return 0; 222 1.4 christos } 223 1.1 christos break; 224 1.1 christos 225 1.4 christos case 'T': /* The time as "%H:%M:%S". */ 226 1.1 christos LEGAL_ALT(0); 227 1.4 christos if (!(bp = isc_tm_strptime(bp, "%H:%M:%S", tm))) { 228 1.8 christos return 0; 229 1.4 christos } 230 1.1 christos break; 231 1.1 christos 232 1.4 christos case 'X': /* The time, using the locale's format. */ 233 1.1 christos LEGAL_ALT(ALT_E); 234 1.4 christos if (!(bp = isc_tm_strptime(bp, "%H:%M:%S", tm))) { 235 1.8 christos return 0; 236 1.4 christos } 237 1.1 christos break; 238 1.1 christos 239 1.4 christos case 'x': /* The date, using the locale's format. */ 240 1.1 christos LEGAL_ALT(ALT_E); 241 1.4 christos if (!(bp = isc_tm_strptime(bp, "%m/%d/%y", tm))) { 242 1.8 christos return 0; 243 1.4 christos } 244 1.1 christos break; 245 1.1 christos 246 1.1 christos /* 247 1.1 christos * "Elementary" conversion rules. 248 1.1 christos */ 249 1.4 christos case 'A': /* The day of week, using the locale's form. */ 250 1.1 christos case 'a': 251 1.1 christos LEGAL_ALT(0); 252 1.1 christos for (i = 0; i < 7; i++) { 253 1.1 christos /* Full name. */ 254 1.1 christos len = strlen(day[i]); 255 1.4 christos if (strncasecmp(day[i], bp, len) == 0) { 256 1.1 christos break; 257 1.4 christos } 258 1.1 christos 259 1.1 christos /* Abbreviated name. */ 260 1.1 christos len = strlen(abday[i]); 261 1.4 christos if (strncasecmp(abday[i], bp, len) == 0) { 262 1.1 christos break; 263 1.4 christos } 264 1.1 christos } 265 1.1 christos 266 1.1 christos /* Nothing matched. */ 267 1.4 christos if (i == 7) { 268 1.8 christos return 0; 269 1.4 christos } 270 1.1 christos 271 1.1 christos tm->tm_wday = i; 272 1.1 christos bp += len; 273 1.1 christos break; 274 1.1 christos 275 1.4 christos case 'B': /* The month, using the locale's form. */ 276 1.1 christos case 'b': 277 1.1 christos case 'h': 278 1.1 christos LEGAL_ALT(0); 279 1.1 christos for (i = 0; i < 12; i++) { 280 1.1 christos /* Full name. */ 281 1.1 christos len = strlen(mon[i]); 282 1.4 christos if (strncasecmp(mon[i], bp, len) == 0) { 283 1.1 christos break; 284 1.4 christos } 285 1.1 christos 286 1.1 christos /* Abbreviated name. */ 287 1.1 christos len = strlen(abmon[i]); 288 1.4 christos if (strncasecmp(abmon[i], bp, len) == 0) { 289 1.1 christos break; 290 1.4 christos } 291 1.1 christos } 292 1.1 christos 293 1.1 christos /* Nothing matched. */ 294 1.4 christos if (i == 12) { 295 1.8 christos return 0; 296 1.4 christos } 297 1.1 christos 298 1.1 christos tm->tm_mon = i; 299 1.1 christos bp += len; 300 1.1 christos break; 301 1.1 christos 302 1.4 christos case 'C': /* The century number. */ 303 1.1 christos LEGAL_ALT(ALT_E); 304 1.4 christos if (!(conv_num(&bp, &i, 0, 99))) { 305 1.8 christos return 0; 306 1.4 christos } 307 1.1 christos 308 1.1 christos if (split_year) { 309 1.1 christos tm->tm_year = (tm->tm_year % 100) + (i * 100); 310 1.1 christos } else { 311 1.1 christos tm->tm_year = i * 100; 312 1.1 christos split_year = 1; 313 1.1 christos } 314 1.1 christos break; 315 1.1 christos 316 1.4 christos case 'd': /* The day of month. */ 317 1.1 christos case 'e': 318 1.1 christos LEGAL_ALT(ALT_O); 319 1.4 christos if (!(conv_num(&bp, &tm->tm_mday, 1, 31))) { 320 1.8 christos return 0; 321 1.4 christos } 322 1.1 christos break; 323 1.1 christos 324 1.4 christos case 'k': /* The hour (24-hour clock representation). */ 325 1.1 christos LEGAL_ALT(0); 326 1.7 christos FALLTHROUGH; 327 1.1 christos case 'H': 328 1.1 christos LEGAL_ALT(ALT_O); 329 1.4 christos if (!(conv_num(&bp, &tm->tm_hour, 0, 23))) { 330 1.8 christos return 0; 331 1.4 christos } 332 1.1 christos break; 333 1.1 christos 334 1.4 christos case 'l': /* The hour (12-hour clock representation). */ 335 1.1 christos LEGAL_ALT(0); 336 1.7 christos FALLTHROUGH; 337 1.1 christos case 'I': 338 1.1 christos LEGAL_ALT(ALT_O); 339 1.4 christos if (!(conv_num(&bp, &tm->tm_hour, 1, 12))) { 340 1.8 christos return 0; 341 1.4 christos } 342 1.4 christos if (tm->tm_hour == 12) { 343 1.1 christos tm->tm_hour = 0; 344 1.4 christos } 345 1.1 christos break; 346 1.1 christos 347 1.4 christos case 'j': /* The day of year. */ 348 1.1 christos LEGAL_ALT(0); 349 1.4 christos if (!(conv_num(&bp, &i, 1, 366))) { 350 1.8 christos return 0; 351 1.4 christos } 352 1.1 christos tm->tm_yday = i - 1; 353 1.1 christos break; 354 1.1 christos 355 1.4 christos case 'M': /* The minute. */ 356 1.1 christos LEGAL_ALT(ALT_O); 357 1.4 christos if (!(conv_num(&bp, &tm->tm_min, 0, 59))) { 358 1.8 christos return 0; 359 1.4 christos } 360 1.1 christos break; 361 1.1 christos 362 1.4 christos case 'm': /* The month. */ 363 1.1 christos LEGAL_ALT(ALT_O); 364 1.4 christos if (!(conv_num(&bp, &i, 1, 12))) { 365 1.8 christos return 0; 366 1.4 christos } 367 1.1 christos tm->tm_mon = i - 1; 368 1.1 christos break; 369 1.1 christos 370 1.4 christos case 'p': /* The locale's equivalent of AM/PM. */ 371 1.1 christos LEGAL_ALT(0); 372 1.1 christos /* AM? */ 373 1.1 christos if (strcasecmp(am_pm[0], bp) == 0) { 374 1.4 christos if (tm->tm_hour > 11) { 375 1.8 christos return 0; 376 1.4 christos } 377 1.1 christos 378 1.1 christos bp += strlen(am_pm[0]); 379 1.1 christos break; 380 1.1 christos } 381 1.1 christos /* PM? */ 382 1.4 christos else if (strcasecmp(am_pm[1], bp) == 0) 383 1.4 christos { 384 1.4 christos if (tm->tm_hour > 11) { 385 1.8 christos return 0; 386 1.4 christos } 387 1.1 christos 388 1.1 christos tm->tm_hour += 12; 389 1.1 christos bp += strlen(am_pm[1]); 390 1.1 christos break; 391 1.1 christos } 392 1.1 christos 393 1.1 christos /* Nothing matched. */ 394 1.8 christos return 0; 395 1.1 christos 396 1.4 christos case 'S': /* The seconds. */ 397 1.1 christos LEGAL_ALT(ALT_O); 398 1.4 christos if (!(conv_num(&bp, &tm->tm_sec, 0, 61))) { 399 1.8 christos return 0; 400 1.4 christos } 401 1.1 christos break; 402 1.1 christos 403 1.4 christos case 'U': /* The week of year, beginning on sunday. */ 404 1.4 christos case 'W': /* The week of year, beginning on monday. */ 405 1.1 christos LEGAL_ALT(ALT_O); 406 1.1 christos /* 407 1.1 christos * XXX This is bogus, as we can not assume any valid 408 1.1 christos * information present in the tm structure at this 409 1.1 christos * point to calculate a real value, so just check the 410 1.1 christos * range for now. 411 1.1 christos */ 412 1.4 christos if (!(conv_num(&bp, &i, 0, 53))) { 413 1.8 christos return 0; 414 1.4 christos } 415 1.4 christos break; 416 1.1 christos 417 1.4 christos case 'w': /* The day of week, beginning on sunday. */ 418 1.1 christos LEGAL_ALT(ALT_O); 419 1.4 christos if (!(conv_num(&bp, &tm->tm_wday, 0, 6))) { 420 1.8 christos return 0; 421 1.4 christos } 422 1.1 christos break; 423 1.1 christos 424 1.4 christos case 'Y': /* The year. */ 425 1.1 christos LEGAL_ALT(ALT_E); 426 1.4 christos if (!(conv_num(&bp, &i, 0, 9999))) { 427 1.8 christos return 0; 428 1.4 christos } 429 1.1 christos 430 1.1 christos tm->tm_year = i - TM_YEAR_BASE; 431 1.1 christos break; 432 1.1 christos 433 1.4 christos case 'y': /* The year within 100 years of the epoch. */ 434 1.1 christos LEGAL_ALT(ALT_E | ALT_O); 435 1.4 christos if (!(conv_num(&bp, &i, 0, 99))) { 436 1.8 christos return 0; 437 1.4 christos } 438 1.1 christos 439 1.1 christos if (split_year) { 440 1.1 christos tm->tm_year = ((tm->tm_year / 100) * 100) + i; 441 1.1 christos break; 442 1.1 christos } 443 1.1 christos split_year = 1; 444 1.4 christos if (i <= 68) { 445 1.1 christos tm->tm_year = i + 2000 - TM_YEAR_BASE; 446 1.4 christos } else { 447 1.1 christos tm->tm_year = i + 1900 - TM_YEAR_BASE; 448 1.4 christos } 449 1.1 christos break; 450 1.1 christos 451 1.1 christos /* 452 1.1 christos * Miscellaneous conversions. 453 1.1 christos */ 454 1.4 christos case 'n': /* Any kind of white-space. */ 455 1.1 christos case 't': 456 1.1 christos LEGAL_ALT(0); 457 1.4 christos while (isspace((unsigned char)*bp)) { 458 1.1 christos bp++; 459 1.4 christos } 460 1.1 christos break; 461 1.1 christos 462 1.4 christos default: /* Unknown/unsupported conversion. */ 463 1.8 christos return 0; 464 1.1 christos } 465 1.1 christos } 466 1.1 christos 467 1.1 christos /* LINTED functional specification */ 468 1.8 christos return UNCONST(bp); 469 1.1 christos } 470