diff options
Diffstat (limited to 'src/time64.c')
| -rw-r--r-- | src/time64.c | 836 |
1 files changed, 836 insertions, 0 deletions
diff --git a/src/time64.c b/src/time64.c new file mode 100644 index 0000000..8f82e39 --- /dev/null +++ b/src/time64.c | |||
| @@ -0,0 +1,836 @@ | |||
| 1 | /* | ||
| 2 | |||
| 3 | Copyright (c) 2007-2010 Michael G Schwern | ||
| 4 | |||
| 5 | This software originally derived from Paul Sheer's pivotal_gmtime_r.c. | ||
| 6 | |||
| 7 | The MIT License: | ||
| 8 | |||
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 10 | of this software and associated documentation files (the "Software"), to deal | ||
| 11 | in the Software without restriction, including without limitation the rights | ||
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 13 | copies of the Software, and to permit persons to whom the Software is | ||
| 14 | furnished to do so, subject to the following conditions: | ||
| 15 | |||
| 16 | The above copyright notice and this permission notice shall be included in | ||
| 17 | all copies or substantial portions of the Software. | ||
| 18 | |||
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 25 | THE SOFTWARE. | ||
| 26 | |||
| 27 | */ | ||
| 28 | |||
| 29 | /* | ||
| 30 | |||
| 31 | Programmers who have available to them 64-bit time values as a 'long | ||
| 32 | long' type can use localtime64_r() and gmtime64_r() which correctly | ||
| 33 | converts the time even on 32-bit systems. Whether you have 64-bit time | ||
| 34 | values will depend on the operating system. | ||
| 35 | |||
| 36 | localtime64_r() is a 64-bit equivalent of localtime_r(). | ||
| 37 | |||
| 38 | gmtime64_r() is a 64-bit equivalent of gmtime_r(). | ||
| 39 | |||
| 40 | */ | ||
| 41 | |||
| 42 | #include <assert.h> | ||
| 43 | #include <stdlib.h> | ||
| 44 | #include <stdio.h> | ||
| 45 | #include <string.h> | ||
| 46 | #include <time.h> | ||
| 47 | #include <errno.h> | ||
| 48 | #include "time64.h" | ||
| 49 | #include "time64_limits.h" | ||
| 50 | |||
| 51 | |||
| 52 | /* Spec says except for stftime() and the _r() functions, these | ||
| 53 | all return static memory. Stabbings! */ | ||
| 54 | static struct TM Static_Return_Date; | ||
| 55 | static char Static_Return_String[35]; | ||
| 56 | |||
| 57 | static const char days_in_month[2][12] = { | ||
| 58 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, | ||
| 59 | {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, | ||
| 60 | }; | ||
| 61 | |||
| 62 | static const short julian_days_by_month[2][12] = { | ||
| 63 | {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, | ||
| 64 | {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}, | ||
| 65 | }; | ||
| 66 | |||
| 67 | static char wday_name[7][4] = { | ||
| 68 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" | ||
| 69 | }; | ||
| 70 | |||
| 71 | static char mon_name[12][4] = { | ||
| 72 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | ||
| 73 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | ||
| 74 | }; | ||
| 75 | |||
| 76 | static const short length_of_year[2] = { 365, 366 }; | ||
| 77 | |||
| 78 | /* Some numbers relating to the gregorian cycle */ | ||
| 79 | static const Year years_in_gregorian_cycle = 400; | ||
| 80 | #define days_in_gregorian_cycle ((365 * 400) + 100 - 4 + 1) | ||
| 81 | static const Time64_T seconds_in_gregorian_cycle = days_in_gregorian_cycle * 60LL * 60LL * 24LL; | ||
| 82 | |||
| 83 | /* Year range we can trust the time funcitons with */ | ||
| 84 | #define MAX_SAFE_YEAR 2037 | ||
| 85 | #define MIN_SAFE_YEAR 1971 | ||
| 86 | |||
| 87 | /* 28 year Julian calendar cycle */ | ||
| 88 | #define SOLAR_CYCLE_LENGTH 28 | ||
| 89 | |||
| 90 | /* Year cycle from MAX_SAFE_YEAR down. */ | ||
| 91 | static const short safe_years_high[SOLAR_CYCLE_LENGTH] = { | ||
| 92 | 2016, 2017, 2018, 2019, | ||
| 93 | 2020, 2021, 2022, 2023, | ||
| 94 | 2024, 2025, 2026, 2027, | ||
| 95 | 2028, 2029, 2030, 2031, | ||
| 96 | 2032, 2033, 2034, 2035, | ||
| 97 | 2036, 2037, 2010, 2011, | ||
| 98 | 2012, 2013, 2014, 2015 | ||
| 99 | }; | ||
| 100 | |||
| 101 | /* Year cycle from MIN_SAFE_YEAR up */ | ||
| 102 | static const int safe_years_low[SOLAR_CYCLE_LENGTH] = { | ||
| 103 | 1996, 1997, 1998, 1971, | ||
| 104 | 1972, 1973, 1974, 1975, | ||
| 105 | 1976, 1977, 1978, 1979, | ||
| 106 | 1980, 1981, 1982, 1983, | ||
| 107 | 1984, 1985, 1986, 1987, | ||
| 108 | 1988, 1989, 1990, 1991, | ||
| 109 | 1992, 1993, 1994, 1995, | ||
| 110 | }; | ||
| 111 | |||
| 112 | /* This isn't used, but it's handy to look at */ | ||
| 113 | #if 0 | ||
| 114 | static const char dow_year_start[SOLAR_CYCLE_LENGTH] = { | ||
| 115 | 5, 0, 1, 2, /* 0 2016 - 2019 */ | ||
| 116 | 3, 5, 6, 0, /* 4 */ | ||
| 117 | 1, 3, 4, 5, /* 8 1996 - 1998, 1971*/ | ||
| 118 | 6, 1, 2, 3, /* 12 1972 - 1975 */ | ||
| 119 | 4, 6, 0, 1, /* 16 */ | ||
| 120 | 2, 4, 5, 6, /* 20 2036, 2037, 2010, 2011 */ | ||
| 121 | 0, 2, 3, 4 /* 24 2012, 2013, 2014, 2015 */ | ||
| 122 | }; | ||
| 123 | #endif | ||
| 124 | |||
| 125 | /* Let's assume people are going to be looking for dates in the future. | ||
| 126 | Let's provide some cheats so you can skip ahead. | ||
| 127 | This has a 4x speed boost when near 2008. | ||
| 128 | */ | ||
| 129 | /* Number of days since epoch on Jan 1st, 2008 GMT */ | ||
| 130 | #define CHEAT_DAYS (1199145600 / 24 / 60 / 60) | ||
| 131 | #define CHEAT_YEARS 108 | ||
| 132 | |||
| 133 | #define IS_LEAP(n) ((!(((n) + 1900) % 400) || (!(((n) + 1900) % 4) && (((n) + 1900) % 100))) != 0) | ||
| 134 | #define WRAP(a,b,m) ((a) = ((a) < 0 ) ? ((b)--, (a) + (m)) : (a)) | ||
| 135 | |||
| 136 | #ifdef USE_SYSTEM_LOCALTIME | ||
| 137 | # define SHOULD_USE_SYSTEM_LOCALTIME(a) ( \ | ||
| 138 | (a) <= SYSTEM_LOCALTIME_MAX && \ | ||
| 139 | (a) >= SYSTEM_LOCALTIME_MIN \ | ||
| 140 | ) | ||
| 141 | #else | ||
| 142 | # define SHOULD_USE_SYSTEM_LOCALTIME(a) (0) | ||
| 143 | #endif | ||
| 144 | |||
| 145 | #ifdef USE_SYSTEM_GMTIME | ||
| 146 | # define SHOULD_USE_SYSTEM_GMTIME(a) ( \ | ||
| 147 | (a) <= SYSTEM_GMTIME_MAX && \ | ||
| 148 | (a) >= SYSTEM_GMTIME_MIN \ | ||
| 149 | ) | ||
| 150 | #else | ||
| 151 | # define SHOULD_USE_SYSTEM_GMTIME(a) (0) | ||
| 152 | #endif | ||
| 153 | |||
| 154 | /* Multi varadic macros are a C99 thing, alas */ | ||
| 155 | #ifdef TIME_64_DEBUG | ||
| 156 | # define TIME64_TRACE(format) (fprintf(stderr, format)) | ||
| 157 | # define TIME64_TRACE1(format, var1) (fprintf(stderr, format, var1)) | ||
| 158 | # define TIME64_TRACE2(format, var1, var2) (fprintf(stderr, format, var1, var2)) | ||
| 159 | # define TIME64_TRACE3(format, var1, var2, var3) (fprintf(stderr, format, var1, var2, var3)) | ||
| 160 | #else | ||
| 161 | # define TIME64_TRACE(format) ((void)0) | ||
| 162 | # define TIME64_TRACE1(format, var1) ((void)0) | ||
| 163 | # define TIME64_TRACE2(format, var1, var2) ((void)0) | ||
| 164 | # define TIME64_TRACE3(format, var1, var2, var3) ((void)0) | ||
| 165 | #endif | ||
| 166 | |||
| 167 | |||
| 168 | static int is_exception_century(Year year) | ||
| 169 | { | ||
| 170 | int is_exception = ((year % 100 == 0) && !(year % 400 == 0)); | ||
| 171 | TIME64_TRACE1("# is_exception_century: %s\n", is_exception ? "yes" : "no"); | ||
| 172 | |||
| 173 | return(is_exception); | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | /* Compare two dates. | ||
| 178 | The result is like cmp. | ||
| 179 | Ignores things like gmtoffset and dst | ||
| 180 | */ | ||
| 181 | static int cmp_date( const struct TM* left, const struct tm* right ) { | ||
| 182 | if( left->tm_year > right->tm_year ) | ||
| 183 | return 1; | ||
| 184 | else if( left->tm_year < right->tm_year ) | ||
| 185 | return -1; | ||
| 186 | |||
| 187 | if( left->tm_mon > right->tm_mon ) | ||
| 188 | return 1; | ||
| 189 | else if( left->tm_mon < right->tm_mon ) | ||
| 190 | return -1; | ||
| 191 | |||
| 192 | if( left->tm_mday > right->tm_mday ) | ||
| 193 | return 1; | ||
| 194 | else if( left->tm_mday < right->tm_mday ) | ||
| 195 | return -1; | ||
| 196 | |||
| 197 | if( left->tm_hour > right->tm_hour ) | ||
| 198 | return 1; | ||
| 199 | else if( left->tm_hour < right->tm_hour ) | ||
| 200 | return -1; | ||
| 201 | |||
| 202 | if( left->tm_min > right->tm_min ) | ||
| 203 | return 1; | ||
| 204 | else if( left->tm_min < right->tm_min ) | ||
| 205 | return -1; | ||
| 206 | |||
| 207 | if( left->tm_sec > right->tm_sec ) | ||
| 208 | return 1; | ||
| 209 | else if( left->tm_sec < right->tm_sec ) | ||
| 210 | return -1; | ||
| 211 | |||
| 212 | return 0; | ||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | /* Check if a date is safely inside a range. | ||
| 217 | The intention is to check if its a few days inside. | ||
| 218 | */ | ||
| 219 | static int date_in_safe_range( const struct TM* date, const struct tm* min, const struct tm* max ) { | ||
| 220 | if( cmp_date(date, min) == -1 ) | ||
| 221 | return 0; | ||
| 222 | |||
| 223 | if( cmp_date(date, max) == 1 ) | ||
| 224 | return 0; | ||
| 225 | |||
| 226 | return 1; | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | /* timegm() is not in the C or POSIX spec, but it is such a useful | ||
| 231 | extension I would be remiss in leaving it out. Also I need it | ||
| 232 | for localtime64() | ||
| 233 | */ | ||
| 234 | Time64_T timegm64(const struct TM *date) { | ||
| 235 | Time64_T days = 0; | ||
| 236 | Time64_T seconds = 0; | ||
| 237 | Year year; | ||
| 238 | Year orig_year = (Year)date->tm_year; | ||
| 239 | int cycles = 0; | ||
| 240 | |||
| 241 | if( orig_year > 100 ) { | ||
| 242 | cycles = (orig_year - 100) / 400; | ||
| 243 | orig_year -= cycles * 400; | ||
| 244 | days += (Time64_T)cycles * days_in_gregorian_cycle; | ||
| 245 | } | ||
| 246 | else if( orig_year < -300 ) { | ||
| 247 | cycles = (orig_year - 100) / 400; | ||
| 248 | orig_year -= cycles * 400; | ||
| 249 | days += (Time64_T)cycles * days_in_gregorian_cycle; | ||
| 250 | } | ||
| 251 | TIME64_TRACE3("# timegm/ cycles: %d, days: %lld, orig_year: %lld\n", cycles, days, orig_year); | ||
| 252 | |||
| 253 | if( orig_year > 70 ) { | ||
| 254 | year = 70; | ||
| 255 | while( year < orig_year ) { | ||
| 256 | days += length_of_year[IS_LEAP(year)]; | ||
| 257 | year++; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | else if ( orig_year < 70 ) { | ||
| 261 | year = 69; | ||
| 262 | do { | ||
| 263 | days -= length_of_year[IS_LEAP(year)]; | ||
| 264 | year--; | ||
| 265 | } while( year >= orig_year ); | ||
| 266 | } | ||
| 267 | |||
| 268 | days += julian_days_by_month[IS_LEAP(orig_year)][date->tm_mon]; | ||
| 269 | days += date->tm_mday - 1; | ||
| 270 | |||
| 271 | seconds = days * 60 * 60 * 24; | ||
| 272 | |||
| 273 | seconds += date->tm_hour * 60 * 60; | ||
| 274 | seconds += date->tm_min * 60; | ||
| 275 | seconds += date->tm_sec; | ||
| 276 | |||
| 277 | return(seconds); | ||
| 278 | } | ||
| 279 | |||
| 280 | |||
| 281 | static int check_tm(struct TM *tm) | ||
| 282 | { | ||
| 283 | /* Don't forget leap seconds */ | ||
| 284 | assert(tm->tm_sec >= 0); | ||
| 285 | assert(tm->tm_sec <= 61); | ||
| 286 | |||
| 287 | assert(tm->tm_min >= 0); | ||
| 288 | assert(tm->tm_min <= 59); | ||
| 289 | |||
| 290 | assert(tm->tm_hour >= 0); | ||
| 291 | assert(tm->tm_hour <= 23); | ||
| 292 | |||
| 293 | assert(tm->tm_mday >= 1); | ||
| 294 | assert(tm->tm_mday <= days_in_month[IS_LEAP(tm->tm_year)][tm->tm_mon]); | ||
| 295 | |||
| 296 | assert(tm->tm_mon >= 0); | ||
| 297 | assert(tm->tm_mon <= 11); | ||
| 298 | |||
| 299 | assert(tm->tm_wday >= 0); | ||
| 300 | assert(tm->tm_wday <= 6); | ||
| 301 | |||
| 302 | assert(tm->tm_yday >= 0); | ||
| 303 | assert(tm->tm_yday <= length_of_year[IS_LEAP(tm->tm_year)]); | ||
| 304 | |||
| 305 | #ifdef HAVE_TM_TM_GMTOFF | ||
| 306 | assert(tm->tm_gmtoff >= -24 * 60 * 60); | ||
| 307 | assert(tm->tm_gmtoff <= 24 * 60 * 60); | ||
| 308 | #endif | ||
| 309 | |||
| 310 | return 1; | ||
| 311 | } | ||
| 312 | |||
| 313 | |||
| 314 | /* The exceptional centuries without leap years cause the cycle to | ||
| 315 | shift by 16 | ||
| 316 | */ | ||
| 317 | static Year cycle_offset(Year year) | ||
| 318 | { | ||
| 319 | const Year start_year = 2000; | ||
| 320 | Year year_diff = year - start_year; | ||
| 321 | Year exceptions; | ||
| 322 | |||
| 323 | if( year > start_year ) | ||
| 324 | year_diff--; | ||
| 325 | |||
| 326 | exceptions = year_diff / 100; | ||
| 327 | exceptions -= year_diff / 400; | ||
| 328 | |||
| 329 | TIME64_TRACE3("# year: %lld, exceptions: %lld, year_diff: %lld\n", | ||
| 330 | year, exceptions, year_diff); | ||
| 331 | |||
| 332 | return exceptions * 16; | ||
| 333 | } | ||
| 334 | |||
| 335 | /* For a given year after 2038, pick the latest possible matching | ||
| 336 | year in the 28 year calendar cycle. | ||
| 337 | |||
| 338 | A matching year... | ||
| 339 | 1) Starts on the same day of the week. | ||
| 340 | 2) Has the same leap year status. | ||
| 341 | |||
| 342 | This is so the calendars match up. | ||
| 343 | |||
| 344 | Also the previous year must match. When doing Jan 1st you might | ||
| 345 | wind up on Dec 31st the previous year when doing a -UTC time zone. | ||
| 346 | |||
| 347 | Finally, the next year must have the same start day of week. This | ||
| 348 | is for Dec 31st with a +UTC time zone. | ||
| 349 | It doesn't need the same leap year status since we only care about | ||
| 350 | January 1st. | ||
| 351 | */ | ||
| 352 | static int safe_year(const Year year) | ||
| 353 | { | ||
| 354 | int safe_year; | ||
| 355 | Year year_cycle; | ||
| 356 | |||
| 357 | if( year >= MIN_SAFE_YEAR && year <= MAX_SAFE_YEAR ) { | ||
| 358 | return (int)year; | ||
| 359 | } | ||
| 360 | |||
| 361 | year_cycle = year + cycle_offset(year); | ||
| 362 | |||
| 363 | /* safe_years_low is off from safe_years_high by 8 years */ | ||
| 364 | if( year < MIN_SAFE_YEAR ) | ||
| 365 | year_cycle -= 8; | ||
| 366 | |||
| 367 | /* Change non-leap xx00 years to an equivalent */ | ||
| 368 | if( is_exception_century(year) ) | ||
| 369 | year_cycle += 11; | ||
| 370 | |||
| 371 | /* Also xx01 years, since the previous year will be wrong */ | ||
| 372 | if( is_exception_century(year - 1) ) | ||
| 373 | year_cycle += 17; | ||
| 374 | |||
| 375 | year_cycle %= SOLAR_CYCLE_LENGTH; | ||
| 376 | if( year_cycle < 0 ) | ||
| 377 | year_cycle = SOLAR_CYCLE_LENGTH + year_cycle; | ||
| 378 | |||
| 379 | assert( year_cycle >= 0 ); | ||
| 380 | assert( year_cycle < SOLAR_CYCLE_LENGTH ); | ||
| 381 | if( year < MIN_SAFE_YEAR ) | ||
| 382 | safe_year = safe_years_low[year_cycle]; | ||
| 383 | else if( year > MAX_SAFE_YEAR ) | ||
| 384 | safe_year = safe_years_high[year_cycle]; | ||
| 385 | else | ||
| 386 | assert(0); | ||
| 387 | |||
| 388 | TIME64_TRACE3("# year: %lld, year_cycle: %lld, safe_year: %d\n", | ||
| 389 | year, year_cycle, safe_year); | ||
| 390 | |||
| 391 | assert(safe_year <= MAX_SAFE_YEAR && safe_year >= MIN_SAFE_YEAR); | ||
| 392 | |||
| 393 | return safe_year; | ||
| 394 | } | ||
| 395 | |||
| 396 | |||
| 397 | void copy_tm_to_TM64(const struct tm *src, struct TM *dest) { | ||
| 398 | if( src == NULL ) { | ||
| 399 | memset(dest, 0, sizeof(*dest)); | ||
| 400 | } | ||
| 401 | else { | ||
| 402 | # ifdef USE_TM64 | ||
| 403 | dest->tm_sec = src->tm_sec; | ||
| 404 | dest->tm_min = src->tm_min; | ||
| 405 | dest->tm_hour = src->tm_hour; | ||
| 406 | dest->tm_mday = src->tm_mday; | ||
| 407 | dest->tm_mon = src->tm_mon; | ||
| 408 | dest->tm_year = (Year)src->tm_year; | ||
| 409 | dest->tm_wday = src->tm_wday; | ||
| 410 | dest->tm_yday = src->tm_yday; | ||
| 411 | dest->tm_isdst = src->tm_isdst; | ||
| 412 | |||
| 413 | # ifdef HAVE_TM_TM_GMTOFF | ||
| 414 | dest->tm_gmtoff = src->tm_gmtoff; | ||
| 415 | # endif | ||
| 416 | |||
| 417 | # ifdef HAVE_TM_TM_ZONE | ||
| 418 | dest->tm_zone = src->tm_zone; | ||
| 419 | # endif | ||
| 420 | |||
| 421 | # else | ||
| 422 | /* They're the same type */ | ||
| 423 | memcpy(dest, src, sizeof(*dest)); | ||
| 424 | # endif | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | |||
| 429 | void copy_TM64_to_tm(const struct TM *src, struct tm *dest) { | ||
| 430 | if( src == NULL ) { | ||
| 431 | memset(dest, 0, sizeof(*dest)); | ||
| 432 | } | ||
| 433 | else { | ||
| 434 | # ifdef USE_TM64 | ||
| 435 | dest->tm_sec = src->tm_sec; | ||
| 436 | dest->tm_min = src->tm_min; | ||
| 437 | dest->tm_hour = src->tm_hour; | ||
| 438 | dest->tm_mday = src->tm_mday; | ||
| 439 | dest->tm_mon = src->tm_mon; | ||
| 440 | dest->tm_year = (int)src->tm_year; | ||
| 441 | dest->tm_wday = src->tm_wday; | ||
| 442 | dest->tm_yday = src->tm_yday; | ||
| 443 | dest->tm_isdst = src->tm_isdst; | ||
| 444 | |||
| 445 | # ifdef HAVE_TM_TM_GMTOFF | ||
| 446 | dest->tm_gmtoff = src->tm_gmtoff; | ||
| 447 | # endif | ||
| 448 | |||
| 449 | # ifdef HAVE_TM_TM_ZONE | ||
| 450 | dest->tm_zone = src->tm_zone; | ||
| 451 | # endif | ||
| 452 | |||
| 453 | # else | ||
| 454 | /* They're the same type */ | ||
| 455 | memcpy(dest, src, sizeof(*dest)); | ||
| 456 | # endif | ||
| 457 | } | ||
| 458 | } | ||
| 459 | |||
| 460 | |||
| 461 | #ifndef HAVE_LOCALTIME_R | ||
| 462 | /* Simulate localtime_r() to the best of our ability */ | ||
| 463 | static struct tm * fake_localtime_r(const time_t *time, struct tm *result) { | ||
| 464 | const struct tm *static_result = localtime(time); | ||
| 465 | |||
| 466 | assert(result != NULL); | ||
| 467 | |||
| 468 | if( static_result == NULL ) { | ||
| 469 | memset(result, 0, sizeof(*result)); | ||
| 470 | return NULL; | ||
| 471 | } | ||
| 472 | else { | ||
| 473 | memcpy(result, static_result, sizeof(*result)); | ||
| 474 | return result; | ||
| 475 | } | ||
| 476 | } | ||
| 477 | #endif | ||
| 478 | |||
| 479 | |||
| 480 | #ifndef HAVE_GMTIME_R | ||
| 481 | /* Simulate gmtime_r() to the best of our ability */ | ||
| 482 | static struct tm * fake_gmtime_r(const time_t *time, struct tm *result) { | ||
| 483 | const struct tm *static_result = gmtime(time); | ||
| 484 | |||
| 485 | assert(result != NULL); | ||
| 486 | |||
| 487 | if( static_result == NULL ) { | ||
| 488 | memset(result, 0, sizeof(*result)); | ||
| 489 | return NULL; | ||
| 490 | } | ||
| 491 | else { | ||
| 492 | memcpy(result, static_result, sizeof(*result)); | ||
| 493 | return result; | ||
| 494 | } | ||
| 495 | } | ||
| 496 | #endif | ||
| 497 | |||
| 498 | |||
| 499 | static Time64_T seconds_between_years(Year left_year, Year right_year) { | ||
| 500 | int increment = (left_year > right_year) ? 1 : -1; | ||
| 501 | Time64_T seconds = 0; | ||
| 502 | int cycles; | ||
| 503 | |||
| 504 | if( left_year > 2400 ) { | ||
| 505 | cycles = (left_year - 2400) / 400; | ||
| 506 | left_year -= cycles * 400; | ||
| 507 | seconds += cycles * seconds_in_gregorian_cycle; | ||
| 508 | } | ||
| 509 | else if( left_year < 1600 ) { | ||
| 510 | cycles = (left_year - 1600) / 400; | ||
| 511 | left_year += cycles * 400; | ||
| 512 | seconds += cycles * seconds_in_gregorian_cycle; | ||
| 513 | } | ||
| 514 | |||
| 515 | while( left_year != right_year ) { | ||
| 516 | seconds += length_of_year[IS_LEAP(right_year - 1900)] * 60 * 60 * 24; | ||
| 517 | right_year += increment; | ||
| 518 | } | ||
| 519 | |||
| 520 | return seconds * increment; | ||
| 521 | } | ||
| 522 | |||
| 523 | |||
| 524 | Time64_T mktime64(struct TM *input_date) { | ||
| 525 | struct tm safe_date; | ||
| 526 | struct TM date; | ||
| 527 | Time64_T time; | ||
| 528 | Year year = input_date->tm_year + 1900; | ||
| 529 | |||
| 530 | if( date_in_safe_range(input_date, &SYSTEM_MKTIME_MIN, &SYSTEM_MKTIME_MAX) ) | ||
| 531 | { | ||
| 532 | copy_TM64_to_tm(input_date, &safe_date); | ||
| 533 | time = (Time64_T)mktime(&safe_date); | ||
| 534 | |||
| 535 | /* Correct the possibly out of bound input date */ | ||
| 536 | copy_tm_to_TM64(&safe_date, input_date); | ||
| 537 | return time; | ||
| 538 | } | ||
| 539 | |||
| 540 | /* Have to make the year safe in date else it won't fit in safe_date */ | ||
| 541 | date = *input_date; | ||
| 542 | date.tm_year = safe_year(year) - 1900; | ||
| 543 | copy_TM64_to_tm(&date, &safe_date); | ||
| 544 | |||
| 545 | time = (Time64_T)mktime(&safe_date); | ||
| 546 | |||
| 547 | /* Correct the user's possibly out of bound input date */ | ||
| 548 | copy_tm_to_TM64(&safe_date, input_date); | ||
| 549 | |||
| 550 | time += seconds_between_years(year, (Year)(safe_date.tm_year + 1900)); | ||
| 551 | |||
| 552 | return time; | ||
| 553 | } | ||
| 554 | |||
| 555 | |||
| 556 | /* Because I think mktime() is a crappy name */ | ||
| 557 | Time64_T timelocal64(struct TM *date) { | ||
| 558 | return mktime64(date); | ||
| 559 | } | ||
| 560 | |||
| 561 | |||
| 562 | struct TM *gmtime64_r (const Time64_T *in_time, struct TM *p) | ||
| 563 | { | ||
| 564 | int v_tm_sec, v_tm_min, v_tm_hour, v_tm_mon, v_tm_wday; | ||
| 565 | Time64_T v_tm_tday; | ||
| 566 | int leap; | ||
| 567 | Time64_T m; | ||
| 568 | Time64_T time = *in_time; | ||
| 569 | Year year = 70; | ||
| 570 | int cycles = 0; | ||
| 571 | |||
| 572 | assert(p != NULL); | ||
| 573 | |||
| 574 | /* Use the system gmtime() if time_t is small enough */ | ||
| 575 | if( SHOULD_USE_SYSTEM_GMTIME(*in_time) ) { | ||
| 576 | time_t safe_time = (time_t)*in_time; | ||
| 577 | struct tm safe_date; | ||
| 578 | GMTIME_R(&safe_time, &safe_date); | ||
| 579 | |||
| 580 | copy_tm_to_TM64(&safe_date, p); | ||
| 581 | assert(check_tm(p)); | ||
| 582 | |||
| 583 | return p; | ||
| 584 | } | ||
| 585 | |||
| 586 | #ifdef HAVE_TM_TM_GMTOFF | ||
| 587 | p->tm_gmtoff = 0; | ||
| 588 | #endif | ||
| 589 | p->tm_isdst = 0; | ||
| 590 | |||
| 591 | #ifdef HAVE_TM_TM_ZONE | ||
| 592 | p->tm_zone = (char*)"UTC"; | ||
| 593 | #endif | ||
| 594 | |||
| 595 | v_tm_sec = (int)(time % 60); | ||
| 596 | time /= 60; | ||
| 597 | v_tm_min = (int)(time % 60); | ||
| 598 | time /= 60; | ||
| 599 | v_tm_hour = (int)(time % 24); | ||
| 600 | time /= 24; | ||
| 601 | v_tm_tday = time; | ||
| 602 | |||
| 603 | WRAP (v_tm_sec, v_tm_min, 60); | ||
| 604 | WRAP (v_tm_min, v_tm_hour, 60); | ||
| 605 | WRAP (v_tm_hour, v_tm_tday, 24); | ||
| 606 | |||
| 607 | v_tm_wday = (int)((v_tm_tday + 4) % 7); | ||
| 608 | if (v_tm_wday < 0) | ||
| 609 | v_tm_wday += 7; | ||
| 610 | m = v_tm_tday; | ||
| 611 | |||
| 612 | if (m >= CHEAT_DAYS) { | ||
| 613 | year = CHEAT_YEARS; | ||
| 614 | m -= CHEAT_DAYS; | ||
| 615 | } | ||
| 616 | |||
| 617 | if (m >= 0) { | ||
| 618 | /* Gregorian cycles, this is huge optimization for distant times */ | ||
| 619 | cycles = (int)(m / (Time64_T) days_in_gregorian_cycle); | ||
| 620 | if( cycles ) { | ||
| 621 | m -= (cycles * (Time64_T) days_in_gregorian_cycle); | ||
| 622 | year += (cycles * years_in_gregorian_cycle); | ||
| 623 | } | ||
| 624 | |||
| 625 | /* Years */ | ||
| 626 | leap = IS_LEAP (year); | ||
| 627 | while (m >= (Time64_T) length_of_year[leap]) { | ||
| 628 | m -= (Time64_T) length_of_year[leap]; | ||
| 629 | year++; | ||
| 630 | leap = IS_LEAP (year); | ||
| 631 | } | ||
| 632 | |||
| 633 | /* Months */ | ||
| 634 | v_tm_mon = 0; | ||
| 635 | while (m >= (Time64_T) days_in_month[leap][v_tm_mon]) { | ||
| 636 | m -= (Time64_T) days_in_month[leap][v_tm_mon]; | ||
| 637 | v_tm_mon++; | ||
| 638 | } | ||
| 639 | } else { | ||
| 640 | year--; | ||
| 641 | |||
| 642 | /* Gregorian cycles */ | ||
| 643 | cycles = (int)((m / (Time64_T) days_in_gregorian_cycle) + 1); | ||
| 644 | if( cycles ) { | ||
| 645 | m -= (cycles * (Time64_T) days_in_gregorian_cycle); | ||
| 646 | year += (cycles * years_in_gregorian_cycle); | ||
| 647 | } | ||
| 648 | |||
| 649 | /* Years */ | ||
| 650 | leap = IS_LEAP (year); | ||
| 651 | while (m < (Time64_T) -length_of_year[leap]) { | ||
| 652 | m += (Time64_T) length_of_year[leap]; | ||
| 653 | year--; | ||
| 654 | leap = IS_LEAP (year); | ||
| 655 | } | ||
| 656 | |||
| 657 | /* Months */ | ||
| 658 | v_tm_mon = 11; | ||
| 659 | while (m < (Time64_T) -days_in_month[leap][v_tm_mon]) { | ||
| 660 | m += (Time64_T) days_in_month[leap][v_tm_mon]; | ||
| 661 | v_tm_mon--; | ||
| 662 | } | ||
| 663 | m += (Time64_T) days_in_month[leap][v_tm_mon]; | ||
| 664 | } | ||
| 665 | |||
| 666 | p->tm_year = year; | ||
| 667 | if( p->tm_year != year ) { | ||
| 668 | #ifdef EOVERFLOW | ||
| 669 | errno = EOVERFLOW; | ||
| 670 | #endif | ||
| 671 | return NULL; | ||
| 672 | } | ||
| 673 | |||
| 674 | /* At this point m is less than a year so casting to an int is safe */ | ||
| 675 | p->tm_mday = (int) m + 1; | ||
| 676 | p->tm_yday = julian_days_by_month[leap][v_tm_mon] + (int)m; | ||
| 677 | p->tm_sec = v_tm_sec; | ||
| 678 | p->tm_min = v_tm_min; | ||
| 679 | p->tm_hour = v_tm_hour; | ||
| 680 | p->tm_mon = v_tm_mon; | ||
| 681 | p->tm_wday = v_tm_wday; | ||
| 682 | |||
| 683 | assert(check_tm(p)); | ||
| 684 | |||
| 685 | return p; | ||
| 686 | } | ||
| 687 | |||
| 688 | |||
| 689 | struct TM *localtime64_r (const Time64_T *time, struct TM *local_tm) | ||
| 690 | { | ||
| 691 | time_t safe_time; | ||
| 692 | struct tm safe_date; | ||
| 693 | struct TM gm_tm; | ||
| 694 | Year orig_year; | ||
| 695 | int month_diff; | ||
| 696 | |||
| 697 | assert(local_tm != NULL); | ||
| 698 | |||
| 699 | /* Use the system localtime() if time_t is small enough */ | ||
| 700 | if( SHOULD_USE_SYSTEM_LOCALTIME(*time) ) { | ||
| 701 | safe_time = (time_t)*time; | ||
| 702 | |||
| 703 | TIME64_TRACE1("Using system localtime for %lld\n", *time); | ||
| 704 | |||
| 705 | LOCALTIME_R(&safe_time, &safe_date); | ||
| 706 | |||
| 707 | copy_tm_to_TM64(&safe_date, local_tm); | ||
| 708 | assert(check_tm(local_tm)); | ||
| 709 | |||
| 710 | return local_tm; | ||
| 711 | } | ||
| 712 | |||
| 713 | if( gmtime64_r(time, &gm_tm) == NULL ) { | ||
| 714 | TIME64_TRACE1("gmtime64_r returned null for %lld\n", *time); | ||
| 715 | return NULL; | ||
| 716 | } | ||
| 717 | |||
| 718 | orig_year = gm_tm.tm_year; | ||
| 719 | |||
| 720 | if (gm_tm.tm_year > (2037 - 1900) || | ||
| 721 | gm_tm.tm_year < (1970 - 1900) | ||
| 722 | ) | ||
| 723 | { | ||
| 724 | TIME64_TRACE1("Mapping tm_year %lld to safe_year\n", (Year)gm_tm.tm_year); | ||
| 725 | gm_tm.tm_year = safe_year((Year)(gm_tm.tm_year + 1900)) - 1900; | ||
| 726 | } | ||
| 727 | |||
| 728 | safe_time = (time_t)timegm64(&gm_tm); | ||
| 729 | if( LOCALTIME_R(&safe_time, &safe_date) == NULL ) { | ||
| 730 | TIME64_TRACE1("localtime_r(%d) returned NULL\n", (int)safe_time); | ||
| 731 | return NULL; | ||
| 732 | } | ||
| 733 | |||
| 734 | copy_tm_to_TM64(&safe_date, local_tm); | ||
| 735 | |||
| 736 | local_tm->tm_year = orig_year; | ||
| 737 | if( local_tm->tm_year != orig_year ) { | ||
| 738 | TIME64_TRACE2("tm_year overflow: tm_year %lld, orig_year %lld\n", | ||
| 739 | (Year)local_tm->tm_year, (Year)orig_year); | ||
| 740 | |||
| 741 | #ifdef EOVERFLOW | ||
| 742 | errno = EOVERFLOW; | ||
| 743 | #endif | ||
| 744 | return NULL; | ||
| 745 | } | ||
| 746 | |||
| 747 | |||
| 748 | month_diff = local_tm->tm_mon - gm_tm.tm_mon; | ||
| 749 | |||
| 750 | /* When localtime is Dec 31st previous year and | ||
| 751 | gmtime is Jan 1st next year. | ||
| 752 | */ | ||
| 753 | if( month_diff == 11 ) { | ||
| 754 | local_tm->tm_year--; | ||
| 755 | } | ||
| 756 | |||
| 757 | /* When localtime is Jan 1st, next year and | ||
| 758 | gmtime is Dec 31st, previous year. | ||
| 759 | */ | ||
| 760 | if( month_diff == -11 ) { | ||
| 761 | local_tm->tm_year++; | ||
| 762 | } | ||
| 763 | |||
| 764 | /* GMT is Jan 1st, xx01 year, but localtime is still Dec 31st | ||
| 765 | in a non-leap xx00. There is one point in the cycle | ||
| 766 | we can't account for which the safe xx00 year is a leap | ||
| 767 | year. So we need to correct for Dec 31st comming out as | ||
| 768 | the 366th day of the year. | ||
| 769 | */ | ||
| 770 | if( !IS_LEAP(local_tm->tm_year) && local_tm->tm_yday == 365 ) | ||
| 771 | local_tm->tm_yday--; | ||
| 772 | |||
| 773 | assert(check_tm(local_tm)); | ||
| 774 | |||
| 775 | return local_tm; | ||
| 776 | } | ||
| 777 | |||
| 778 | |||
| 779 | static int valid_tm_wday( const struct TM* date ) { | ||
| 780 | if( 0 <= date->tm_wday && date->tm_wday <= 6 ) | ||
| 781 | return 1; | ||
| 782 | else | ||
| 783 | return 0; | ||
| 784 | } | ||
| 785 | |||
| 786 | static int valid_tm_mon( const struct TM* date ) { | ||
| 787 | if( 0 <= date->tm_mon && date->tm_mon <= 11 ) | ||
| 788 | return 1; | ||
| 789 | else | ||
| 790 | return 0; | ||
| 791 | } | ||
| 792 | |||
| 793 | |||
| 794 | char *asctime64_r( const struct TM* date, char *result ) { | ||
| 795 | /* I figure everything else can be displayed, even hour 25, but if | ||
| 796 | these are out of range we walk off the name arrays */ | ||
| 797 | if( !valid_tm_wday(date) || !valid_tm_mon(date) ) | ||
| 798 | return NULL; | ||
| 799 | |||
| 800 | sprintf(result, TM64_ASCTIME_FORMAT, | ||
| 801 | wday_name[date->tm_wday], | ||
| 802 | mon_name[date->tm_mon], | ||
| 803 | date->tm_mday, date->tm_hour, | ||
| 804 | date->tm_min, date->tm_sec, | ||
| 805 | 1900 + date->tm_year); | ||
| 806 | |||
| 807 | return result; | ||
| 808 | } | ||
| 809 | |||
| 810 | |||
| 811 | char *ctime64_r( const Time64_T* time, char* result ) { | ||
| 812 | struct TM date; | ||
| 813 | |||
| 814 | localtime64_r( time, &date ); | ||
| 815 | return asctime64_r( &date, result ); | ||
| 816 | } | ||
| 817 | |||
| 818 | |||
| 819 | /* Non-thread safe versions of the above */ | ||
| 820 | struct TM *localtime64(const Time64_T *time) { | ||
| 821 | tzset(); | ||
| 822 | return localtime64_r(time, &Static_Return_Date); | ||
| 823 | } | ||
| 824 | |||
| 825 | struct TM *gmtime64(const Time64_T *time) { | ||
| 826 | return gmtime64_r(time, &Static_Return_Date); | ||
| 827 | } | ||
| 828 | |||
| 829 | char *asctime64( const struct TM* date ) { | ||
| 830 | return asctime64_r( date, Static_Return_String ); | ||
| 831 | } | ||
| 832 | |||
| 833 | char *ctime64( const Time64_T* time ) { | ||
| 834 | tzset(); | ||
| 835 | return asctime64(localtime64(time)); | ||
| 836 | } | ||
