| 1 |
// Copyright (c) 2022, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
// Time conversion to/from POSIX time_t and struct tm, with no support |
| 5 |
// for time zones other than UTC |
| 6 |
|
| 7 |
#include <assert.h> |
| 8 |
#include <inttypes.h> |
| 9 |
#include <limits.h> |
| 10 |
#include <string.h> |
| 11 |
#include <time.h> |
| 12 |
|
| 13 |
#include "internal.h" |
| 14 |
|
| 15 |
#define SECS_PER_HOUR (60 * 60) |
| 16 |
#define SECS_PER_DAY (INT64_C(24) * SECS_PER_HOUR) |
| 17 |
|
| 18 |
|
| 19 |
// Is a year/month/day combination valid, in the range from year 0000 |
| 20 |
// to 9999? |
| 21 |
static int is_valid_date(int64_t year, int64_t month, int64_t day) { |
| 22 |
if (day < 1 || month < 1 || year < 0 || year > 9999) { |
| 23 |
return 0; |
| 24 |
} |
| 25 |
switch (month) { |
| 26 |
case 1: |
| 27 |
case 3: |
| 28 |
case 5: |
| 29 |
case 7: |
| 30 |
case 8: |
| 31 |
case 10: |
| 32 |
case 12: |
| 33 |
return day > 0 && day <= 31; |
| 34 |
case 4: |
| 35 |
case 6: |
| 36 |
case 9: |
| 37 |
case 11: |
| 38 |
return day > 0 && day <= 30; |
| 39 |
case 2: |
| 40 |
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { |
| 41 |
return day > 0 && day <= 29; |
| 42 |
} else { |
| 43 |
return day > 0 && day <= 28; |
| 44 |
} |
| 45 |
default: |
| 46 |
return 0; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Is a time valid? Leap seconds of 60 are not considered valid, as |
| 51 |
// the POSIX time in seconds does not include them. |
| 52 |
static int is_valid_time(int64_t hours, int64_t minutes, int64_t seconds) { |
| 53 |
if (hours < 0 || minutes < 0 || seconds < 0 || hours > 23 || minutes > 59 || |
| 54 |
seconds > 59) { |
| 55 |
return 0; |
| 56 |
} |
| 57 |
return 1; |
| 58 |
} |
| 59 |
|
| 60 |
// 0000-01-01 00:00:00 UTC |
| 61 |
#define MIN_POSIX_TIME INT64_C(-62167219200) |
| 62 |
// 9999-12-31 23:59:59 UTC |
| 63 |
#define MAX_POSIX_TIME INT64_C(253402300799) |
| 64 |
|
| 65 |
// Is an int64 time within our expected range? |
| 66 |
static int is_valid_posix_time(int64_t time) { |
| 67 |
return MIN_POSIX_TIME <= time && time <= MAX_POSIX_TIME; |
| 68 |
} |
| 69 |
|
| 70 |
// Inspired by algorithms presented in |
| 71 |
// https://howardhinnant.github.io/date_algorithms.html |
| 72 |
// (Public Domain) |
| 73 |
static int posix_time_from_utc(int64_t year, int64_t month, int64_t day, |
| 74 |
int64_t hours, int64_t minutes, int64_t seconds, |
| 75 |
int64_t *out_time) { |
| 76 |
if (!is_valid_date(year, month, day) || |
| 77 |
!is_valid_time(hours, minutes, seconds)) { |
| 78 |
return 0; |
| 79 |
} |
| 80 |
if (month <= 2) { |
| 81 |
year--; // Start years on Mar 1, so leap days always finish a year. |
| 82 |
} |
| 83 |
// At this point year will be in the range -1 and 9999. |
| 84 |
assert(-1 <= year && year <= 9999); |
| 85 |
int64_t era = (year >= 0 ? year : year - 399) / 400; |
| 86 |
int64_t year_of_era = year - era * 400; |
| 87 |
int64_t day_of_year = |
| 88 |
(153 * (month > 2 ? month - 3 : month + 9) + 2) / 5 + day - 1; |
| 89 |
int64_t day_of_era = |
| 90 |
year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year; |
| 91 |
int64_t posix_days = era * 146097 + day_of_era - 719468; |
| 92 |
*out_time = posix_days * SECS_PER_DAY + hours * SECS_PER_HOUR + minutes * 60 + |
| 93 |
seconds; |
| 94 |
return 1; |
| 95 |
} |
| 96 |
|
| 97 |
// Inspired by algorithms presented in |
| 98 |
// https://howardhinnant.github.io/date_algorithms.html |
| 99 |
// (Public Domain) |
| 100 |
static int utc_from_posix_time(int64_t time, int *out_year, int *out_month, |
| 101 |
int *out_day, int *out_hours, int *out_minutes, |
| 102 |
int *out_seconds) { |
| 103 |
if (!is_valid_posix_time(time)) { |
| 104 |
return 0; |
| 105 |
} |
| 106 |
int64_t days = time / SECS_PER_DAY; |
| 107 |
int64_t leftover_seconds = time % SECS_PER_DAY; |
| 108 |
if (leftover_seconds < 0) { |
| 109 |
days--; |
| 110 |
leftover_seconds += SECS_PER_DAY; |
| 111 |
} |
| 112 |
days += 719468; // Shift to starting epoch of Mar 1 0000. |
| 113 |
// At this point, days will be in the range -61 and 3652364. |
| 114 |
assert(-61 <= days && days <= 3652364); |
| 115 |
int64_t era = (days > 0 ? days : days - 146096) / 146097; |
| 116 |
int64_t day_of_era = days - era * 146097; |
| 117 |
int64_t year_of_era = (day_of_era - day_of_era / 1460 + day_of_era / 36524 - |
| 118 |
day_of_era / 146096) / |
| 119 |
365; |
| 120 |
*out_year = (int)(year_of_era + era * 400); // Year starting on Mar 1. |
| 121 |
int64_t day_of_year = |
| 122 |
day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100); |
| 123 |
int64_t month_of_year = (5 * day_of_year + 2) / 153; |
| 124 |
*out_month = |
| 125 |
(int)(month_of_year < 10 ? month_of_year + 3 : month_of_year - 9); |
| 126 |
if (*out_month <= 2) { |
| 127 |
(*out_year)++; // Adjust year back to Jan 1 start of year. |
| 128 |
} |
| 129 |
*out_day = (int)(day_of_year - (153 * month_of_year + 2) / 5 + 1); |
| 130 |
*out_hours = (int)(leftover_seconds / SECS_PER_HOUR); |
| 131 |
leftover_seconds %= SECS_PER_HOUR; |
| 132 |
*out_minutes = (int)(leftover_seconds / 60); |
| 133 |
*out_seconds = (int)(leftover_seconds % 60); |
| 134 |
return 1; |
| 135 |
} |
| 136 |
|
| 137 |
int OPENSSL_tm_to_posix(const struct tm *tm, int64_t *out) { |
| 138 |
return posix_time_from_utc(tm->tm_year + INT64_C(1900), |
| 139 |
tm->tm_mon + INT64_C(1), tm->tm_mday, tm->tm_hour, |
| 140 |
tm->tm_min, tm->tm_sec, out); |
| 141 |
} |
| 142 |
|
| 143 |
int OPENSSL_posix_to_tm(int64_t time, struct tm *out_tm) { |
| 144 |
if (out_tm == NULL) { |
| 145 |
OPENSSL_PUT_ERROR(ASN1, ERR_R_PASSED_NULL_PARAMETER); |
| 146 |
return 0; |
| 147 |
} |
| 148 |
struct tm tmp_tm; |
| 149 |
OPENSSL_memset(&tmp_tm, 0, sizeof(tmp_tm)); |
| 150 |
if (!utc_from_posix_time(time, &tmp_tm.tm_year, &tmp_tm.tm_mon, |
| 151 |
&tmp_tm.tm_mday, &tmp_tm.tm_hour, &tmp_tm.tm_min, |
| 152 |
&tmp_tm.tm_sec)) { |
| 153 |
return 0; |
| 154 |
} |
| 155 |
tmp_tm.tm_year -= 1900; |
| 156 |
tmp_tm.tm_mon -= 1; |
| 157 |
*out_tm = tmp_tm; |
| 158 |
|
| 159 |
return 1; |
| 160 |
} |
| 161 |
|
| 162 |
int OPENSSL_timegm(const struct tm *tm, time_t *out) { |
| 163 |
OPENSSL_STATIC_ASSERT( |
| 164 |
sizeof(time_t) == sizeof(int32_t) || sizeof(time_t) == sizeof(int64_t), |
| 165 |
time_t_is_broken); |
| 166 |
int64_t posix_time; |
| 167 |
if (!OPENSSL_tm_to_posix(tm, &posix_time)) { |
| 168 |
return 0; |
| 169 |
} |
| 170 |
if (sizeof(time_t) == sizeof(int32_t) && |
| 171 |
(posix_time > INT32_MAX || posix_time < INT32_MIN)) { |
| 172 |
return 0; |
| 173 |
} |
| 174 |
*out = (time_t)posix_time; |
| 175 |
return 1; |
| 176 |
} |
| 177 |
|
| 178 |
struct tm *OPENSSL_gmtime(const time_t *time, struct tm *out_tm) { |
| 179 |
OPENSSL_STATIC_ASSERT( |
| 180 |
sizeof(time_t) == sizeof(int32_t) || sizeof(time_t) == sizeof(int64_t), |
| 181 |
time_t_is_broken); |
| 182 |
int64_t posix_time = *time; |
| 183 |
if (!OPENSSL_posix_to_tm(posix_time, out_tm)) { |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
return out_tm; |
| 187 |
} |
| 188 |
|
| 189 |
int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, int64_t offset_sec) { |
| 190 |
int64_t posix_time; |
| 191 |
if (!OPENSSL_tm_to_posix(tm, &posix_time)) { |
| 192 |
return 0; |
| 193 |
} |
| 194 |
OPENSSL_STATIC_ASSERT(INT_MAX <= INT64_MAX / SECS_PER_DAY, |
| 195 |
day_offset_in_seconds_cannot_overflow) |
| 196 |
OPENSSL_STATIC_ASSERT(MAX_POSIX_TIME <= INT64_MAX - INT_MAX * SECS_PER_DAY, |
| 197 |
addition_cannot_overflow) |
| 198 |
OPENSSL_STATIC_ASSERT(MIN_POSIX_TIME >= INT64_MIN - INT_MIN * SECS_PER_DAY, |
| 199 |
addition_cannot_underflow) |
| 200 |
posix_time += offset_day * SECS_PER_DAY; |
| 201 |
if (posix_time > 0 && offset_sec > INT64_MAX - posix_time) { |
| 202 |
return 0; |
| 203 |
} |
| 204 |
if (posix_time < 0 && offset_sec < INT64_MIN - posix_time) { |
| 205 |
return 0; |
| 206 |
} |
| 207 |
posix_time += offset_sec; |
| 208 |
|
| 209 |
if (!OPENSSL_posix_to_tm(posix_time, tm)) { |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
return 1; |
| 214 |
} |
| 215 |
|
| 216 |
int OPENSSL_gmtime_diff(int *out_days, int *out_secs, const struct tm *from, |
| 217 |
const struct tm *to) { |
| 218 |
int64_t time_to, time_from; |
| 219 |
if (!OPENSSL_tm_to_posix(to, &time_to) || |
| 220 |
!OPENSSL_tm_to_posix(from, &time_from)) { |
| 221 |
return 0; |
| 222 |
} |
| 223 |
// Times are in range, so these calculations can not overflow. |
| 224 |
OPENSSL_STATIC_ASSERT(SECS_PER_DAY <= INT_MAX, seconds_per_day_does_not_fit_in_int) |
| 225 |
OPENSSL_STATIC_ASSERT((MAX_POSIX_TIME - MIN_POSIX_TIME) / SECS_PER_DAY <= INT_MAX, |
| 226 |
range_of_valid_POSIX_times_in_days_does_not_fit_in_int) |
| 227 |
int64_t timediff = time_to - time_from; |
| 228 |
int64_t daydiff = timediff / SECS_PER_DAY; |
| 229 |
timediff %= SECS_PER_DAY; |
| 230 |
*out_secs = (int)timediff; |
| 231 |
*out_days = (int)daydiff; |
| 232 |
return 1; |
| 233 |
} |