summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--configure.ac28
-rw-r--r--src/Makefile.am1
-rw-r--r--src/time64.c836
-rw-r--r--src/time64.h85
-rw-r--r--src/time64_limits.h95
-rw-r--r--src/xplist.c28
6 files changed, 1061 insertions, 12 deletions
diff --git a/configure.ac b/configure.ac
index 8394997..5f99d95 100644
--- a/configure.ac
+++ b/configure.ac
@@ -56,7 +56,7 @@ AC_TYPE_UINT32_T
56AC_TYPE_UINT8_T 56AC_TYPE_UINT8_T
57 57
58# Checks for library functions. 58# Checks for library functions.
59AC_CHECK_FUNCS([asprintf strcasecmp strdup strerror strndup stpcpy vasprintf]) 59AC_CHECK_FUNCS([asprintf strcasecmp strdup strerror strndup stpcpy vasprintf gmtime_r localtime_r timegm])
60 60
61# Checking endianness 61# Checking endianness
62AC_C_BIGENDIAN([AC_DEFINE([__BIG_ENDIAN__], [1], [big endian])], 62AC_C_BIGENDIAN([AC_DEFINE([__BIG_ENDIAN__], [1], [big endian])],
@@ -78,6 +78,32 @@ case ${host_os} in
78esac 78esac
79AM_CONDITIONAL(WIN32, test x$win32 = xtrue) 79AM_CONDITIONAL(WIN32, test x$win32 = xtrue)
80 80
81# Check if struct tm has a tm_gmtoff member
82AC_CACHE_CHECK(for tm_gmtoff in struct tm, ac_cv_struct_tm_gmtoff,
83 AC_TRY_COMPILE([
84 #include <time.h>
85 ], [
86 struct tm tm;
87 tm.tm_gmtoff = 1;
88 ], ac_cv_struct_tm_gmtoff=yes, ac_cv_struct_tm_gmtoff=no))
89
90if (test "$ac_cv_struct_tm_gmtoff" = "yes"); then
91 AC_DEFINE(HAVE_TM_TM_GMTOFF, 1, [Define if struct tm has a tm_gmtoff member])
92fi
93
94# Check if struct tm has a tm_zone member
95AC_CACHE_CHECK(for tm_zone in struct tm, ac_cv_struct_tm_zone,
96 AC_TRY_COMPILE([
97 #include <time.h>
98 ], [
99 struct tm tm;
100 tm.tm_zone = 1;
101 ], ac_cv_struct_tm_zone=yes, ac_cv_struct_tm_zone=no))
102
103if (test "$ac_cv_struct_tm_zone" = "yes"); then
104 AC_DEFINE(HAVE_TM_TM_ZONE, 1, [Define if struct tm has a tm_zone member])
105fi
106
81# Cython Python Bindings 107# Cython Python Bindings
82AC_ARG_WITH([cython], 108AC_ARG_WITH([cython],
83 [AS_HELP_STRING([--without-cython], 109 [AS_HELP_STRING([--without-cython],
diff --git a/src/Makefile.am b/src/Makefile.am
index 83b975c..b9117f3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -10,6 +10,7 @@ libplist_la_SOURCES = base64.c base64.h \
10 bytearray.c bytearray.h \ 10 bytearray.c bytearray.h \
11 hashtable.c hashtable.h \ 11 hashtable.c hashtable.h \
12 ptrarray.c ptrarray.h \ 12 ptrarray.c ptrarray.h \
13 time64.c time64.h time64_limits.h \
13 xplist.c \ 14 xplist.c \
14 bplist.c \ 15 bplist.c \
15 plist.c plist.h 16 plist.c plist.h
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
3Copyright (c) 2007-2010 Michael G Schwern
4
5This software originally derived from Paul Sheer's pivotal_gmtime_r.c.
6
7The MIT License:
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE SOFTWARE.
26
27*/
28
29/*
30
31Programmers who have available to them 64-bit time values as a 'long
32long' type can use localtime64_r() and gmtime64_r() which correctly
33converts the time even on 32-bit systems. Whether you have 64-bit time
34values will depend on the operating system.
35
36localtime64_r() is a 64-bit equivalent of localtime_r().
37
38gmtime64_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! */
54static struct TM Static_Return_Date;
55static char Static_Return_String[35];
56
57static 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
62static 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
67static char wday_name[7][4] = {
68 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
69};
70
71static char mon_name[12][4] = {
72 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
73 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
74};
75
76static const short length_of_year[2] = { 365, 366 };
77
78/* Some numbers relating to the gregorian cycle */
79static const Year years_in_gregorian_cycle = 400;
80#define days_in_gregorian_cycle ((365 * 400) + 100 - 4 + 1)
81static 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. */
91static 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 */
102static 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
114static 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
168static 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*/
181static 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*/
219static 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*/
234Time64_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
281static 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*/
317static 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*/
352static 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
397void 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
429void 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 */
463static 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 */
482static 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
499static 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
524Time64_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 */
557Time64_T timelocal64(struct TM *date) {
558 return mktime64(date);
559}
560
561
562struct 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
689struct 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
779static 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
786static 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
794char *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
811char *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 */
820struct TM *localtime64(const Time64_T *time) {
821 tzset();
822 return localtime64_r(time, &Static_Return_Date);
823}
824
825struct TM *gmtime64(const Time64_T *time) {
826 return gmtime64_r(time, &Static_Return_Date);
827}
828
829char *asctime64( const struct TM* date ) {
830 return asctime64_r( date, Static_Return_String );
831}
832
833char *ctime64( const Time64_T* time ) {
834 tzset();
835 return asctime64(localtime64(time));
836}
diff --git a/src/time64.h b/src/time64.h
new file mode 100644
index 0000000..556e06f
--- /dev/null
+++ b/src/time64.h
@@ -0,0 +1,85 @@
1#ifndef TIME64_H
2# define TIME64_H
3
4#include <time.h>
5#ifdef HAVE_CONFIG_H
6#include "config.h"
7#endif
8
9/* Set our custom types */
10typedef long long Int64;
11typedef Int64 Time64_T;
12typedef Int64 Year;
13
14
15/* A copy of the tm struct but with a 64 bit year */
16struct TM64 {
17 int tm_sec;
18 int tm_min;
19 int tm_hour;
20 int tm_mday;
21 int tm_mon;
22 Year tm_year;
23 int tm_wday;
24 int tm_yday;
25 int tm_isdst;
26
27#ifdef HAVE_TM_TM_GMTOFF
28 long tm_gmtoff;
29#endif
30
31#ifdef HAVE_TM_TM_ZONE
32 char *tm_zone;
33#endif
34};
35
36
37/* Decide which tm struct to use */
38#ifdef USE_TM64
39#define TM TM64
40#else
41#define TM tm
42#endif
43
44
45/* Declare public functions */
46struct TM *gmtime64_r (const Time64_T *, struct TM *);
47struct TM *localtime64_r (const Time64_T *, struct TM *);
48struct TM *gmtime64 (const Time64_T *);
49struct TM *localtime64 (const Time64_T *);
50
51char *asctime64 (const struct TM *);
52char *asctime64_r (const struct TM *, char *);
53
54char *ctime64 (const Time64_T*);
55char *ctime64_r (const Time64_T*, char*);
56
57Time64_T timegm64 (const struct TM *);
58Time64_T mktime64 (struct TM *);
59Time64_T timelocal64 (struct TM *);
60
61
62/* Not everyone has gm/localtime_r(), provide a replacement */
63#ifdef HAVE_LOCALTIME_R
64# define LOCALTIME_R(clock, result) localtime_r(clock, result)
65#else
66# define LOCALTIME_R(clock, result) fake_localtime_r(clock, result)
67#endif
68#ifdef HAVE_GMTIME_R
69# define GMTIME_R(clock, result) gmtime_r(clock, result)
70#else
71# define GMTIME_R(clock, result) fake_gmtime_r(clock, result)
72#endif
73
74
75/* Use a different asctime format depending on how big the year is */
76#ifdef USE_TM64
77 #define TM64_ASCTIME_FORMAT "%.3s %.3s%3d %.2d:%.2d:%.2d %lld\n"
78#else
79 #define TM64_ASCTIME_FORMAT "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n"
80#endif
81
82void copy_tm_to_TM64(const struct tm *src, struct TM *dest);
83void copy_TM64_to_tm(const struct TM *src, struct tm *dest);
84
85#endif
diff --git a/src/time64_limits.h b/src/time64_limits.h
new file mode 100644
index 0000000..91079af
--- /dev/null
+++ b/src/time64_limits.h
@@ -0,0 +1,95 @@
1/*
2 Maximum and minimum inputs your system's respective time functions
3 can correctly handle. time64.h will use your system functions if
4 the input falls inside these ranges and corresponding USE_SYSTEM_*
5 constant is defined.
6*/
7
8#ifndef TIME64_LIMITS_H
9#define TIME64_LIMITS_H
10
11/* Max/min for localtime() */
12#define SYSTEM_LOCALTIME_MAX 2147483647
13#define SYSTEM_LOCALTIME_MIN -2147483647-1
14
15/* Max/min for gmtime() */
16#define SYSTEM_GMTIME_MAX 2147483647
17#define SYSTEM_GMTIME_MIN -2147483647-1
18
19/* Max/min for mktime() */
20static const struct tm SYSTEM_MKTIME_MAX = {
21 7,
22 14,
23 19,
24 18,
25 0,
26 138,
27 1,
28 17,
29 0
30#ifdef HAVE_TM_TM_GMTOFF
31 ,-28800
32#endif
33#ifdef HAVE_TM_TM_ZONE
34 ,(char*)"PST"
35#endif
36};
37
38static const struct tm SYSTEM_MKTIME_MIN = {
39 52,
40 45,
41 12,
42 13,
43 11,
44 1,
45 5,
46 346,
47 0
48#ifdef HAVE_TM_TM_GMTOFF
49 ,-28800
50#endif
51#ifdef HAVE_TM_TM_ZONE
52 ,(char*)"PST"
53#endif
54};
55
56/* Max/min for timegm() */
57#ifdef HAVE_TIMEGM
58static const struct tm SYSTEM_TIMEGM_MAX = {
59 7,
60 14,
61 3,
62 19,
63 0,
64 138,
65 2,
66 18,
67 0
68 #ifdef HAVE_TM_TM_GMTOFF
69 ,0
70 #endif
71 #ifdef HAVE_TM_TM_ZONE
72 ,(char*)"UTC"
73 #endif
74};
75
76static const struct tm SYSTEM_TIMEGM_MIN = {
77 52,
78 45,
79 20,
80 13,
81 11,
82 1,
83 5,
84 346,
85 0
86 #ifdef HAVE_TM_TM_GMTOFF
87 ,0
88 #endif
89 #ifdef HAVE_TM_TM_ZONE
90 ,(char*)"UTC"
91 #endif
92};
93#endif /* HAVE_TIMEGM */
94
95#endif /* TIME64_LIMITS_H */
diff --git a/src/xplist.c b/src/xplist.c
index e55a094..fc665ac 100644
--- a/src/xplist.c
+++ b/src/xplist.c
@@ -41,6 +41,7 @@
41 41
42#include "plist.h" 42#include "plist.h"
43#include "base64.h" 43#include "base64.h"
44#include "time64.h"
44 45
45#define XPLIST_TEXT BAD_CAST("text") 46#define XPLIST_TEXT BAD_CAST("text")
46#define XPLIST_KEY BAD_CAST("key") 47#define XPLIST_KEY BAD_CAST("key")
@@ -258,12 +259,15 @@ static void node_to_xml(node_t* node, void *xml_struct)
258 case PLIST_DATE: 259 case PLIST_DATE:
259 tag = XPLIST_DATE; 260 tag = XPLIST_DATE;
260 { 261 {
261 time_t timev = (time_t)node_data->realval + MAC_EPOCH; 262 Time64_T timev = (Time64_T)node_data->realval + MAC_EPOCH;
262 struct tm *btime = gmtime(&timev); 263 struct TM _btime;
264 struct TM *btime = gmtime64_r(&timev, &_btime);
263 if (btime) { 265 if (btime) {
264 val = (char*)malloc(24); 266 val = (char*)malloc(24);
265 memset(val, 0, 24); 267 memset(val, 0, 24);
266 if (strftime(val, 24, "%Y-%m-%dT%H:%M:%SZ", btime) <= 0) { 268 struct tm _tmcopy;
269 copy_TM64_to_tm(btime, &_tmcopy);
270 if (strftime(val, 24, "%Y-%m-%dT%H:%M:%SZ", &_tmcopy) <= 0) {
267 free (val); 271 free (val);
268 val = NULL; 272 val = NULL;
269 } 273 }
@@ -346,7 +350,7 @@ static void node_to_xml(node_t* node, void *xml_struct)
346 return; 350 return;
347} 351}
348 352
349static void parse_date(const char *strval, struct tm *btime) 353static void parse_date(const char *strval, struct TM *btime)
350{ 354{
351 if (!btime) return; 355 if (!btime) return;
352 memset(btime, 0, sizeof(struct tm)); 356 memset(btime, 0, sizeof(struct tm));
@@ -354,7 +358,12 @@ static void parse_date(const char *strval, struct tm *btime)
354#ifdef strptime 358#ifdef strptime
355 strptime((char*)strval, "%Y-%m-%dT%H:%M:%SZ", btime); 359 strptime((char*)strval, "%Y-%m-%dT%H:%M:%SZ", btime);
356#else 360#else
357 sscanf(strval, "%d-%d-%dT%d:%d:%dZ", &btime->tm_year, &btime->tm_mon, &btime->tm_mday, &btime->tm_hour, &btime->tm_min, &btime->tm_sec); 361#ifdef USE_TM64
362 #define PLIST_SSCANF_FORMAT "%lld-%d-%dT%d:%d:%dZ"
363#else
364 #define PLIST_SSCANF_FORMAT "%d-%d-%dT%d:%d:%dZ"
365#endif
366 sscanf(strval, PLIST_SSCANF_FORMAT, &btime->tm_year, &btime->tm_mon, &btime->tm_mday, &btime->tm_hour, &btime->tm_min, &btime->tm_sec);
358 btime->tm_year-=1900; 367 btime->tm_year-=1900;
359 btime->tm_mon--; 368 btime->tm_mon--;
360#endif 369#endif
@@ -453,14 +462,11 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
453 if (!xmlStrcmp(node->name, XPLIST_DATE)) 462 if (!xmlStrcmp(node->name, XPLIST_DATE))
454 { 463 {
455 xmlChar *strval = xmlNodeGetContent(node); 464 xmlChar *strval = xmlNodeGetContent(node);
456 time_t timev = 0; 465 Time64_T timev = 0;
457 if (strlen((const char*)strval) >= 11) { 466 if (strlen((const char*)strval) >= 11) {
458 struct tm btime; 467 struct TM btime;
459 struct tm* tm_utc;
460 parse_date((const char*)strval, &btime); 468 parse_date((const char*)strval, &btime);
461 timev = mktime(&btime); 469 timev = timegm64(&btime);
462 tm_utc = gmtime(&timev);
463 timev -= (mktime(tm_utc) - timev);
464 } 470 }
465 data->realval = (double)(timev - MAC_EPOCH); 471 data->realval = (double)(timev - MAC_EPOCH);
466 data->type = PLIST_DATE; 472 data->type = PLIST_DATE;