1 |
/* Convert a `struct tm' to a time_t value. |
/* Convert a `struct tm' to a time_t value. |
2 |
Copyright (C) 1993-1999, 2002, 2003, 2004, 2005 Free Software Foundation, |
Copyright (C) 1993-1999, 2002-2004, 2005 Free Software Foundation, Inc. |
3 |
Inc. |
Inc. |
4 |
This file is part of the GNU C Library. |
This file is part of the GNU C Library. |
5 |
Contributed by Paul Eggert (eggert@twinsun.com). |
Contributed by Paul Eggert (eggert@twinsun.com). |
38 |
|
|
39 |
#include <limits.h> |
#include <limits.h> |
40 |
|
|
41 |
|
#include <string.h> /* For the real memcpy prototype. */ |
42 |
|
|
43 |
#if DEBUG |
#if DEBUG |
44 |
# include <stdio.h> |
# include <stdio.h> |
45 |
# include <stdlib.h> |
# include <stdlib.h> |
|
# include <string.h> |
|
46 |
/* Make it work even if the system's libc has its own mktime routine. */ |
/* Make it work even if the system's libc has its own mktime routine. */ |
47 |
# define mktime my_mktime |
# define mktime my_mktime |
48 |
#endif /* DEBUG */ |
#endif /* DEBUG */ |
229 |
ranged_convert (struct tm *(*convert) (const time_t *, struct tm *), |
ranged_convert (struct tm *(*convert) (const time_t *, struct tm *), |
230 |
time_t *t, struct tm *tp) |
time_t *t, struct tm *tp) |
231 |
{ |
{ |
232 |
struct tm *r; |
struct tm *r = convert (t, tp); |
233 |
|
|
234 |
if (! (r = (*convert) (t, tp)) && *t) |
if (!r && *t) |
235 |
{ |
{ |
236 |
time_t bad = *t; |
time_t bad = *t; |
237 |
time_t ok = 0; |
time_t ok = 0; |
|
struct tm tm; |
|
238 |
|
|
239 |
/* BAD is a known unconvertible time_t, and OK is a known good one. |
/* BAD is a known unconvertible time_t, and OK is a known good one. |
240 |
Use binary search to narrow the range between BAD and OK until |
Use binary search to narrow the range between BAD and OK until |
244 |
time_t mid = *t = (bad < 0 |
time_t mid = *t = (bad < 0 |
245 |
? bad + ((ok - bad) >> 1) |
? bad + ((ok - bad) >> 1) |
246 |
: ok + ((bad - ok) >> 1)); |
: ok + ((bad - ok) >> 1)); |
247 |
if ((r = (*convert) (t, tp))) |
r = convert (t, tp); |
248 |
{ |
if (r) |
249 |
tm = *r; |
ok = mid; |
|
ok = mid; |
|
|
} |
|
250 |
else |
else |
251 |
bad = mid; |
bad = mid; |
252 |
} |
} |
256 |
/* The last conversion attempt failed; |
/* The last conversion attempt failed; |
257 |
revert to the most recent successful attempt. */ |
revert to the most recent successful attempt. */ |
258 |
*t = ok; |
*t = ok; |
259 |
*tp = tm; |
r = convert (t, tp); |
|
r = tp; |
|
260 |
} |
} |
261 |
} |
} |
262 |
|
|
485 |
t2 = t1 + sec_adjustment; |
t2 = t1 + sec_adjustment; |
486 |
if (((t1 < t) != (sec_requested < 0)) |
if (((t1 < t) != (sec_requested < 0)) |
487 |
| ((t2 < t1) != (sec_adjustment < 0)) |
| ((t2 < t1) != (sec_adjustment < 0)) |
488 |
| ! (*convert) (&t2, &tm)) |
| ! convert (&t2, &tm)) |
489 |
return -1; |
return -1; |
490 |
t = t2; |
t = t2; |
491 |
} |
} |