Mercurial > gnulib
annotate lib/mktime.c @ 39899:f0d0013f13a7
autoupdate
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Mon, 08 Oct 2018 11:32:09 -0700 |
parents | c9d15b629a3a |
children | 7ac9c014f477 |
rev | line source |
---|---|
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
1 /* Convert a 'struct tm' to a time_t value. |
19484
10eb9086bea0
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19190
diff
changeset
|
2 Copyright (C) 1993-2018 Free Software Foundation, Inc. |
1713 | 3 This file is part of the GNU C Library. |
7294 | 4 Contributed by Paul Eggert <eggert@twinsun.com>. |
508 | 5 |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
6 The GNU C Library is free software; you can redistribute it and/or |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
7 modify it under the terms of the GNU Lesser General Public |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
8 License as published by the Free Software Foundation; either |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
9 version 2.1 of the License, or (at your option) any later version. |
9 | 10 |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
11 The GNU C Library is distributed in the hope that it will be useful, |
782 | 12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
14 Lesser General Public License for more details. |
9 | 15 |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
16 You should have received a copy of the GNU Lesser General Public |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
17 License along with the GNU C Library; if not, see |
19190 | 18 <https://www.gnu.org/licenses/>. */ |
9 | 19 |
18304
e04e8b486f70
mktime: simplify DEBUG_MKTIME
Paul Eggert <eggert@cs.ucla.edu>
parents:
18303
diff
changeset
|
20 /* Define this to 1 to have a standalone program to test this implementation of |
187 | 21 mktime. */ |
18304
e04e8b486f70
mktime: simplify DEBUG_MKTIME
Paul Eggert <eggert@cs.ucla.edu>
parents:
18303
diff
changeset
|
22 #ifndef DEBUG_MKTIME |
e04e8b486f70
mktime: simplify DEBUG_MKTIME
Paul Eggert <eggert@cs.ucla.edu>
parents:
18303
diff
changeset
|
23 # define DEBUG_MKTIME 0 |
e04e8b486f70
mktime: simplify DEBUG_MKTIME
Paul Eggert <eggert@cs.ucla.edu>
parents:
18303
diff
changeset
|
24 #endif |
187 | 25 |
18838
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
26 /* The following macros influence what gets defined when this file is compiled: |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
27 |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
28 Macro/expression Which gnulib module This compilation unit |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
29 should define |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
30 |
39812
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
31 _LIBC (glibc proper) mktime |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
32 |
18838
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
33 NEED_MKTIME_WORKING mktime rpl_mktime |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
34 || NEED_MKTIME_WINDOWS |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
35 |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
36 NEED_MKTIME_INTERNAL mktime-internal mktime_internal |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
37 |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
38 DEBUG_MKTIME (defined manually) my_mktime, main |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
39 */ |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
40 |
18305
ebba8469e29d
mktime: resurrect DEBUG_MKTIME testing
Paul Eggert <eggert@cs.ucla.edu>
parents:
18304
diff
changeset
|
41 #if !defined _LIBC && !DEBUG_MKTIME |
1109 | 42 # include <config.h> |
9 | 43 #endif |
44 | |
508 | 45 /* Assume that leap seconds are possible, unless told otherwise. |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
46 If the host has a 'zic' command with a '-L leapsecondfilename' option, |
508 | 47 then it supports leap seconds; otherwise it probably doesn't. */ |
48 #ifndef LEAP_SECONDS_POSSIBLE | |
1109 | 49 # define LEAP_SECONDS_POSSIBLE 1 |
508 | 50 #endif |
51 | |
187 | 52 #include <time.h> |
53 | |
4354
7b5fd4dc4f90
Assume freestanding C89 or better.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4352
diff
changeset
|
54 #include <limits.h> |
18296
fd2d8716107c
mktime: improve integer overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18294
diff
changeset
|
55 #include <stdbool.h> |
39812
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
56 #include <stdlib.h> |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
57 #include <string.h> |
9 | 58 |
18296
fd2d8716107c
mktime: improve integer overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18294
diff
changeset
|
59 #include <intprops.h> |
fd2d8716107c
mktime: improve integer overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18294
diff
changeset
|
60 #include <verify.h> |
5932
8ef7d6084536
* mktime.c: Include <string.h> even if !DEBUG. (From glibc.)
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
61 |
18304
e04e8b486f70
mktime: simplify DEBUG_MKTIME
Paul Eggert <eggert@cs.ucla.edu>
parents:
18303
diff
changeset
|
62 #if DEBUG_MKTIME |
1109 | 63 # include <stdio.h> |
508 | 64 /* Make it work even if the system's libc has its own mktime routine. */ |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
65 # undef mktime |
1109 | 66 # define mktime my_mktime |
39812
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
67 #endif /* DEBUG_MKTIME */ |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
68 |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
69 #ifndef NEED_MKTIME_INTERNAL |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
70 # define NEED_MKTIME_INTERNAL 0 |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
71 #endif |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
72 #ifndef NEED_MKTIME_WINDOWS |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
73 # define NEED_MKTIME_WINDOWS 0 |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
74 #endif |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
75 #ifndef NEED_MKTIME_WORKING |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
76 # define NEED_MKTIME_WORKING DEBUG_MKTIME |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
77 #endif |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
78 |
39832
8228a72f9f50
mktime: simplify in prep for glibc merge
Paul Eggert <eggert@cs.ucla.edu>
parents:
39814
diff
changeset
|
79 #include "mktime-internal.h" |
9 | 80 |
39899 | 81 #if !defined _LIBC && (NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS) |
39812
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
82 static void |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
83 my_tzset (void) |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
84 { |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
85 # if NEED_MKTIME_WINDOWS |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
86 /* Rectify the value of the environment variable TZ. |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
87 There are four possible kinds of such values: |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
88 - Traditional US time zone names, e.g. "PST8PDT". Syntax: see |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
89 <https://msdn.microsoft.com/en-us/library/90s5c885.aspx> |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
90 - Time zone names based on geography, that contain one or more |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
91 slashes, e.g. "Europe/Moscow". |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
92 - Time zone names based on geography, without slashes, e.g. |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
93 "Singapore". |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
94 - Time zone names that contain explicit DST rules. Syntax: see |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
95 <http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03> |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
96 The Microsoft CRT understands only the first kind. It produces incorrect |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
97 results if the value of TZ is of the other kinds. |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
98 But in a Cygwin environment, /etc/profile.d/tzset.sh sets TZ to a value |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
99 of the second kind for most geographies, or of the first kind in a few |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
100 other geographies. If it is of the second kind, neutralize it. For the |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
101 Microsoft CRT, an absent or empty TZ means the time zone that the user |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
102 has set in the Windows Control Panel. |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
103 If the value of TZ is of the third or fourth kind -- Cygwin programs |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
104 understand these syntaxes as well --, it does not matter whether we |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
105 neutralize it or not, since these values occur only when a Cygwin user |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
106 has set TZ explicitly; this case is 1. rare and 2. under the user's |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
107 responsibility. */ |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
108 const char *tz = getenv ("TZ"); |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
109 if (tz != NULL && strchr (tz, '/') != NULL) |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
110 _putenv ("TZ="); |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
111 # elif HAVE_TZSET |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
112 tzset (); |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
113 # endif |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
114 } |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
115 # undef __tzset |
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
116 # define __tzset() my_tzset () |
18838
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
117 #endif |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
118 |
39812
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
119 #if defined _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_INTERNAL |
18838
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
120 |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
121 /* A signed type that can represent an integer number of years |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
122 multiplied by three times the number of seconds in a year. It is |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
123 needed when converting a tm_year value times the number of seconds |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
124 in a year. The factor of three comes because these products need |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
125 to be subtracted from each other, and sometimes with an offset |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
126 added to them, without worrying about overflow. |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
127 |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
128 Much of the code uses long_int to represent time_t values, to |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
129 lessen the hassle of dealing with platforms where time_t is |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
130 unsigned, and because long_int should suffice to represent all |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
131 time_t values that mktime can generate even on platforms where |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
132 time_t is excessively wide. */ |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
133 |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
134 #if INT_MAX <= LONG_MAX / 3 / 366 / 24 / 60 / 60 |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
135 typedef long int long_int; |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
136 #else |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
137 typedef long long int long_int; |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
138 #endif |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
139 verify (INT_MAX <= TYPE_MAXIMUM (long_int) / 3 / 366 / 24 / 60 / 60); |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
140 |
5409
bf796f27ccfc
(SHR): New macro, which is a portable
Paul Eggert <eggert@cs.ucla.edu>
parents:
5371
diff
changeset
|
141 /* Shift A right by B bits portably, by dividing A by 2**B and |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
142 truncating towards minus infinity. B should be in the range 0 <= B |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
143 <= LONG_INT_BITS - 2, where LONG_INT_BITS is the number of useful |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
144 bits in a long_int. LONG_INT_BITS is at least 32. |
5409
bf796f27ccfc
(SHR): New macro, which is a portable
Paul Eggert <eggert@cs.ucla.edu>
parents:
5371
diff
changeset
|
145 |
bf796f27ccfc
(SHR): New macro, which is a portable
Paul Eggert <eggert@cs.ucla.edu>
parents:
5371
diff
changeset
|
146 ISO C99 says that A >> B is implementation-defined if A < 0. Some |
bf796f27ccfc
(SHR): New macro, which is a portable
Paul Eggert <eggert@cs.ucla.edu>
parents:
5371
diff
changeset
|
147 implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift |
bf796f27ccfc
(SHR): New macro, which is a portable
Paul Eggert <eggert@cs.ucla.edu>
parents:
5371
diff
changeset
|
148 right in the usual way when A < 0, so SHR falls back on division if |
bf796f27ccfc
(SHR): New macro, which is a portable
Paul Eggert <eggert@cs.ucla.edu>
parents:
5371
diff
changeset
|
149 ordinary A >> B doesn't seem to be the usual signed shift. */ |
bf796f27ccfc
(SHR): New macro, which is a portable
Paul Eggert <eggert@cs.ucla.edu>
parents:
5371
diff
changeset
|
150 |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
151 static long_int |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
152 shr (long_int a, int b) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
153 { |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
154 long_int one = 1; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
155 return (-one >> 1 == -1 |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
156 ? a >> b |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
157 : a / (one << b) - (a % (one << b) < 0)); |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
158 } |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
159 |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
160 /* Bounds for the intersection of time_t and long_int. */ |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
161 |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
162 static long_int const mktime_min |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
163 = ((TYPE_SIGNED (time_t) && TYPE_MINIMUM (time_t) < TYPE_MINIMUM (long_int)) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
164 ? TYPE_MINIMUM (long_int) : TYPE_MINIMUM (time_t)); |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
165 static long_int const mktime_max |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
166 = (TYPE_MAXIMUM (long_int) < TYPE_MAXIMUM (time_t) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
167 ? TYPE_MAXIMUM (long_int) : TYPE_MAXIMUM (time_t)); |
508 | 168 |
18296
fd2d8716107c
mktime: improve integer overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18294
diff
changeset
|
169 verify (TYPE_IS_INTEGER (time_t)); |
508 | 170 |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
171 #define EPOCH_YEAR 1970 |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
172 #define TM_YEAR_BASE 1900 |
18296
fd2d8716107c
mktime: improve integer overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18294
diff
changeset
|
173 verify (TM_YEAR_BASE % 100 == 0); |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
174 |
18296
fd2d8716107c
mktime: improve integer overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18294
diff
changeset
|
175 /* Is YEAR + TM_YEAR_BASE a leap year? */ |
fd2d8716107c
mktime: improve integer overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18294
diff
changeset
|
176 static bool |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
177 leapyear (long_int year) |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
178 { |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
179 /* Don't add YEAR to TM_YEAR_BASE, as that might overflow. |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
180 Also, work even if YEAR is negative. */ |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
181 return |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
182 ((year & 3) == 0 |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
183 && (year % 100 != 0 |
16877 | 184 || ((year / 100) & 3) == (- (TM_YEAR_BASE / 100) & 3))); |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
185 } |
508 | 186 |
187 /* How many days come before each month (0-12). */ | |
4026
c4a75407123a
Sync from libc, now that it has the latest fix.
Jim Meyering <jim@meyering.net>
parents:
4018
diff
changeset
|
188 #ifndef _LIBC |
c4a75407123a
Sync from libc, now that it has the latest fix.
Jim Meyering <jim@meyering.net>
parents:
4018
diff
changeset
|
189 static |
c4a75407123a
Sync from libc, now that it has the latest fix.
Jim Meyering <jim@meyering.net>
parents:
4018
diff
changeset
|
190 #endif |
c4a75407123a
Sync from libc, now that it has the latest fix.
Jim Meyering <jim@meyering.net>
parents:
4018
diff
changeset
|
191 const unsigned short int __mon_yday[2][13] = |
187 | 192 { |
193 /* Normal years. */ | |
508 | 194 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, |
187 | 195 /* Leap years. */ |
508 | 196 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } |
187 | 197 }; |
198 | |
199 | |
18296
fd2d8716107c
mktime: improve integer overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18294
diff
changeset
|
200 /* Do the values A and B differ according to the rules for tm_isdst? |
fd2d8716107c
mktime: improve integer overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18294
diff
changeset
|
201 A and B differ if one is zero and the other positive. */ |
fd2d8716107c
mktime: improve integer overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18294
diff
changeset
|
202 static bool |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
203 isdst_differ (int a, int b) |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
204 { |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
205 return (!a != !b) && (0 <= a) && (0 <= b); |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
206 } |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
207 |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
208 /* Return an integer value measuring (YEAR1-YDAY1 HOUR1:MIN1:SEC1) - |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
209 (YEAR0-YDAY0 HOUR0:MIN0:SEC0) in seconds, assuming that the clocks |
18663
caf9d6314c8f
maint: time stamp -> timestamp
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
210 were not adjusted between the timestamps. |
9 | 211 |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
212 The YEAR values uses the same numbering as TP->tm_year. Values |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
213 need not be in the usual range. However, YEAR1 must not overflow |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
214 when multiplied by three times the number of seconds in a year, and |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
215 likewise for YDAY1 and three times the number of seconds in a day. */ |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
216 |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
217 static long_int |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
218 ydhms_diff (long_int year1, long_int yday1, int hour1, int min1, int sec1, |
16877 | 219 int year0, int yday0, int hour0, int min0, int sec0) |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
220 { |
18296
fd2d8716107c
mktime: improve integer overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18294
diff
changeset
|
221 verify (-1 / 2 == 0); |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
222 |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
223 /* Compute intervening leap days correctly even if year is negative. |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
224 Take care to avoid integer overflow here. */ |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
225 int a4 = shr (year1, 2) + shr (TM_YEAR_BASE, 2) - ! (year1 & 3); |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
226 int b4 = shr (year0, 2) + shr (TM_YEAR_BASE, 2) - ! (year0 & 3); |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
227 int a100 = a4 / 25 - (a4 % 25 < 0); |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
228 int b100 = b4 / 25 - (b4 % 25 < 0); |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
229 int a400 = shr (a100, 2); |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
230 int b400 = shr (b100, 2); |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
231 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
232 |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
233 /* Compute the desired time without overflowing. */ |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
234 long_int years = year1 - year0; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
235 long_int days = 365 * years + yday1 - yday0 + intervening_leap_days; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
236 long_int hours = 24 * days + hour1 - hour0; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
237 long_int minutes = 60 * hours + min1 - min0; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
238 long_int seconds = 60 * minutes + sec1 - sec0; |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
239 return seconds; |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
240 } |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
241 |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
242 /* Return the average of A and B, even if A + B would overflow. |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
243 Round toward positive infinity. */ |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
244 static long_int |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
245 long_int_avg (long_int a, long_int b) |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
246 { |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
247 return shr (a, 1) + shr (b, 1) + ((a | b) & 1); |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
248 } |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
249 |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
250 /* Return a time_t value corresponding to (YEAR-YDAY HOUR:MIN:SEC), |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
251 assuming that T corresponds to *TP and that no clock adjustments |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
252 occurred between *TP and the desired time. |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
253 Although T and the returned value are of type long_int, |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
254 they represent time_t values and must be in time_t range. |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
255 If TP is null, return a value not equal to T; this avoids false matches. |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
256 YEAR and YDAY must not be so large that multiplying them by three times the |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
257 number of seconds in a year (or day, respectively) would overflow long_int. |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
258 If the returned value would be out of range, yield the minimal or |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
259 maximal in-range value, except do not yield a value equal to T. */ |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
260 static long_int |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
261 guess_time_tm (long_int year, long_int yday, int hour, int min, int sec, |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
262 long_int t, const struct tm *tp) |
187 | 263 { |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
264 if (tp) |
1109 | 265 { |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
266 long_int result; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
267 long_int d = ydhms_diff (year, yday, hour, min, sec, |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
268 tp->tm_year, tp->tm_yday, |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
269 tp->tm_hour, tp->tm_min, tp->tm_sec); |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
270 if (! INT_ADD_WRAPV (t, d, &result)) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
271 return result; |
1109 | 272 } |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
273 |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
274 /* Overflow occurred one way or another. Return the nearest result |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
275 that is actually in range, except don't report a zero difference |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
276 if the actual difference is nonzero, as that would cause a false |
7276
4be2c469b318
* config/srclist.txt: Temporarily comment out mktime.c until glibc bug
Paul Eggert <eggert@cs.ucla.edu>
parents:
6243
diff
changeset
|
277 match; and don't oscillate between two values, as that would |
4be2c469b318
* config/srclist.txt: Temporarily comment out mktime.c until glibc bug
Paul Eggert <eggert@cs.ucla.edu>
parents:
6243
diff
changeset
|
278 confuse the spring-forward gap detector. */ |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
279 return (t < long_int_avg (mktime_min, mktime_max) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
280 ? (t <= mktime_min + 1 ? t + 1 : mktime_min) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
281 : (mktime_max - 1 <= t ? t - 1 : mktime_max)); |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
282 } |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
283 |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
284 /* Use CONVERT to convert T to a struct tm value in *TM. T must be in |
18308
3e948459432e
mktime: spelling fix in comment
Paul Eggert <eggert@cs.ucla.edu>
parents:
18307
diff
changeset
|
285 range for time_t. Return TM if successful, NULL if T is out of |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
286 range for CONVERT. */ |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
287 static struct tm * |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
288 convert_time (struct tm *(*convert) (const time_t *, struct tm *), |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
289 long_int t, struct tm *tm) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
290 { |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
291 time_t x = t; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
292 return convert (&x, tm); |
295 | 293 } |
294 | |
1109 | 295 /* Use CONVERT to convert *T to a broken down time in *TP. |
296 If *T is out of range for conversion, adjust it so that | |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
297 it is the nearest in-range value and then convert that. |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
298 A value is in range if it fits in both time_t and long_int. */ |
1109 | 299 static struct tm * |
1557 | 300 ranged_convert (struct tm *(*convert) (const time_t *, struct tm *), |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
301 long_int *t, struct tm *tp) |
1109 | 302 { |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
303 struct tm *r; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
304 if (*t < mktime_min) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
305 *t = mktime_min; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
306 else if (mktime_max < *t) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
307 *t = mktime_max; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
308 r = convert_time (convert, *t, tp); |
1109 | 309 |
5932
8ef7d6084536
* mktime.c: Include <string.h> even if !DEBUG. (From glibc.)
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
310 if (!r && *t) |
1109 | 311 { |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
312 long_int bad = *t; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
313 long_int ok = 0; |
1109 | 314 |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
315 /* BAD is a known unconvertible value, and OK is a known good one. |
16877 | 316 Use binary search to narrow the range between BAD and OK until |
317 they differ by 1. */ | |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
318 while (true) |
16877 | 319 { |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
320 long_int mid = long_int_avg (ok, bad); |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
321 if (mid != ok && mid != bad) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
322 break; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
323 r = convert_time (convert, mid, tp); |
16877 | 324 if (r) |
325 ok = mid; | |
326 else | |
327 bad = mid; | |
328 } | |
1109 | 329 |
330 if (!r && ok) | |
16877 | 331 { |
332 /* The last conversion attempt failed; | |
333 revert to the most recent successful attempt. */ | |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
334 r = convert_time (convert, ok, tp); |
16877 | 335 } |
1109 | 336 } |
337 | |
338 return r; | |
339 } | |
340 | |
39812
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
341 |
508 | 342 /* Convert *TP to a time_t value, inverting |
343 the monotonic and mostly-unit-linear conversion function CONVERT. | |
344 Use *OFFSET to keep track of a guess at the offset of the result, | |
345 compared to what the result would be for UTC without leap seconds. | |
4931
3743557233a8
(__mktime_internal) [!_LIBC]: Define to mktime_internal
Jim Meyering <jim@meyering.net>
parents:
4666
diff
changeset
|
346 If *OFFSET's guess is correct, only one CONVERT call is needed. |
3743557233a8
(__mktime_internal) [!_LIBC]: Define to mktime_internal
Jim Meyering <jim@meyering.net>
parents:
4666
diff
changeset
|
347 This function is external because it is used also by timegm.c. */ |
4026
c4a75407123a
Sync from libc, now that it has the latest fix.
Jim Meyering <jim@meyering.net>
parents:
4018
diff
changeset
|
348 time_t |
1557 | 349 __mktime_internal (struct tm *tp, |
16877 | 350 struct tm *(*convert) (const time_t *, struct tm *), |
18303
8b34efae5441
Port mktime_internal offset to unsigned time_t
Paul Eggert <eggert@cs.ucla.edu>
parents:
18296
diff
changeset
|
351 mktime_offset_t *offset) |
508 | 352 { |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
353 long_int t, gt, t0, t1, t2, dt; |
508 | 354 struct tm tm; |
355 | |
356 /* The maximum number of probes (calls to CONVERT) should be enough | |
357 to handle any combinations of time zone rule changes, solar time, | |
1517
3f9b7ee1667a
(__mktime_internal): When the requested time falls
Jim Meyering <jim@meyering.net>
parents:
1516
diff
changeset
|
358 leap seconds, and oscillations around a spring-forward gap. |
3f9b7ee1667a
(__mktime_internal): When the requested time falls
Jim Meyering <jim@meyering.net>
parents:
1516
diff
changeset
|
359 POSIX.1 prohibits leap seconds, but some hosts have them anyway. */ |
3f9b7ee1667a
(__mktime_internal): When the requested time falls
Jim Meyering <jim@meyering.net>
parents:
1516
diff
changeset
|
360 int remaining_probes = 6; |
508 | 361 |
362 /* Time requested. Copy it in case CONVERT modifies *TP; this can | |
363 occur if TP is localtime's returned value and CONVERT is localtime. */ | |
364 int sec = tp->tm_sec; | |
365 int min = tp->tm_min; | |
366 int hour = tp->tm_hour; | |
367 int mday = tp->tm_mday; | |
368 int mon = tp->tm_mon; | |
369 int year_requested = tp->tm_year; | |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
370 int isdst = tp->tm_isdst; |
508 | 371 |
4018
ad164b161300
Merge in changes from libc.
Jim Meyering <jim@meyering.net>
parents:
4016
diff
changeset
|
372 /* 1 if the previous probe was DST. */ |
ad164b161300
Merge in changes from libc.
Jim Meyering <jim@meyering.net>
parents:
4016
diff
changeset
|
373 int dst2; |
ad164b161300
Merge in changes from libc.
Jim Meyering <jim@meyering.net>
parents:
4016
diff
changeset
|
374 |
508 | 375 /* Ensure that mon is in range, and set year accordingly. */ |
376 int mon_remainder = mon % 12; | |
377 int negative_mon_remainder = mon_remainder < 0; | |
378 int mon_years = mon / 12 - negative_mon_remainder; | |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
379 long_int lyear_requested = year_requested; |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
380 long_int year = lyear_requested + mon_years; |
508 | 381 |
382 /* The other values need not be in range: | |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
383 the remaining code handles overflows correctly. */ |
508 | 384 |
385 /* Calculate day of year from year, month, and day of month. | |
386 The result need not be in range. */ | |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
387 int mon_yday = ((__mon_yday[leapyear (year)] |
16877 | 388 [mon_remainder + 12 * negative_mon_remainder]) |
389 - 1); | |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
390 long_int lmday = mday; |
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
391 long_int yday = mon_yday + lmday; |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
392 |
39814
6cbd05e63545
mktime: fix unlikely race+overflow bug
Paul Eggert <eggert@cs.ucla.edu>
parents:
39813
diff
changeset
|
393 mktime_offset_t off = *offset; |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
394 int negative_offset_guess; |
508 | 395 |
1030 | 396 int sec_requested = sec; |
4018
ad164b161300
Merge in changes from libc.
Jim Meyering <jim@meyering.net>
parents:
4016
diff
changeset
|
397 |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
398 if (LEAP_SECONDS_POSSIBLE) |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
399 { |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
400 /* Handle out-of-range seconds specially, |
16877 | 401 since ydhms_tm_diff assumes every minute has 60 seconds. */ |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
402 if (sec < 0) |
16877 | 403 sec = 0; |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
404 if (59 < sec) |
16877 | 405 sec = 59; |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
406 } |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
407 |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
408 /* Invert CONVERT by probing. First assume the same offset as last |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
409 time. */ |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
410 |
39814
6cbd05e63545
mktime: fix unlikely race+overflow bug
Paul Eggert <eggert@cs.ucla.edu>
parents:
39813
diff
changeset
|
411 INT_SUBTRACT_WRAPV (0, off, &negative_offset_guess); |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
412 t0 = ydhms_diff (year, yday, hour, min, sec, |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
413 EPOCH_YEAR - TM_YEAR_BASE, 0, 0, 0, negative_offset_guess); |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
414 |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
415 /* Repeatedly use the error to improve the guess. */ |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
416 |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
417 for (t = t1 = t2 = t0, dst2 = 0; |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
418 (gt = guess_time_tm (year, yday, hour, min, sec, t, |
16877 | 419 ranged_convert (convert, &t, &tm)), |
420 t != gt); | |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
421 t1 = t2, t2 = t, t = gt, dst2 = tm.tm_isdst != 0) |
1517
3f9b7ee1667a
(__mktime_internal): When the requested time falls
Jim Meyering <jim@meyering.net>
parents:
1516
diff
changeset
|
422 if (t == t1 && t != t2 |
16877 | 423 && (tm.tm_isdst < 0 |
424 || (isdst < 0 | |
425 ? dst2 <= (tm.tm_isdst != 0) | |
426 : (isdst != 0) != (tm.tm_isdst != 0)))) | |
1517
3f9b7ee1667a
(__mktime_internal): When the requested time falls
Jim Meyering <jim@meyering.net>
parents:
1516
diff
changeset
|
427 /* We can't possibly find a match, as we are oscillating |
16877 | 428 between two values. The requested time probably falls |
429 within a spring-forward gap of size GT - T. Follow the common | |
430 practice in this case, which is to return a time that is GT - T | |
431 away from the requested time, preferring a time whose | |
432 tm_isdst differs from the requested value. (If no tm_isdst | |
433 was requested and only one of the two values has a nonzero | |
434 tm_isdst, prefer that value.) In practice, this is more | |
435 useful than returning -1. */ | |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
436 goto offset_found; |
1517
3f9b7ee1667a
(__mktime_internal): When the requested time falls
Jim Meyering <jim@meyering.net>
parents:
1516
diff
changeset
|
437 else if (--remaining_probes == 0) |
508 | 438 return -1; |
439 | |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
440 /* We have a match. Check whether tm.tm_isdst has the requested |
1517
3f9b7ee1667a
(__mktime_internal): When the requested time falls
Jim Meyering <jim@meyering.net>
parents:
1516
diff
changeset
|
441 value, if any. */ |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
442 if (isdst_differ (isdst, tm.tm_isdst)) |
508 | 443 { |
1661
f3f8922f57bb
(__mktime_internal): Adopt the traditional (and
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
444 /* tm.tm_isdst has the wrong value. Look for a neighboring |
16877 | 445 time with the right value, and use its UTC offset. |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
446 |
16877 | 447 Heuristic: probe the adjacent timestamps in both directions, |
448 looking for the desired isdst. This should work for all real | |
449 time zone histories in the tz database. */ | |
1661
f3f8922f57bb
(__mktime_internal): Adopt the traditional (and
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
450 |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
451 /* Distance between probes when looking for a DST boundary. In |
16877 | 452 tzdata2003a, the shortest period of DST is 601200 seconds |
453 (e.g., America/Recife starting 2000-10-08 01:00), and the | |
454 shortest period of non-DST surrounded by DST is 694800 | |
455 seconds (Africa/Tunis starting 1943-04-17 01:00). Use the | |
456 minimum of these two values, so we don't miss these short | |
457 periods when probing. */ | |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
458 int stride = 601200; |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
459 |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
460 /* The longest period of DST in tzdata2003a is 536454000 seconds |
16877 | 461 (e.g., America/Jujuy starting 1946-10-01 01:00). The longest |
462 period of non-DST is much longer, but it makes no real sense | |
463 to search for more than a year of non-DST, so use the DST | |
464 max. */ | |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
465 int duration_max = 536454000; |
1661
f3f8922f57bb
(__mktime_internal): Adopt the traditional (and
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
466 |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
467 /* Search in both directions, so the maximum distance is half |
16877 | 468 the duration; add the stride to avoid off-by-1 problems. */ |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
469 int delta_bound = duration_max / 2 + stride; |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
470 |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
471 int delta, direction; |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
472 |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
473 for (delta = stride; delta < delta_bound; delta += stride) |
16877 | 474 for (direction = -1; direction <= 1; direction += 2) |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
475 { |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
476 long_int ot; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
477 if (! INT_ADD_WRAPV (t, delta * direction, &ot)) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
478 { |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
479 struct tm otm; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
480 ranged_convert (convert, &ot, &otm); |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
481 if (! isdst_differ (isdst, otm.tm_isdst)) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
482 { |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
483 /* We found the desired tm_isdst. |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
484 Extrapolate back to the desired time. */ |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
485 t = guess_time_tm (year, yday, hour, min, sec, ot, &otm); |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
486 ranged_convert (convert, &t, &tm); |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
487 goto offset_found; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
488 } |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
489 } |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
490 } |
508 | 491 } |
492 | |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
493 offset_found: |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
494 /* Set *OFFSET to the low-order bits of T - T0 - NEGATIVE_OFFSET_GUESS. |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
495 This is just a heuristic to speed up the next mktime call, and |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
496 correctness is unaffected if integer overflow occurs here. */ |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
497 INT_SUBTRACT_WRAPV (t, t0, &dt); |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
498 INT_SUBTRACT_WRAPV (dt, negative_offset_guess, offset); |
508 | 499 |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
500 if (LEAP_SECONDS_POSSIBLE && sec_requested != tm.tm_sec) |
508 | 501 { |
502 /* Adjust time to reflect the tm_sec requested, not the normalized value. | |
16877 | 503 Also, repair any damage from a false match due to a leap second. */ |
18307
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
504 long_int sec_adjustment = sec == 0 && tm.tm_sec == 60; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
505 sec_adjustment -= sec; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
506 sec_adjustment += sec_requested; |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
507 if (INT_ADD_WRAPV (t, sec_adjustment, &t) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
508 || ! (mktime_min <= t && t <= mktime_max) |
534fd793480e
mktime: port to stricter signed overflow checking
Paul Eggert <eggert@cs.ucla.edu>
parents:
18306
diff
changeset
|
509 || ! convert_time (convert, t, &tm)) |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
510 return -1; |
508 | 511 } |
512 | |
513 *tp = tm; | |
514 return t; | |
187 | 515 } |
508 | 516 |
39812
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
517 #endif /* _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_INTERNAL */ |
18838
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
518 |
39812
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
519 #if defined _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS |
1713 | 520 |
521 /* Convert *TP to a time_t value. */ | |
522 time_t | |
4354
7b5fd4dc4f90
Assume freestanding C89 or better.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4352
diff
changeset
|
523 mktime (struct tm *tp) |
1713 | 524 { |
525 /* POSIX.1 8.1.1 requires that whenever mktime() is called, the | |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
526 time zone names contained in the external variable 'tzname' shall |
1713 | 527 be set as if the tzset() function had been called. */ |
528 __tzset (); | |
529 | |
39863 | 530 # if defined _LIBC || NEED_MKTIME_WORKING |
39812
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
531 static mktime_offset_t localtime_offset; |
4621
3d28ad4e18e2
Add time_r module. Change timegm, mktime, and strftime to use localtime_r
Paul Eggert <eggert@cs.ucla.edu>
parents:
4417
diff
changeset
|
532 return __mktime_internal (tp, __localtime_r, &localtime_offset); |
18838
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
533 # else |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
534 # undef mktime |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
535 return mktime (tp); |
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
536 # endif |
1713 | 537 } |
39812
9971c7f37ee6
mktime, timegm: simplify merge to glibc
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
538 #endif /* _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS */ |
18838
e1dcce772ae6
mktime: Work around TZ problem on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18663
diff
changeset
|
539 |
508 | 540 #ifdef weak_alias |
541 weak_alias (mktime, timelocal) | |
542 #endif | |
4018
ad164b161300
Merge in changes from libc.
Jim Meyering <jim@meyering.net>
parents:
4016
diff
changeset
|
543 |
ad164b161300
Merge in changes from libc.
Jim Meyering <jim@meyering.net>
parents:
4016
diff
changeset
|
544 #ifdef _LIBC |
ad164b161300
Merge in changes from libc.
Jim Meyering <jim@meyering.net>
parents:
4016
diff
changeset
|
545 libc_hidden_def (mktime) |
ad164b161300
Merge in changes from libc.
Jim Meyering <jim@meyering.net>
parents:
4016
diff
changeset
|
546 libc_hidden_weak (timelocal) |
ad164b161300
Merge in changes from libc.
Jim Meyering <jim@meyering.net>
parents:
4016
diff
changeset
|
547 #endif |
187 | 548 |
18304
e04e8b486f70
mktime: simplify DEBUG_MKTIME
Paul Eggert <eggert@cs.ucla.edu>
parents:
18303
diff
changeset
|
549 #if DEBUG_MKTIME |
508 | 550 |
551 static int | |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
552 not_equal_tm (const struct tm *a, const struct tm *b) |
508 | 553 { |
554 return ((a->tm_sec ^ b->tm_sec) | |
16877 | 555 | (a->tm_min ^ b->tm_min) |
556 | (a->tm_hour ^ b->tm_hour) | |
557 | (a->tm_mday ^ b->tm_mday) | |
558 | (a->tm_mon ^ b->tm_mon) | |
559 | (a->tm_year ^ b->tm_year) | |
560 | (a->tm_yday ^ b->tm_yday) | |
16881
122207a2ef3a
* lib/mktime.c: Revert autoupdate, which fetched the wrong version.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16879
diff
changeset
|
561 | isdst_differ (a->tm_isdst, b->tm_isdst)); |
508 | 562 } |
563 | |
564 static void | |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
565 print_tm (const struct tm *tp) |
508 | 566 { |
1109 | 567 if (tp) |
568 printf ("%04d-%02d-%02d %02d:%02d:%02d yday %03d wday %d isdst %d", | |
16877 | 569 tp->tm_year + TM_YEAR_BASE, tp->tm_mon + 1, tp->tm_mday, |
570 tp->tm_hour, tp->tm_min, tp->tm_sec, | |
571 tp->tm_yday, tp->tm_wday, tp->tm_isdst); | |
1109 | 572 else |
573 printf ("0"); | |
508 | 574 } |
575 | |
576 static int | |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
577 check_result (time_t tk, struct tm tmk, time_t tl, const struct tm *lt) |
508 | 578 { |
1109 | 579 if (tk != tl || !lt || not_equal_tm (&tmk, lt)) |
508 | 580 { |
581 printf ("mktime ("); | |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
582 print_tm (lt); |
508 | 583 printf (")\nyields ("); |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
584 print_tm (&tmk); |
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
585 printf (") == %ld, should be %ld\n", (long int) tk, (long int) tl); |
508 | 586 return 1; |
587 } | |
588 | |
589 return 0; | |
590 } | |
591 | |
592 int | |
4354
7b5fd4dc4f90
Assume freestanding C89 or better.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4352
diff
changeset
|
593 main (int argc, char **argv) |
187 | 594 { |
508 | 595 int status = 0; |
596 struct tm tm, tmk, tml; | |
1109 | 597 struct tm *lt; |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
598 time_t tk, tl, tl1; |
508 | 599 char trailer; |
187 | 600 |
18306
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
601 /* Sanity check, plus call tzset. */ |
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
602 tl = 0; |
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
603 if (! localtime (&tl)) |
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
604 { |
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
605 printf ("localtime (0) fails\n"); |
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
606 status = 1; |
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
607 } |
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
608 |
508 | 609 if ((argc == 3 || argc == 4) |
610 && (sscanf (argv[1], "%d-%d-%d%c", | |
16877 | 611 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &trailer) |
612 == 3) | |
508 | 613 && (sscanf (argv[2], "%d:%d:%d%c", |
16877 | 614 &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &trailer) |
615 == 3)) | |
508 | 616 { |
617 tm.tm_year -= TM_YEAR_BASE; | |
618 tm.tm_mon--; | |
619 tm.tm_isdst = argc == 3 ? -1 : atoi (argv[3]); | |
620 tmk = tm; | |
621 tl = mktime (&tmk); | |
18306
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
622 lt = localtime_r (&tl, &tml); |
4417
5cf298f2f367
Fix some boundary cases and remove need for floating point.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4354
diff
changeset
|
623 printf ("mktime returns %ld == ", (long int) tl); |
508 | 624 print_tm (&tmk); |
625 printf ("\n"); | |
1109 | 626 status = check_result (tl, tmk, tl, lt); |
508 | 627 } |
628 else if (argc == 4 || (argc == 5 && strcmp (argv[4], "-") == 0)) | |
629 { | |
630 time_t from = atol (argv[1]); | |
631 time_t by = atol (argv[2]); | |
632 time_t to = atol (argv[3]); | |
187 | 633 |
508 | 634 if (argc == 4) |
16877 | 635 for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1) |
636 { | |
18306
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
637 lt = localtime_r (&tl, &tml); |
16877 | 638 if (lt) |
639 { | |
18306
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
640 tmk = tml; |
16877 | 641 tk = mktime (&tmk); |
642 status |= check_result (tk, tmk, tl, &tml); | |
643 } | |
644 else | |
645 { | |
18306
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
646 printf ("localtime_r (%ld) yields 0\n", (long int) tl); |
16877 | 647 status = 1; |
648 } | |
649 tl1 = tl + by; | |
650 if ((tl1 < tl) != (by < 0)) | |
651 break; | |
652 } | |
508 | 653 else |
16877 | 654 for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1) |
655 { | |
656 /* Null benchmark. */ | |
18306
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
657 lt = localtime_r (&tl, &tml); |
16877 | 658 if (lt) |
659 { | |
18306
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
660 tmk = tml; |
16877 | 661 tk = tl; |
662 status |= check_result (tk, tmk, tl, &tml); | |
663 } | |
664 else | |
665 { | |
18306
6998697a3e42
mktime: speed up DEBUG_MKTIME benchmarks
Paul Eggert <eggert@cs.ucla.edu>
parents:
18305
diff
changeset
|
666 printf ("localtime_r (%ld) yields 0\n", (long int) tl); |
16877 | 667 status = 1; |
668 } | |
669 tl1 = tl + by; | |
670 if ((tl1 < tl) != (by < 0)) | |
671 break; | |
672 } | |
508 | 673 } |
674 else | |
675 printf ("Usage:\ | |
676 \t%s YYYY-MM-DD HH:MM:SS [ISDST] # Test given time.\n\ | |
677 \t%s FROM BY TO # Test values FROM, FROM+BY, ..., TO.\n\ | |
678 \t%s FROM BY TO - # Do not test those values (for benchmark).\n", | |
16877 | 679 argv[0], argv[0], argv[0]); |
187 | 680 |
508 | 681 return status; |
682 } | |
295 | 683 |
18198
004a3263ca0e
mktime: rename macro to avoid glibc clash
Martin Sebor <msebor@redhat.com>
parents:
18189
diff
changeset
|
684 #endif /* DEBUG_MKTIME */ |
295 | 685 |
686 /* | |
687 Local Variables: | |
18198
004a3263ca0e
mktime: rename macro to avoid glibc clash
Martin Sebor <msebor@redhat.com>
parents:
18189
diff
changeset
|
688 compile-command: "gcc -DDEBUG_MKTIME -I. -Wall -W -O2 -g mktime.c -o mktime" |
295 | 689 End: |
690 */ |