1 /* Copyright (C) 1993 Free Software Foundation, Inc.
2 Contributed by Noel Cragg (noel@cs.oberlin.edu).
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19 Cambridge, MA 02139, USA. */
21 /* #define DEBUG */ /* Define this to have a standalone shell to test
22 * this implementation of mktime
26 #if defined (CONFIG_BROKETS)
27 /* We use <config.h> instead of "config.h" so that a compilation
28 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
29 (which it would do because it found this file in $srcdir). */
36 #include <sys/types.h> /* Some systems define `time_t' here. */
41 /* Nonzero if YEAR is a leap year (every 4 years,
42 except every 100th isn't, and every 400th is). */
43 #define __isleap(year) \
44 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
48 /* How many days are in each month. */
49 const unsigned short int __mon_lengths[2][12] =
52 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
54 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
58 static int times_through_search; /* This library routine should never
59 hang -- make sure we always return
60 when we're searching for a value */
62 /* After testing this, the maximum number of iterations that I had on
63 any number that I tried was 3! Not bad.
65 mktime converts a `struct tm' (broken-down local time) into a `time_t';
66 it is the opposite of localtime. It is possible to put the following
67 values out of range and have mktime compensate: tm_sec, tm_min, tm_hour,
68 tm_mday, tm_year. The other values in the structure are ignored. */
75 int debugging_enabled = 0;
77 /* Print the values in a `struct tm'. */
82 printf ("%d/%d/%d %d:%d:%d (%s) yday:%d f:%d o:%ld",
103 unsigned long int v1, v2;
108 #define doit(x, secs) \
109 v1 += t1->x * secs; \
110 v2 += t2->x * secs; \
115 else if (t1->x > t2->x) \
119 doit (tm_year, 31536000); /* Okay, not all years have 365 days. */
120 doit (tm_mon, 2592000); /* Okay, not all months have 30 days. */
121 doit (tm_mday, 86400);
122 doit (tm_hour, 3600);
128 /* We should also make sure that the sign of DISTANCE is correct --
129 * if DIFF_FLAG is positive, the distance should be positive and
132 distance = (v1 > v2) ? (v1 - v2) : (v2 - v1);
134 distance = -distance;
136 if (times_through_search > 20) /* Arbitrary # of calls, but makes
137 sure we never hang if there's a
138 problem with this algorithm */
140 distance = diff_flag;
143 /* We need this DIFF_FLAG business because it is forseeable that
144 * the distance may be zero when, in actuality, the two structures
145 * are different. This is usually the case when the dates are
146 * 366 days apart and one of the years is a leap year. */
148 if ((distance == 0) && diff_flag)
149 distance = 86400 * diff_flag;
155 /* Modified b-search -- make intelligent guesses as to where the time
156 * might lie along the timeline, assuming that our target time lies a
157 * linear distance (w/o considering time jumps of a particular region).
159 * Assume that time does not fluctuate at all along the timeline --
160 * e.g., assume that a day will always take 86400 seconds, etc. -- and
161 * come up with a hypothetical value for the time_t representation of
162 * the struct tm TARGET, in relation to the guess variable -- it should
163 * be pretty close! */
173 times_through_search = 0;
179 times_through_search++;
181 guess_tm = localtime (&guess);
184 if (debugging_enabled)
186 printf ("guess %d == ", (int) guess);
192 /* Are we on the money? */
193 distance = dist_tm (target, guess_tm);
195 } while (distance != 0);
200 /* Since this function will call localtime many times (and the user might
201 * be passing their `struct tm *' right from localtime, let's make a copy
202 * for ourselves and run the search on the copy.
204 * Also, we have to normalize the timeptr because it's possible to call mktime
205 * with values that are out of range for a specific item (like 30th Feb). */
210 struct tm private_mktime_struct_tm; /* Yes, users can get a ptr to this. */
214 me = &private_mktime_struct_tm;
218 #define normalize(foo,x,y,bar); \
219 while (me->foo < x) \
222 me->foo = (y - (x - me->foo)); \
224 while (me->foo > y) \
227 me->foo = (x + (me->foo - y)); \
230 normalize (tm_sec, 0, 59, tm_min);
231 normalize (tm_min, 0, 59, tm_hour);
232 normalize (tm_hour, 0, 23, tm_mday);
234 /* Do the month first, so day range can be found. */
235 normalize (tm_mon, 0, 11, tm_year);
236 normalize (tm_mday, 1,
237 __mon_lengths[__isleap (me->tm_year)][me->tm_mon],
240 /* Do the month again, because the day may have pushed it out of range. */
241 normalize (tm_mon, 0, 11, tm_year);
243 /* Do the day again, because the month may have changed the range. */
244 normalize (tm_mday, 1,
245 __mon_lengths[__isleap (me->tm_year)][me->tm_mon],
249 if (debugging_enabled)
251 printf ("After normalizing: ");
257 result = search (me);
278 printf ("starting long test...\n");
280 for (q = 10000000; q < 1000000000; q++)
282 struct tm *tm = localtime (&q);
283 if ((q % 10000) == 0) { printf ("%ld\n", q); fflush (stdout); }
284 if (q != mktime (tm))
285 { printf ("failed for %ld\n", q); fflush (stdout); }
288 printf ("test finished\n");
295 printf ("wrong # of args\n");
299 debugging_enabled = 1; /* We want to see the info */
304 printf ("Time: %d %s\n", time, ctime ((time_t *) &time));
306 tmptr = localtime ((time_t *) &time);
307 printf ("localtime returns: ");
310 printf ("mktime: %d\n\n", (int) mktime (tmptr));
314 tmptr->tm_hour -= 20;
315 tmptr->tm_mday -= 20;
317 tmptr->tm_year -= 20;
318 tmptr->tm_gmtoff -= 20000; /* This has no effect! */
319 tmptr->tm_zone = NULL; /* Nor does this! */
320 tmptr->tm_isdst = -1;
322 printf ("changed ranges: ");
326 result_time = mktime (tmptr);
327 printf ("\nmktime: %d\n", result_time);