-
Notifications
You must be signed in to change notification settings - Fork 0
/
mktime.c.diff
418 lines (404 loc) · 12.4 KB
/
mktime.c.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
*** ./mktime.c.orig Tue Jun 16 23:00:00 1998
--- ./mktime.c Thu Feb 01 23:36:23 2001
***************
*** 1,298 ****
! /***
! *mktime.c - Convert struct tm value to time_t value.
! *
! * Copyright (c) 1987-1997, Microsoft Corporation. All rights reserved.
! *
! *Purpose:
! * Defines mktime() and _mkgmtime(), routines to converts a time value
! * in a tm structure (possibly incomplete) into a time_t value, then
! * update (all) the structure fields with "normalized" values.
! *
! *******************************************************************************/
!
! #include <cruntime.h>
! #include <stddef.h>
! #include <ctime.h>
#include <time.h>
- #include <internal.h>
- /*
- * ChkAdd evaluates to TRUE if dest = src1 + src2 has overflowed
- */
#define ChkAdd(dest, src1, src2) ( ((src1 >= 0L) && (src2 >= 0L) \
&& (dest < 0L)) || ((src1 < 0L) && (src2 < 0L) && (dest >= 0L)) )
- /*
- * ChkMul evaluates to TRUE if dest = src1 * src2 has overflowed
- */
#define ChkMul(dest, src1, src2) ( src1 ? (dest/src1 != src2) : 0 )
! /*
! * Core function for both mktime() and _mkgmtime()
! */
! static time_t __cdecl _make_time_t( struct tm *, int);
!
!
! /***
! *time_t mktime(tb) - Normalize user time block structure
! *
! *Purpose:
! * Mktime converts a time structure, passed in as an argument, into a
! * calendar time value in internal format (time_t). It also completes
! * and updates the fields the of the passed in structure with 'normalized'
! * values. There are three practical uses for this routine:
! *
! * (1) Convert broken-down time to internal time format (time_t).
! * (2) To have mktime fill in the tm_wday, tm_yday, or tm_isdst fields.
! * (3) To pass in a time structure with 'out of range' values for some
! * fields and have mktime "normalize" them (e.g., pass in 1/35/87 and
! * get back 2/4/87).
! *Entry:
! * struct tm *tb - pointer to a tm time structure to convert and
! * normalize
! *
! *Exit:
! * If successful, mktime returns the specified calender time encoded as
! * a time_t value. Otherwise, (time_t)(-1) is returned to indicate an
! * error.
! *
! *Exceptions:
! * None.
! *
! *******************************************************************************/
!
!
! time_t __cdecl mktime (
! struct tm *tb
! )
{
! return( _make_time_t(tb, 1) );
}
!
! /***
! *time_t _mkgmtime(tb) - Convert broken down UTC time to time_t
! *
! *Purpose:
! * Convert a tm structure, passed in as an argument, containing a UTC
! * time value to internal format (time_t). It also completes and updates
! * the fields the of the passed in structure with 'normalized' values.
!
! *Entry:
! * struct tm *tb - pointer to a tm time structure to convert and
! * normalize
! *
! *Exit:
! * If successful, _mkgmtime returns the calender time encoded as time_t
! * Otherwise, (time_t)(-1) is returned to indicate an error.
! *
! *Exceptions:
! * None.
! *
! *******************************************************************************/
!
! time_t __cdecl _mkgmtime (
! struct tm *tb
! )
{
! return( _make_time_t(tb, 0) );
}
!
! /***
! *static time_t make_time_t(tb, ultflag) -
! *
! *Purpose:
! * Converts a struct tm value to a time_t value, then updates the struct
! * tm value. Either local time or UTC is supported, based on ultflag.
! * This is the routine that actually does the work for both mktime() and
! * _mkgmtime().
! *
! *Entry:
! * struct tm *tb - pointer to a tm time structure to convert and
! * normalize
! * int ultflag - use local time flag. the tb structure is assumed
! * to represent a local date/time if ultflag > 0.
! * otherwise, UTC is assumed.
! *
! *Exit:
! * If successful, mktime returns the specified calender time encoded as
! * a time_t value. Otherwise, (time_t)(-1) is returned to indicate an
! * error.
! *
! *Exceptions:
! * None.
! *
! *******************************************************************************/
!
! static time_t __cdecl _make_time_t (
! struct tm *tb,
! int ultflag
! )
{
! long tmptm1, tmptm2, tmptm3;
! struct tm *tbtemp;
! /*
! * First, make sure tm_year is reasonably close to being in range.
! */
! if ( ((tmptm1 = tb->tm_year) < _BASE_YEAR - 1) || (tmptm1 > _MAX_YEAR
! + 1) )
! goto err_mktime;
!
!
! /*
! * Adjust month value so it is in the range 0 - 11. This is because
! * we don't know how many days are in months 12, 13, 14, etc.
! */
!
! if ( (tb->tm_mon < 0) || (tb->tm_mon > 11) ) {
!
! /*
! * no danger of overflow because the range check above.
! */
! tmptm1 += (tb->tm_mon / 12);
!
! if ( (tb->tm_mon %= 12) < 0 ) {
! tb->tm_mon += 12;
! tmptm1--;
! }
!
! /*
! * Make sure year count is still in range.
! */
! if ( (tmptm1 < _BASE_YEAR - 1) || (tmptm1 > _MAX_YEAR + 1) )
! goto err_mktime;
! }
!
! /***** HERE: tmptm1 holds number of elapsed years *****/
!
! /*
! * Calculate days elapsed minus one, in the given year, to the given
! * month. Check for leap year and adjust if necessary.
! */
! tmptm2 = _days[tb->tm_mon];
! if ( !(tmptm1 & 3) && (tb->tm_mon > 1) )
! tmptm2++;
!
! /*
! * Calculate elapsed days since base date (midnight, 1/1/70, UTC)
! *
! *
! * 365 days for each elapsed year since 1970, plus one more day for
! * each elapsed leap year. no danger of overflow because of the range
! * check (above) on tmptm1.
! */
! tmptm3 = (tmptm1 - _BASE_YEAR) * 365L + ((tmptm1 - 1L) >> 2)
! - _LEAP_YEAR_ADJUST;
!
! /*
! * elapsed days to current month (still no possible overflow)
! */
! tmptm3 += tmptm2;
!
! /*
! * elapsed days to current date. overflow is now possible.
! */
! tmptm1 = tmptm3 + (tmptm2 = (long)(tb->tm_mday));
! if ( ChkAdd(tmptm1, tmptm3, tmptm2) )
! goto err_mktime;
!
! /***** HERE: tmptm1 holds number of elapsed days *****/
!
! /*
! * Calculate elapsed hours since base date
! */
! tmptm2 = tmptm1 * 24L;
! if ( ChkMul(tmptm2, tmptm1, 24L) )
! goto err_mktime;
!
! tmptm1 = tmptm2 + (tmptm3 = (long)tb->tm_hour);
! if ( ChkAdd(tmptm1, tmptm2, tmptm3) )
! goto err_mktime;
!
! /***** HERE: tmptm1 holds number of elapsed hours *****/
!
! /*
! * Calculate elapsed minutes since base date
! */
!
! tmptm2 = tmptm1 * 60L;
! if ( ChkMul(tmptm2, tmptm1, 60L) )
! goto err_mktime;
!
! tmptm1 = tmptm2 + (tmptm3 = (long)tb->tm_min);
! if ( ChkAdd(tmptm1, tmptm2, tmptm3) )
! goto err_mktime;
!
! /***** HERE: tmptm1 holds number of elapsed minutes *****/
!
! /*
! * Calculate elapsed seconds since base date
! */
!
! tmptm2 = tmptm1 * 60L;
! if ( ChkMul(tmptm2, tmptm1, 60L) )
! goto err_mktime;
!
! tmptm1 = tmptm2 + (tmptm3 = (long)tb->tm_sec);
! if ( ChkAdd(tmptm1, tmptm2, tmptm3) )
! goto err_mktime;
!
! /***** HERE: tmptm1 holds number of elapsed seconds *****/
!
! if ( ultflag ) {
!
! /*
! * Adjust for timezone. No need to check for overflow since
! * localtime() will check its arg value
! */
!
! #ifdef _WIN32
! __tzset();
! #else /* _WIN32 */
! #if defined (_M_MPPC) || defined (_M_M68K)
! _tzset();
! #endif /* defined (_M_MPPC) || defined (_M_M68K) */
! #endif /* _WIN32 */
!
! tmptm1 += _timezone;
!
! /*
! * Convert this second count back into a time block structure.
! * If localtime returns NULL, return an error.
! */
! if ( (tbtemp = localtime(&tmptm1)) == NULL )
! goto err_mktime;
!
! /*
! * Now must compensate for DST. The ANSI rules are to use the
! * passed-in tm_isdst flag if it is non-negative. Otherwise,
! * compute if DST applies. Recall that tbtemp has the time without
! * DST compensation, but has set tm_isdst correctly.
! */
! if ( (tb->tm_isdst > 0) || ((tb->tm_isdst < 0) &&
! (tbtemp->tm_isdst > 0)) ) {
! tmptm1 += _dstbias;
! tbtemp = localtime(&tmptm1); /* reconvert, can't get NULL */
! }
!
! }
! else {
! if ( (tbtemp = gmtime(&tmptm1)) == NULL )
! goto err_mktime;
! }
!
! /***** HERE: tmptm1 holds number of elapsed seconds, adjusted *****/
! /***** for local time if requested *****/
!
! *tb = *tbtemp;
! return (time_t)tmptm1;
!
! err_mktime:
! /*
! * All errors come to here
! */
! return (time_t)(-1);
}
--- 1,115 ----
! // mktime.c - adapted from Microsoft C Runtime
! //
! // Time-stamp: <01/02/01 21:30:19 keuchel@lightning>
!
! #include "celib.h"
#include <time.h>
#define ChkAdd(dest, src1, src2) ( ((src1 >= 0L) && (src2 >= 0L) \
&& (dest < 0L)) || ((src1 < 0L) && (src2 < 0L) && (dest >= 0L)) )
#define ChkMul(dest, src1, src2) ( src1 ? (dest/src1 != src2) : 0 )
+ static time_t _make_time_t( struct tm *, int);
! time_t
! mktime (struct tm *tb)
{
! return( _make_time_t(tb, 1) );
}
! time_t
! _mkgmtime (struct tm *tb)
{
! return( _make_time_t(tb, 0) );
}
! static time_t
! _make_time_t (struct tm *tb, int ultflag)
{
! long tmptm1, tmptm2, tmptm3;
! struct tm *tbtemp;
!
! if ( ((tmptm1 = tb->tm_year) < _BASE_YEAR - 1) || (tmptm1 > _MAX_YEAR + 1) )
! goto err_mktime;
!
! if ( (tb->tm_mon < 0) || (tb->tm_mon > 11) )
! {
! tmptm1 += (tb->tm_mon / 12);
!
! if ( (tb->tm_mon %= 12) < 0 )
! {
! tb->tm_mon += 12;
! tmptm1--;
! }
!
! if ( (tmptm1 < _BASE_YEAR - 1) || (tmptm1 > _MAX_YEAR + 1) )
! goto err_mktime;
! }
!
! tmptm2 = _days[tb->tm_mon];
!
! if ( !(tmptm1 & 3) && (tb->tm_mon > 1) )
! tmptm2++;
!
! tmptm3 = (tmptm1 - _BASE_YEAR) * 365L + ((tmptm1 - 1L) >> 2)
! - _LEAP_YEAR_ADJUST;
!
! tmptm3 += tmptm2;
!
! tmptm1 = tmptm3 + (tmptm2 = (long)(tb->tm_mday));
! if ( ChkAdd(tmptm1, tmptm3, tmptm2) )
! goto err_mktime;
!
! tmptm2 = tmptm1 * 24L;
! if ( ChkMul(tmptm2, tmptm1, 24L) )
! goto err_mktime;
!
! tmptm1 = tmptm2 + (tmptm3 = (long)tb->tm_hour);
! if ( ChkAdd(tmptm1, tmptm2, tmptm3) )
! goto err_mktime;
!
! tmptm2 = tmptm1 * 60L;
! if ( ChkMul(tmptm2, tmptm1, 60L) )
! goto err_mktime;
!
! tmptm1 = tmptm2 + (tmptm3 = (long)tb->tm_min);
! if ( ChkAdd(tmptm1, tmptm2, tmptm3) )
! goto err_mktime;
!
! tmptm2 = tmptm1 * 60L;
! if ( ChkMul(tmptm2, tmptm1, 60L) )
! goto err_mktime;
!
! tmptm1 = tmptm2 + (tmptm3 = (long)tb->tm_sec);
! if ( ChkAdd(tmptm1, tmptm2, tmptm3) )
! goto err_mktime;
!
! if ( ultflag )
! {
! __tzset();
!
! tmptm1 += _timezone;
!
! if ( (tbtemp = localtime(&tmptm1)) == NULL )
! goto err_mktime;
!
! if ( (tb->tm_isdst > 0) || ((tb->tm_isdst < 0) &&
! (tbtemp->tm_isdst > 0)) )
! {
! tmptm1 += _dstbias;
! tbtemp = localtime(&tmptm1);
! }
! }
! else
! {
! if ( (tbtemp = gmtime(&tmptm1)) == NULL )
! goto err_mktime;
! }
!
! *tb = *tbtemp;
! return (time_t)tmptm1;
! err_mktime:
! return (time_t)(-1);
}