Add explicit time32 and time64 support Handle the three potential system scenarios: - system time_t is time64 - system time_t is time32 and supports time64 - system time_t is time32 and does not support time64 Add the explicit time32 and time64 functions when necessary and map them accordingly for each of these cases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
diff --git a/configure.ac b/configure.ac
index bedc615..e516f93 100644
--- a/configure.ac
+++ b/configure.ac
@@ -29,6 +29,7 @@ user_CFLAGS=${CFLAGS-unset}
# Checks for operating system services and capabilities.
AC_USE_SYSTEM_EXTENSIONS
AC_SYS_LARGEFILE
+LIBBSD_SYS_TIME64
AM_PROG_AR
LT_INIT
diff --git a/include/Makefile.am b/include/Makefile.am
index 9a75b97..52b78be 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -75,3 +75,14 @@ nobase_include_HEADERS += \
bsd/vis.h \
# EOL
endif
+
+do_header_subst = $(AM_V_GEN) $(SED) \
+ -e 's:^//\(.define LIBBSD_SYS_TIME_BITS\).*:\1 $(LIBBSD_SYS_TIME_BITS):' \
+ -e 's:^//\(.define LIBBSD_SYS_HAS_TIME64\).*:\1 $(LIBBSD_SYS_HAS_TIME64):' \
+ # EOL
+
+install-data-hook:
+ $(do_header_subst) <$(DESTDIR)$(includedir)/bsd/sys/cdefs.h \
+ >$(DESTDIR)$(includedir)/bsd/sys/cdefs.h.new
+ mv $(DESTDIR)$(includedir)/bsd/sys/cdefs.h.new \
+ $(DESTDIR)$(includedir)/bsd/sys/cdefs.h
diff --git a/include/bsd/sys/cdefs.h b/include/bsd/sys/cdefs.h
index 126f427..d2627c4 100644
--- a/include/bsd/sys/cdefs.h
+++ b/include/bsd/sys/cdefs.h
@@ -86,6 +86,10 @@
#define _SYS_CDEFS_H
#endif
+/* Define the ABI for the current system. */
+//#define LIBBSD_SYS_TIME_BITS 0
+//#define LIBBSD_SYS_HAS_TIME64 0
+
#define LIBBSD_CONCAT(x, y) x ## y
#define LIBBSD_STRING(x) #x
diff --git a/include/bsd/timeconv.h b/include/bsd/timeconv.h
index a426bd3..755c89c 100644
--- a/include/bsd/timeconv.h
+++ b/include/bsd/timeconv.h
@@ -58,4 +58,17 @@ time_t _long_to_time(long tlong);
int _time_to_int(time_t t);
time_t _int_to_time(int tint);
+#if LIBBSD_SYS_TIME_BITS == 32 && LIBBSD_SYS_HAS_TIME64
+#if defined _TIME_BITS && _TIME_BITS == 64
+time_t LIBBSD_REDIRECT(_time32_to_time, (int32_t t32), _time32_to_time_time64);
+int32_t LIBBSD_REDIRECT(_time_to_time32, (time_t t), _time_to_time32_time64);
+time_t LIBBSD_REDIRECT(_time64_to_time, (int64_t t64), _time64_to_time_time64);
+int64_t LIBBSD_REDIRECT(_time_to_time64, (time_t t), _time_to_time64_time64);
+long LIBBSD_REDIRECT(_time_to_long, (time_t t), _time_to_long_time64);
+time_t LIBBSD_REDIRECT(_long_to_time, (long tlong), _long_to_time_time64);
+int LIBBSD_REDIRECT(_time_to_int, (time_t t), _time_to_int_time64);
+time_t LIBBSD_REDIRECT(_int_to_time, (int tint), _int_to_time_time64);
+#endif
+#endif
+
#endif /* LIBBSD_TIMECONV_H */
diff --git a/m4/libbsd-large.m4 b/m4/libbsd-large.m4
new file mode 100644
index 0000000..d279111
--- /dev/null
+++ b/m4/libbsd-large.m4
@@ -0,0 +1,51 @@
+# Copyright © 2024 Guillem Jover <guillem@hadrons.org>
+
+# LIBBSD_SYS_TIME64
+# -----------------
+# Check for availability of time64 support.
+AC_DEFUN([LIBBSD_SYS_TIME64], [
+ # Check the default time_t size.
+ AC_CHECK_SIZEOF([time_t], [], [[
+#undef _TIME_BITS
+#include <time.h>
+]])
+ AS_IF([test $ac_cv_sizeof_time_t = 8], [
+ libbsd_sys_time_bits=64
+ ], [
+ libbsd_sys_time_bits=32
+ ])
+ AC_DEFINE_UNQUOTED([LIBBSD_SYS_TIME_BITS], [$libbsd_sys_time_bits],
+ [The number of bits for the default system time_t ABI])
+ AC_SUBST([LIBBSD_SYS_TIME_BITS], [$libbsd_sys_time_bits])
+ AS_UNSET([ac_cv_sizeof_time_t])
+ AM_CONDITIONAL([LIBBSD_SYS_IS_TIME32], [test "$libbsd_sys_time_bits" -eq 32])
+
+ # Check the whether the system supports 64-bit time_t.
+ AC_CHECK_SIZEOF([time_t], [], [[
+#define _FILE_OFFSET_BITS 64
+#define _TIME_BITS 64
+#include <time.h>
+]])
+ AS_IF([test $ac_cv_sizeof_time_t = 8], [
+ libbsd_sys_has_time64=1
+ ], [
+ libbsd_sys_has_time64=0
+ ])
+ AC_DEFINE_UNQUOTED([LIBBSD_SYS_HAS_TIME64], [$libbsd_sys_has_time64],
+ [Enable if the system supports 64-bit time_t])
+ AC_SUBST([LIBBSD_SYS_HAS_TIME64], [$libbsd_sys_has_time64])
+ AM_CONDITIONAL([LIBBSD_SYS_HAS_TIME64], [test "$libbsd_sys_has_time64" -eq 1])
+ AS_IF([test "$libbsd_sys_time_bits" = 32 && \
+ test "$libbsd_sys_has_time64" -eq 1], [
+ abi_time64=yes
+ ], [
+ abi_time64=no
+ ])
+ LIBBSD_SELECT_ABI([time64], [explicit time64 time_t support])
+
+ AS_IF([test $ac_cv_sys_file_offset_bits = 64 && \
+ test $libbsd_sys_time_bits = 32 && \
+ test $ac_cv_sizeof_time_t = 8], [
+ AC_DEFINE([_TIME_BITS], [64], [Enable 64-bit time_t support])
+ ])
+])
diff --git a/src/libbsd.map.in b/src/libbsd.map.in
index 9670d4c..d7047dc 100644
--- a/src/libbsd.map.in
+++ b/src/libbsd.map.in
@@ -305,4 +305,15 @@ LIBBSD_0.12.0 {
vasprintf;
asprintf;
#endif
+
+#if LIBBSD_ABI_TIMECONV && LIBBSD_ABI_TIME64
+ _time32_to_time_time64;
+ _time_to_time32_time64;
+ _time64_to_time_time64;
+ _time_to_time64_time64;
+ _time_to_long_time64;
+ _long_to_time_time64;
+ _time_to_int_time64;
+ _int_to_time_time64;
+#endif
} LIBBSD_0.11.0;
diff --git a/src/timeconv.c b/src/timeconv.c
index 11f926e..0c122e4 100644
--- a/src/timeconv.c
+++ b/src/timeconv.c
@@ -32,17 +32,161 @@
#include <sys/cdefs.h>
#include <sys/types.h>
-#include <timeconv.h>
+
+#include "local-link.h"
+
+#if LIBBSD_SYS_TIME_BITS == 64
+/*
+ * - enable time64 functions
+ * - symver libbsd<func>_time64 -> <func> 0.7
+ */
+#define time32_symbol(name)
+#define time64_symbol(name) \
+ libbsd_strong_alias(libbsd ## name ## _time64, name)
+#elif LIBBSD_SYS_TIME_BITS == 32 && LIBBSD_SYS_HAS_TIME64
+/*
+ * - enable time32 functions
+ * - enable time64 functions
+ * - symver libbsd<func>_time32 -> <func> 0.7
+ * - map libbsd<func>_time64 <func>_time64 0.12
+ */
+#define time32_symbol(name) \
+ libbsd_strong_alias(libbsd ## name ## _time32, name)
+#define time64_symbol(name) \
+ libbsd_strong_alias(libbsd ## name ## _time64, name ## _time64)
+#elif LIBBSD_SYS_TIME_BITS == 32 && !LIBBSD_SYS_HAS_TIME64
+/*
+ * - enable time32 functions
+ * - symver libbsd<func>_time32 -> <func>
+ */
+#define time32_symbol(name) \
+ libbsd_strong_alias(libbsd ## name ## _time32, name)
+#define time64_symbol(name)
+#else
+#error "Unknown time_t support"
+#endif
+
+#if LIBBSD_SYS_HAS_TIME64
+typedef int64_t libbsd_time64_t;
/*
* Convert a 32 bit representation of time_t into time_t. XXX needs to
* implement the 50-year rule to handle post-2038 conversions.
*/
-time_t
-_time32_to_time(int32_t t32)
+libbsd_time64_t
+libbsd_time32_to_time_time64(int32_t t32);
+libbsd_time64_t
+libbsd_time32_to_time_time64(int32_t t32)
+{
+ return((libbsd_time64_t)t32);
+}
+time64_symbol(_time32_to_time);
+
+/*
+ * Convert time_t to a 32 bit representation. If time_t is 64 bits we can
+ * simply chop it down. The resulting 32 bit representation can be
+ * converted back to a temporally local 64 bit time_t using time32_to_time.
+ */
+int32_t
+libbsd_time_to_time32_time64(libbsd_time64_t t);
+int32_t
+libbsd_time_to_time32_time64(libbsd_time64_t t)
+{
+ return((int32_t)t);
+}
+time64_symbol(_time_to_time32);
+
+/*
+ * Convert a 64 bit representation of time_t into time_t. If time_t is
+ * represented as 32 bits we can simply chop it and not support times
+ * past 2038.
+ */
+libbsd_time64_t
+libbsd_time64_to_time_time64(int64_t t64);
+libbsd_time64_t
+libbsd_time64_to_time_time64(int64_t t64)
+{
+ return((libbsd_time64_t)t64);
+}
+time64_symbol(_time64_to_time);
+
+/*
+ * Convert time_t to a 64 bit representation. If time_t is represented
+ * as 32 bits we simply sign-extend and do not support times past 2038.
+ */
+int64_t
+libbsd_time_to_time64_time64(libbsd_time64_t t);
+int64_t
+libbsd_time_to_time64_time64(libbsd_time64_t t)
+{
+ return((int64_t)t);
+}
+time64_symbol(_time_to_time64);
+
+/*
+ * Convert to/from 'long'. Depending on the sizeof(long) this may or
+ * may not require using the 50-year rule.
+ */
+long
+libbsd_time_to_long_time64(libbsd_time64_t t);
+long
+libbsd_time_to_long_time64(libbsd_time64_t t)
+{
+ if (sizeof(long) == sizeof(int64_t))
+ return(libbsd_time_to_time64_time64(t));
+ return((long)t);
+}
+time64_symbol(_time_to_long);
+
+libbsd_time64_t
+libbsd_long_to_time_time64(long tlong);
+libbsd_time64_t
+libbsd_long_to_time_time64(long tlong)
+{
+ if (sizeof(long) == sizeof(int32_t))
+ return(libbsd_time32_to_time_time64(tlong));
+ return((libbsd_time64_t)tlong);
+}
+time64_symbol(_long_to_time);
+
+/*
+ * Convert to/from 'int'. Depending on the sizeof(int) this may or
+ * may not require using the 50-year rule.
+ */
+int
+libbsd_time_to_int_time64(time_t t);
+int
+libbsd_time_to_int_time64(time_t t)
{
- return((time_t)t32);
+ if (sizeof(int) == sizeof(int64_t))
+ return(libbsd_time_to_time64_time64(t));
+ return((int)t);
}
+time64_symbol(_time_to_int);
+
+libbsd_time64_t
+libbsd_int_to_time_time64(int tint);
+libbsd_time64_t
+libbsd_int_to_time_time64(int tint)
+{
+ if (sizeof(int) == sizeof(int32_t))
+ return(libbsd_time32_to_time_time64(tint));
+ return((libbsd_time64_t)tint);
+}
+time64_symbol(_int_to_time);
+#endif
+
+#if LIBBSD_SYS_TIME_BITS == 32
+typedef int32_t libbsd_time32_t;
+
+libbsd_time32_t
+libbsd_time32_to_time_time32(int32_t t32);
+libbsd_time32_t
+libbsd_time32_to_time_time32(int32_t t32)
+{
+ return((libbsd_time32_t)t32);
+}
+time32_symbol(_time32_to_time);
/*
* Convert time_t to a 32 bit representation. If time_t is 64 bits we can
@@ -50,68 +194,90 @@ _time32_to_time(int32_t t32)
* converted back to a temporally local 64 bit time_t using time32_to_time.
*/
int32_t
-_time_to_time32(time_t t)
+libbsd_time_to_time32_time32(libbsd_time32_t t);
+int32_t
+libbsd_time_to_time32_time32(libbsd_time32_t t)
{
return((int32_t)t);
}
+time32_symbol(_time_to_time32);
/*
* Convert a 64 bit representation of time_t into time_t. If time_t is
* represented as 32 bits we can simply chop it and not support times
* past 2038.
*/
-time_t
-_time64_to_time(int64_t t64)
+libbsd_time32_t
+libbsd_time64_to_time_time32(int64_t t64);
+libbsd_time32_t
+libbsd_time64_to_time_time32(int64_t t64)
{
- return((time_t)t64);
+ return((libbsd_time32_t)t64);
}
+time32_symbol(_time64_to_time);
/*
* Convert time_t to a 64 bit representation. If time_t is represented
* as 32 bits we simply sign-extend and do not support times past 2038.
*/
int64_t
-_time_to_time64(time_t t)
+libbsd_time_to_time64_time32(libbsd_time32_t t);
+int64_t
+libbsd_time_to_time64_time32(libbsd_time32_t t)
{
return((int64_t)t);
}
+time32_symbol(_time_to_time64);
/*
* Convert to/from 'long'. Depending on the sizeof(long) this may or
* may not require using the 50-year rule.
*/
long
-_time_to_long(time_t t)
+libbsd_time_to_long_time32(libbsd_time32_t t);
+long
+libbsd_time_to_long_time32(libbsd_time32_t t)
{
if (sizeof(long) == sizeof(int64_t))
- return(_time_to_time64(t));
+ return(libbsd_time_to_time64_time32(t));
return((long)t);
}
+time32_symbol(_time_to_long);
-time_t
-_long_to_time(long tlong)
+libbsd_time32_t
+libbsd_long_to_time_time32(long tlong);
+libbsd_time32_t
+libbsd_long_to_time_time32(long tlong)
{
if (sizeof(long) == sizeof(int32_t))
- return(_time32_to_time(tlong));
- return((time_t)tlong);
+ return(libbsd_time32_to_time_time32(tlong));
+ return((libbsd_time32_t)tlong);
}
+time32_symbol(_long_to_time);
/*
* Convert to/from 'int'. Depending on the sizeof(int) this may or
* may not require using the 50-year rule.
*/
int
-_time_to_int(time_t t)
+libbsd_time_to_int_time32(libbsd_time32_t t);
+int
+libbsd_time_to_int_time32(libbsd_time32_t t)
{
if (sizeof(int) == sizeof(int64_t))
- return(_time_to_time64(t));
+ return(libbsd_time_to_time64_time32(t));
return((int)t);
}
+time32_symbol(_time_to_int);
-time_t
-_int_to_time(int tint)
+libbsd_time32_t
+libbsd_int_to_time_time32(int tint);
+libbsd_time32_t
+libbsd_int_to_time_time32(int tint)
{
if (sizeof(int) == sizeof(int32_t))
- return(_time32_to_time(tint));
- return((time_t)tint);
+ return(libbsd_time32_to_time_time32(tint));
+ return((libbsd_time32_t)tint);
}
+time32_symbol(_int_to_time);
+#endif
diff --git a/test/.gitignore b/test/.gitignore
index a80f854..bfd5572 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -22,5 +22,7 @@ strl
strmode
strnstr
strtonum
+timeconv32
+timeconv64
vis
vis-openbsd
diff --git a/test/Makefile.am b/test/Makefile.am
index 1c64abb..13c3309 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -182,4 +182,12 @@ check_PROGRAMS += \
# EOL
endif
+if LIBBSD_SYS_IS_TIME32
+check_PROGRAMS += timeconv32
+endif
+
+if LIBBSD_SYS_HAS_TIME64
+check_PROGRAMS += timeconv64
+endif
+
TESTS = $(check_SCRIPTS) $(check_PROGRAMS)
diff --git a/test/timeconv32.c b/test/timeconv32.c
new file mode 100644
index 0000000..549e612
--- /dev/null
+++ b/test/timeconv32.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2024 Guillem Jover <guillem@hadrons.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#undef _TIME_BITS
+
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <timeconv.h>
+
+int
+main(int argc, char **argv)
+{
+ time_t t;
+
+ t = _time64_to_time(INT64_MAX);
+ assert(t < INT64_MAX);
+
+ return 0;
+}
diff --git a/test/timeconv64.c b/test/timeconv64.c
new file mode 100644
index 0000000..d175ed2
--- /dev/null
+++ b/test/timeconv64.c
@@ -0,0 +1,40 @@
+/*
+ * Copyright © 2024 Guillem Jover <guillem@hadrons.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <assert.h>
+#include <stdint.h>
+#include <timeconv.h>
+
+int
+main(int argc, char **argv)
+{
+ time_t t;
+
+ t = _time64_to_time(INT64_MAX);
+ assert(t == INT64_MAX);
+
+ return 0;
+}