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