diff --git a/parse_posix.c b/parse_posix.c index 204b328..424876f 100644 --- a/parse_posix.c +++ b/parse_posix.c @@ -490,6 +490,14 @@ timelib_sll timelib_ts_at_start_of_year(timelib_sll year) timelib_sll epoch_leap_years = count_leap_years(1970); timelib_sll current_leap_years = count_leap_years(year); + /* FIXME: handle error instead of clamping result */ + if (year < (TIMELIB_SLL_MIN / SECS_PER_DAY + epoch_leap_years - current_leap_years) / DAYS_PER_YEAR + 1970) { + return TIMELIB_SLL_MIN; + } + if (year > (TIMELIB_SLL_MAX / SECS_PER_DAY + epoch_leap_years - current_leap_years) / DAYS_PER_YEAR + 1970) { + return TIMELIB_SLL_MAX; + } + return SECS_PER_DAY * ( ((year-1970) * DAYS_PER_YEAR) + current_leap_years diff --git a/timelib.h b/timelib.h index 5a92366..a03b8e0 100644 --- a/timelib.h +++ b/timelib.h @@ -65,10 +65,14 @@ typedef uint32_t timelib_ulong; typedef uint64_t timelib_ull; typedef int64_t timelib_sll; # define TIMELIB_LL_CONST(n) n ## i64 +# define TIMELIB_SLL_MIN INT64_MIN +# define TIMELIB_SLL_MAX INT64_MAX #else typedef unsigned long long timelib_ull; typedef signed long long timelib_sll; # define TIMELIB_LL_CONST(n) n ## ll +# define TIMELIB_SLL_MIN LLONG_MIN +# define TIMELIB_SLL_MAX LLONG_MAX #endif typedef struct _ttinfo ttinfo;