- {
- struct tm *guess_tm;
- time_t guess = 0;
- time_t distance = 0;
- time_t last_distance = 0;
-
- times_through_search = 0;
-
- do
- {
- guess += distance;
-
- times_through_search++;
-
- guess_tm = (*producer) (&guess);
-
-#ifdef DEBUG
- if (debugging_enabled)
- {
- printf (" Guessing time_t == %d\n ", (int) guess);
- printtm (guess_tm);
- putchar ('\n');
- }
-#endif
-
- /* How far is our guess from the desired struct tm? */
- distance = dist_tm (me, guess_tm);
-
- /* Handle periods of time where a period of time is skipped.
- For example, 2:15 3 April 1994 does not exist, because DST
- is in effect. The distance function will alternately
- return values of 3600 and -3600, because it doesn't know
- that the requested time doesn't exist. In these situations
- (even if the skip is not exactly an hour) the distances
- returned will be the same, but alternating in sign. We
- want the later time, so check to see that the distance is
- oscillating and we've chosen the correct of the two
- possibilities.
-
- Useful: 3 Apr 94 765356300, 30 Oct 94 783496000 */
-
- if ((distance == -last_distance) && (distance < last_distance))
+ /* Repeatedly use the error to improve the guess. */
+
+ for (t = t1 = t2 = t0, dst2 = 0;
+ (gt = guess_time_tm (year, yday, hour, min, sec, &t,
+ ranged_convert (convert, &t, &tm)),
+ t != gt);
+ t1 = t2, t2 = t, t = gt, dst2 = tm.tm_isdst != 0)
+ if (t == t1 && t != t2
+ && (tm.tm_isdst < 0
+ || (isdst < 0
+ ? dst2 <= (tm.tm_isdst != 0)
+ : (isdst != 0) != (tm.tm_isdst != 0))))
+ /* We can't possibly find a match, as we are oscillating
+ between two values. The requested time probably falls
+ within a spring-forward gap of size GT - T. Follow the common
+ practice in this case, which is to return a time that is GT - T
+ away from the requested time, preferring a time whose
+ tm_isdst differs from the requested value. (If no tm_isdst
+ was requested and only one of the two values has a nonzero
+ tm_isdst, prefer that value.) In practice, this is more
+ useful than returning -1. */
+ goto offset_found;
+ else if (--remaining_probes == 0)
+ return -1;
+
+ /* We have a match. Check whether tm.tm_isdst has the requested
+ value, if any. */
+ if (isdst != tm.tm_isdst && 0 <= isdst && 0 <= tm.tm_isdst)
+ {
+ /* tm.tm_isdst has the wrong value. Look for a neighboring
+ time with the right value, and use its UTC offset.
+
+ Heuristic: probe the adjacent timestamps in both directions,
+ looking for the desired isdst. This should work for all real
+ time zone histories in the tz database. */
+
+ /* Distance between probes when looking for a DST boundary. In
+ tzdata2003a, the shortest period of DST is 601200 seconds
+ (e.g., America/Recife starting 2000-10-08 01:00), and the
+ shortest period of non-DST surrounded by DST is 694800
+ seconds (Africa/Tunis starting 1943-04-17 01:00). Use the
+ minimum of these two values, so we don't miss these short
+ periods when probing. */
+ int stride = 601200;
+
+ /* The longest period of DST in tzdata2003a is 536454000 seconds
+ (e.g., America/Jujuy starting 1946-10-01 01:00). The longest
+ period of non-DST is much longer, but it makes no real sense
+ to search for more than a year of non-DST, so use the DST
+ max. */
+ int duration_max = 536454000;
+
+ /* Search in both directions, so the maximum distance is half
+ the duration; add the stride to avoid off-by-1 problems. */
+ int delta_bound = duration_max / 2 + stride;
+
+ int delta, direction;
+
+ for (delta = stride; delta < delta_bound; delta += stride)
+ for (direction = -1; direction <= 1; direction += 2)