1
0
mirror of https://frontier.innolan.net/rainlance/amiga-tz.git synced 2025-11-19 21:49:22 +00:00
Files
amiga-tz/difftime.c

49 lines
1.4 KiB
C
Raw Normal View History

/*
** This file is in the public domain, so clarified as of
** 1996-06-05 by Arthur David Olson.
*/
/*LINTLIBRARY*/
#include "private.h" /* for time_t and TYPE_SIGNED */
Port to NetBSD, where, e.g., 'time_t' is wider than 'long'. I audited the code and fixed as many width-asssumptions as I could find, including several places where the code assumed that 'time_t' was no wider than 'long'; this assumption is not true on 32-bit NetBSD platforms. This caught every problem that is already fixed in the NetBSD zic.c, and caught quite a few more. * Makefile: Add comments re HAVE_DOS_FILE_NAMES and HAVE_INTTYPES_H. * date.c (checkfinal, netsettime): Don't use 'long' where 'int' will do. * difftime.c (difftime): Mark with ATTRIBUTE_CONST. Use uintmax_t, not unsigned long, for the widest unsigned integer type. Use long double, not double, if time_t is wider than uintmax_t; this can in theory help on nonstandard platforms, such as GCC with 64-bit uintmax_t and 128-bit __int128_t. * localtime.c (struct ttinfo.tt_gmtoff, struct rule.r_time) (detzcode, getsecs, getoffset, gmtsub, localsub, increment_overflow32) (normalize_overflow32, time1, time2, timesub, transtime, tzparse) (time2sub, timeoff, gtime): * tzfile.h (SECSPERDAY): * zdump.c (SECSPERDAY): * zic.c (convert, puttzcode): Use int_fast32_t, not long, when all we care is that values up to 2**31 can be stored. This doesn't fix any bugs, but it allows more opportunity for compiler optimization. (struct lsinfo.ls_corr, timesub, leapcorr): Use int_fast64_t, not long, when values up to 2**63 can be stored. (timesub): Make it clearer when we are truncating 0.5 to 0. (increment_overflow32): Rename from long_increment_overflow. All uses changed. (normalize_overflow32): Rename from long_normalize_overflow. All uses changed. * private.h (HAVE_INTTYPES_H, ATTRIBUTE_CONST): New macros. Include <inttypes.h> if HAVE_INTTYPES_H. (INT_FAST64_MIN, INT_FAST64_MAX, SCNdFAST64, int_fast32_t, PRIdMAX) (uintmax_t, PRIuMAX, _Noreturn): Define to reasonable values if it's an older compiler. * scheck.c (scheck): Add support for arbitrary formats, such as those that SCNdFAST64 can expand to, at the price of no longer supporting weird conversion specs like "%[%]". * strftime.c (_fmt): Use intmax_t and uintmax_t to format time_t, not long and unsigned long. * zdump.c (int_fast32_t, intmax_t, PRIdMAX, SCNdMAX): Define for pre-C99 compilers, like private.h does. (delta, yeartot, main): Use intmax_t, not long. (hunt): Use time_t, not long, since the diff must be nonnegative. (tformat): Allow for time_t wider than long. * zic.c (ZIC_MIN, ZIC_MAX, SCNdZIC): New macros. (OFFSET_STRLEN_MAXIMUM, RULE_STRLEN_MAXIMUM): Remove. (struct rule): Make r_loyear, r_hiyear, r_tod, r_stdoff, z_gmtoff, z_stdoff zic_t, not long. (addtype, gethms, oadd, rpytime, tadd, gmtoffs, corr, inleap) (stringoffset, stringrule, outzone, addtype, adjleap, rpytime) (LDAYSPERWEEK): Use zic_t, not long. (leapminyear, leapmaxyear, min_year, max_year, rulesub, updateminmax) (outzone, rpytime): Use zic_t, not int. (usage): Now _Noreturn. (main): Use S_IWGRP, not 'unix', to determine whether to call umask. (writezone): Omit unnecessary cast. (mkdirs): Use HAVE_DOS_FILE_NAMES, not 'unix', to determine whether to parse DOS file anmes. (eitol): Remove; no longer needed.
2013-05-27 21:26:18 -07:00
double ATTRIBUTE_CONST
Assume C89. * Makefile (GCC_DEBUG_FLAGS): Modernize for GCC 4.6.3 x86. * asctime.c (asctime_r, asctime): * date.c (main, dogmt, reset, wildinput, nondigit, oops, display) (timeout, sametm, netsettime): * difftime.c (difftime): * ialloc.c (icatalloc, icpyalloc): * localtime.c (detzcode, detzcode64, differ_by_repeat) (tzload, typesequiv, getzname, getnum, getsecs, getoffset) (getrule, transtime, tzparse, gmtload, tzsetwall, tzset) (localsub, localtime, localtime_r, gmtsub, gmtime, gmtime_r) (offtime, leaps_thru_end_of, timesub, ctime, ctime_r) (increment_overflow, long_increment_overflow) (normalize_overflow, long_normalize_overflow, tmcomp, time2sub) (time2, time1, mktime, timelocal, timegm, timeoff, gtime) (leapcorr, time2posix, posix2time): * scheck.c (scheck): * strftime.c (strftime, _fmt, _conv, _add, _yconv, _loc): * zdump.c (my_localtime, abbrok, usage, main, yeartot) (delta, abbr, dumptime): * zic.c (memcheck, eats, eat, error, warning, main) (dolink, itsdir, rcomp, associate, infile, gethms, inrule) (inzone, inzcont, inzsub, inleap, inlink, rulesub, convert) (convert64, puttzcode, puttzcode64, atcomp, is32, writezone, DO) (doabbr, updateminmax, stringoffset, stringrule, stringzone) (outzone, addtt, addtype, leapadd, yearistype, lowerit, ciequal) (itsabbr, byword, getfields, oadd, tadd, rpytime, newabbr) (mkdirs, eitol): Assume C89 or better. Mostly this consists of using function prototypes. In a few places, prototypes are required for portability to hosts where time_t does not promote to itself and where a preceding prototype does not override a definition. But while we're at it we might as well be consistent: it's safe to assume at-least-C89 these days. * ialloc.c (nonzero, imalloc, icalloc, irealloc, ifree, icfree): Remove; no longer needed now that we assume C89 or better. All callers changed to use malloc, calloc, realloc, free. * localtime.c (getzname, getqzname, leaps_thru_end_of, transtime): * zdump.c (delta, yeartot): * zic.c (eitol): Now pure. * private.h, zdump.c (ATTRIBUTE_PURE): New macro. * private.h (icalloc, imalloc, irealloc, icfree, ifree): Remove decls. * zic.c: Remove no-longer-necessary forward decls. (max_time, min_time): Now const. (setboundaries): Remove.
2012-10-12 07:53:12 -07:00
difftime(const time_t time1, const time_t time0)
{
2004-11-01 13:18:56 -05:00
/*
** If (sizeof (double) > sizeof (time_t)) simply convert and subtract
** (assuming that the larger type has more precision).
*/
if (sizeof (double) > sizeof (time_t))
return (double) time1 - (double) time0;
2004-10-26 12:24:05 -04:00
if (!TYPE_SIGNED(time_t)) {
2004-10-25 12:50:55 -04:00
/*
** The difference of two unsigned values can't overflow
** if the minuend is greater than or equal to the subtrahend.
*/
if (time1 >= time0)
Port to NetBSD, where, e.g., 'time_t' is wider than 'long'. I audited the code and fixed as many width-asssumptions as I could find, including several places where the code assumed that 'time_t' was no wider than 'long'; this assumption is not true on 32-bit NetBSD platforms. This caught every problem that is already fixed in the NetBSD zic.c, and caught quite a few more. * Makefile: Add comments re HAVE_DOS_FILE_NAMES and HAVE_INTTYPES_H. * date.c (checkfinal, netsettime): Don't use 'long' where 'int' will do. * difftime.c (difftime): Mark with ATTRIBUTE_CONST. Use uintmax_t, not unsigned long, for the widest unsigned integer type. Use long double, not double, if time_t is wider than uintmax_t; this can in theory help on nonstandard platforms, such as GCC with 64-bit uintmax_t and 128-bit __int128_t. * localtime.c (struct ttinfo.tt_gmtoff, struct rule.r_time) (detzcode, getsecs, getoffset, gmtsub, localsub, increment_overflow32) (normalize_overflow32, time1, time2, timesub, transtime, tzparse) (time2sub, timeoff, gtime): * tzfile.h (SECSPERDAY): * zdump.c (SECSPERDAY): * zic.c (convert, puttzcode): Use int_fast32_t, not long, when all we care is that values up to 2**31 can be stored. This doesn't fix any bugs, but it allows more opportunity for compiler optimization. (struct lsinfo.ls_corr, timesub, leapcorr): Use int_fast64_t, not long, when values up to 2**63 can be stored. (timesub): Make it clearer when we are truncating 0.5 to 0. (increment_overflow32): Rename from long_increment_overflow. All uses changed. (normalize_overflow32): Rename from long_normalize_overflow. All uses changed. * private.h (HAVE_INTTYPES_H, ATTRIBUTE_CONST): New macros. Include <inttypes.h> if HAVE_INTTYPES_H. (INT_FAST64_MIN, INT_FAST64_MAX, SCNdFAST64, int_fast32_t, PRIdMAX) (uintmax_t, PRIuMAX, _Noreturn): Define to reasonable values if it's an older compiler. * scheck.c (scheck): Add support for arbitrary formats, such as those that SCNdFAST64 can expand to, at the price of no longer supporting weird conversion specs like "%[%]". * strftime.c (_fmt): Use intmax_t and uintmax_t to format time_t, not long and unsigned long. * zdump.c (int_fast32_t, intmax_t, PRIdMAX, SCNdMAX): Define for pre-C99 compilers, like private.h does. (delta, yeartot, main): Use intmax_t, not long. (hunt): Use time_t, not long, since the diff must be nonnegative. (tformat): Allow for time_t wider than long. * zic.c (ZIC_MIN, ZIC_MAX, SCNdZIC): New macros. (OFFSET_STRLEN_MAXIMUM, RULE_STRLEN_MAXIMUM): Remove. (struct rule): Make r_loyear, r_hiyear, r_tod, r_stdoff, z_gmtoff, z_stdoff zic_t, not long. (addtype, gethms, oadd, rpytime, tadd, gmtoffs, corr, inleap) (stringoffset, stringrule, outzone, addtype, adjleap, rpytime) (LDAYSPERWEEK): Use zic_t, not long. (leapminyear, leapmaxyear, min_year, max_year, rulesub, updateminmax) (outzone, rpytime): Use zic_t, not int. (usage): Now _Noreturn. (main): Use S_IWGRP, not 'unix', to determine whether to call umask. (writezone): Omit unnecessary cast. (mkdirs): Use HAVE_DOS_FILE_NAMES, not 'unix', to determine whether to parse DOS file anmes. (eitol): Remove; no longer needed.
2013-05-27 21:26:18 -07:00
return time1 - time0;
else return -(double) (time0 - time1);
}
2004-10-26 12:24:05 -04:00
/*
** Handle cases where both time1 and time0 have the same sign
** (meaning that their difference cannot overflow).
*/
if ((time1 < 0) == (time0 < 0))
2004-10-26 12:24:05 -04:00
return time1 - time0;
/*
** time1 and time0 have opposite signs.
Port to NetBSD, where, e.g., 'time_t' is wider than 'long'. I audited the code and fixed as many width-asssumptions as I could find, including several places where the code assumed that 'time_t' was no wider than 'long'; this assumption is not true on 32-bit NetBSD platforms. This caught every problem that is already fixed in the NetBSD zic.c, and caught quite a few more. * Makefile: Add comments re HAVE_DOS_FILE_NAMES and HAVE_INTTYPES_H. * date.c (checkfinal, netsettime): Don't use 'long' where 'int' will do. * difftime.c (difftime): Mark with ATTRIBUTE_CONST. Use uintmax_t, not unsigned long, for the widest unsigned integer type. Use long double, not double, if time_t is wider than uintmax_t; this can in theory help on nonstandard platforms, such as GCC with 64-bit uintmax_t and 128-bit __int128_t. * localtime.c (struct ttinfo.tt_gmtoff, struct rule.r_time) (detzcode, getsecs, getoffset, gmtsub, localsub, increment_overflow32) (normalize_overflow32, time1, time2, timesub, transtime, tzparse) (time2sub, timeoff, gtime): * tzfile.h (SECSPERDAY): * zdump.c (SECSPERDAY): * zic.c (convert, puttzcode): Use int_fast32_t, not long, when all we care is that values up to 2**31 can be stored. This doesn't fix any bugs, but it allows more opportunity for compiler optimization. (struct lsinfo.ls_corr, timesub, leapcorr): Use int_fast64_t, not long, when values up to 2**63 can be stored. (timesub): Make it clearer when we are truncating 0.5 to 0. (increment_overflow32): Rename from long_increment_overflow. All uses changed. (normalize_overflow32): Rename from long_normalize_overflow. All uses changed. * private.h (HAVE_INTTYPES_H, ATTRIBUTE_CONST): New macros. Include <inttypes.h> if HAVE_INTTYPES_H. (INT_FAST64_MIN, INT_FAST64_MAX, SCNdFAST64, int_fast32_t, PRIdMAX) (uintmax_t, PRIuMAX, _Noreturn): Define to reasonable values if it's an older compiler. * scheck.c (scheck): Add support for arbitrary formats, such as those that SCNdFAST64 can expand to, at the price of no longer supporting weird conversion specs like "%[%]". * strftime.c (_fmt): Use intmax_t and uintmax_t to format time_t, not long and unsigned long. * zdump.c (int_fast32_t, intmax_t, PRIdMAX, SCNdMAX): Define for pre-C99 compilers, like private.h does. (delta, yeartot, main): Use intmax_t, not long. (hunt): Use time_t, not long, since the diff must be nonnegative. (tformat): Allow for time_t wider than long. * zic.c (ZIC_MIN, ZIC_MAX, SCNdZIC): New macros. (OFFSET_STRLEN_MAXIMUM, RULE_STRLEN_MAXIMUM): Remove. (struct rule): Make r_loyear, r_hiyear, r_tod, r_stdoff, z_gmtoff, z_stdoff zic_t, not long. (addtype, gethms, oadd, rpytime, tadd, gmtoffs, corr, inleap) (stringoffset, stringrule, outzone, addtype, adjleap, rpytime) (LDAYSPERWEEK): Use zic_t, not long. (leapminyear, leapmaxyear, min_year, max_year, rulesub, updateminmax) (outzone, rpytime): Use zic_t, not int. (usage): Now _Noreturn. (main): Use S_IWGRP, not 'unix', to determine whether to call umask. (writezone): Omit unnecessary cast. (mkdirs): Use HAVE_DOS_FILE_NAMES, not 'unix', to determine whether to parse DOS file anmes. (eitol): Remove; no longer needed.
2013-05-27 21:26:18 -07:00
** Punt if uintmax_t is too narrow.
** This suffers from double rounding; attempt to lessen that
** by using long double temporaries.
2004-10-26 12:24:05 -04:00
*/
Port to NetBSD, where, e.g., 'time_t' is wider than 'long'. I audited the code and fixed as many width-asssumptions as I could find, including several places where the code assumed that 'time_t' was no wider than 'long'; this assumption is not true on 32-bit NetBSD platforms. This caught every problem that is already fixed in the NetBSD zic.c, and caught quite a few more. * Makefile: Add comments re HAVE_DOS_FILE_NAMES and HAVE_INTTYPES_H. * date.c (checkfinal, netsettime): Don't use 'long' where 'int' will do. * difftime.c (difftime): Mark with ATTRIBUTE_CONST. Use uintmax_t, not unsigned long, for the widest unsigned integer type. Use long double, not double, if time_t is wider than uintmax_t; this can in theory help on nonstandard platforms, such as GCC with 64-bit uintmax_t and 128-bit __int128_t. * localtime.c (struct ttinfo.tt_gmtoff, struct rule.r_time) (detzcode, getsecs, getoffset, gmtsub, localsub, increment_overflow32) (normalize_overflow32, time1, time2, timesub, transtime, tzparse) (time2sub, timeoff, gtime): * tzfile.h (SECSPERDAY): * zdump.c (SECSPERDAY): * zic.c (convert, puttzcode): Use int_fast32_t, not long, when all we care is that values up to 2**31 can be stored. This doesn't fix any bugs, but it allows more opportunity for compiler optimization. (struct lsinfo.ls_corr, timesub, leapcorr): Use int_fast64_t, not long, when values up to 2**63 can be stored. (timesub): Make it clearer when we are truncating 0.5 to 0. (increment_overflow32): Rename from long_increment_overflow. All uses changed. (normalize_overflow32): Rename from long_normalize_overflow. All uses changed. * private.h (HAVE_INTTYPES_H, ATTRIBUTE_CONST): New macros. Include <inttypes.h> if HAVE_INTTYPES_H. (INT_FAST64_MIN, INT_FAST64_MAX, SCNdFAST64, int_fast32_t, PRIdMAX) (uintmax_t, PRIuMAX, _Noreturn): Define to reasonable values if it's an older compiler. * scheck.c (scheck): Add support for arbitrary formats, such as those that SCNdFAST64 can expand to, at the price of no longer supporting weird conversion specs like "%[%]". * strftime.c (_fmt): Use intmax_t and uintmax_t to format time_t, not long and unsigned long. * zdump.c (int_fast32_t, intmax_t, PRIdMAX, SCNdMAX): Define for pre-C99 compilers, like private.h does. (delta, yeartot, main): Use intmax_t, not long. (hunt): Use time_t, not long, since the diff must be nonnegative. (tformat): Allow for time_t wider than long. * zic.c (ZIC_MIN, ZIC_MAX, SCNdZIC): New macros. (OFFSET_STRLEN_MAXIMUM, RULE_STRLEN_MAXIMUM): Remove. (struct rule): Make r_loyear, r_hiyear, r_tod, r_stdoff, z_gmtoff, z_stdoff zic_t, not long. (addtype, gethms, oadd, rpytime, tadd, gmtoffs, corr, inleap) (stringoffset, stringrule, outzone, addtype, adjleap, rpytime) (LDAYSPERWEEK): Use zic_t, not long. (leapminyear, leapmaxyear, min_year, max_year, rulesub, updateminmax) (outzone, rpytime): Use zic_t, not int. (usage): Now _Noreturn. (main): Use S_IWGRP, not 'unix', to determine whether to call umask. (writezone): Omit unnecessary cast. (mkdirs): Use HAVE_DOS_FILE_NAMES, not 'unix', to determine whether to parse DOS file anmes. (eitol): Remove; no longer needed.
2013-05-27 21:26:18 -07:00
if (sizeof (uintmax_t) < sizeof (time_t))
return (long double) time1 - (long double) time0;
2004-10-26 12:24:05 -04:00
/*
** Stay calm...decent optimizers will eliminate the complexity below.
*/
if (time1 >= 0 /* && time0 < 0 */)
Port to NetBSD, where, e.g., 'time_t' is wider than 'long'. I audited the code and fixed as many width-asssumptions as I could find, including several places where the code assumed that 'time_t' was no wider than 'long'; this assumption is not true on 32-bit NetBSD platforms. This caught every problem that is already fixed in the NetBSD zic.c, and caught quite a few more. * Makefile: Add comments re HAVE_DOS_FILE_NAMES and HAVE_INTTYPES_H. * date.c (checkfinal, netsettime): Don't use 'long' where 'int' will do. * difftime.c (difftime): Mark with ATTRIBUTE_CONST. Use uintmax_t, not unsigned long, for the widest unsigned integer type. Use long double, not double, if time_t is wider than uintmax_t; this can in theory help on nonstandard platforms, such as GCC with 64-bit uintmax_t and 128-bit __int128_t. * localtime.c (struct ttinfo.tt_gmtoff, struct rule.r_time) (detzcode, getsecs, getoffset, gmtsub, localsub, increment_overflow32) (normalize_overflow32, time1, time2, timesub, transtime, tzparse) (time2sub, timeoff, gtime): * tzfile.h (SECSPERDAY): * zdump.c (SECSPERDAY): * zic.c (convert, puttzcode): Use int_fast32_t, not long, when all we care is that values up to 2**31 can be stored. This doesn't fix any bugs, but it allows more opportunity for compiler optimization. (struct lsinfo.ls_corr, timesub, leapcorr): Use int_fast64_t, not long, when values up to 2**63 can be stored. (timesub): Make it clearer when we are truncating 0.5 to 0. (increment_overflow32): Rename from long_increment_overflow. All uses changed. (normalize_overflow32): Rename from long_normalize_overflow. All uses changed. * private.h (HAVE_INTTYPES_H, ATTRIBUTE_CONST): New macros. Include <inttypes.h> if HAVE_INTTYPES_H. (INT_FAST64_MIN, INT_FAST64_MAX, SCNdFAST64, int_fast32_t, PRIdMAX) (uintmax_t, PRIuMAX, _Noreturn): Define to reasonable values if it's an older compiler. * scheck.c (scheck): Add support for arbitrary formats, such as those that SCNdFAST64 can expand to, at the price of no longer supporting weird conversion specs like "%[%]". * strftime.c (_fmt): Use intmax_t and uintmax_t to format time_t, not long and unsigned long. * zdump.c (int_fast32_t, intmax_t, PRIdMAX, SCNdMAX): Define for pre-C99 compilers, like private.h does. (delta, yeartot, main): Use intmax_t, not long. (hunt): Use time_t, not long, since the diff must be nonnegative. (tformat): Allow for time_t wider than long. * zic.c (ZIC_MIN, ZIC_MAX, SCNdZIC): New macros. (OFFSET_STRLEN_MAXIMUM, RULE_STRLEN_MAXIMUM): Remove. (struct rule): Make r_loyear, r_hiyear, r_tod, r_stdoff, z_gmtoff, z_stdoff zic_t, not long. (addtype, gethms, oadd, rpytime, tadd, gmtoffs, corr, inleap) (stringoffset, stringrule, outzone, addtype, adjleap, rpytime) (LDAYSPERWEEK): Use zic_t, not long. (leapminyear, leapmaxyear, min_year, max_year, rulesub, updateminmax) (outzone, rpytime): Use zic_t, not int. (usage): Now _Noreturn. (main): Use S_IWGRP, not 'unix', to determine whether to call umask. (writezone): Omit unnecessary cast. (mkdirs): Use HAVE_DOS_FILE_NAMES, not 'unix', to determine whether to parse DOS file anmes. (eitol): Remove; no longer needed.
2013-05-27 21:26:18 -07:00
return (uintmax_t) time1 + (uintmax_t) (-1 - time0) + 1;
return -(double) ((uintmax_t) time0 + (uintmax_t) (-1 - time1) + 1);
}