1 /* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Paul Eggert (eggert@twinsun.com).
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
21 * dgb 10/2/98: ripped this from glibc source to help convert timestamps to unix time
22 * 10/4/98: added new table-based lookup after seeing how ugly the gnu code is
25 /* Assume that leap seconds are possible, unless told otherwise.
26 If the host has a `zic' command with a `-L leapsecondfilename' option,
27 then it supports leap seconds; otherwise it probably doesn't. */
28 #ifndef LEAP_SECONDS_POSSIBLE
29 #define LEAP_SECONDS_POSSIBLE 1
32 #if defined(__linux__) && defined(__KERNEL__)
33 #include <linux/types.h>
34 #include <linux/kernel.h>
37 #include <sys/types.h>
47 #define INT_MIN (~0 << (sizeof (int) * CHAR_BIT - 1))
50 #define INT_MAX (~0 - INT_MIN)
54 #define TIME_T_MIN (0 < (time_t) -1 ? (time_t) 0 \
55 : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
58 #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
61 #define TM_YEAR_BASE 1900
62 #define EPOCH_YEAR 1970
65 /* Nonzero if YEAR is a leap year (every 4 years,
66 except every 100th isn't, and every 400th is). */
67 #define __isleap(year) \
68 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
71 /* How many days come before each month (0-12). */
72 const unsigned short int __mon_yday
[2][13] =
75 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
77 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
80 time_t udf_converttime (struct ktm
*);
81 #ifndef USE_GNU_MKTIME_METHOD
83 #define MAX_YEAR_SECONDS 68
85 time_t year_seconds
[MAX_YEAR_SECONDS
]= {
87 /*1971:*/ 31554000, /*1972:*/ 63090000, /*1973:*/ 94712400,
88 /*1974:*/ 126248400, /*1975:*/ 157784400, /*1976:*/ 189320400,
89 /*1977:*/ 220942800, /*1978:*/ 252478800, /*1979:*/ 284014800,
90 /*1980:*/ 315550800, /*1981:*/ 347173200, /*1982:*/ 378709200,
91 /*1983:*/ 410245200, /*1984:*/ 441781200, /*1985:*/ 473403600,
92 /*1986:*/ 504939600, /*1987:*/ 536475600, /*1988:*/ 568011600,
93 /*1989:*/ 599634000, /*1990:*/ 631170000, /*1991:*/ 662706000,
94 /*1992:*/ 694242000, /*1993:*/ 725864400, /*1994:*/ 757400400,
95 /*1995:*/ 788936400, /*1996:*/ 820472400, /*1997:*/ 852094800,
96 /*1998:*/ 883630800, /*1999:*/ 915166800, /*2000:*/ 946702800,
97 /*2001:*/ 978325200, /*2002:*/ 1009861200, /*2003:*/ 1041397200,
98 /*2004:*/ 1072933200, /*2005:*/ 1104555600, /*2006:*/ 1136091600,
99 /*2007:*/ 1167627600, /*2008:*/ 1199163600, /*2009:*/ 1230786000,
100 /*2010:*/ 1262322000, /*2011:*/ 1293858000, /*2012:*/ 1325394000,
101 /*2013:*/ 1357016400, /*2014:*/ 1388552400, /*2015:*/ 1420088400,
102 /*2016:*/ 1451624400, /*2017:*/ 1483246800, /*2018:*/ 1514782800,
103 /*2019:*/ 1546318800, /*2020:*/ 1577854800, /*2021:*/ 1609477200,
104 /*2022:*/ 1641013200, /*2023:*/ 1672549200, /*2024:*/ 1704085200,
105 /*2025:*/ 1735707600, /*2026:*/ 1767243600, /*2027:*/ 1798779600,
106 /*2028:*/ 1830315600, /*2029:*/ 1861938000, /*2030:*/ 1893474000,
107 /*2031:*/ 1925010000, /*2032:*/ 1956546000, /*2033:*/ 1988168400,
108 /*2034:*/ 2019704400, /*2035:*/ 2051240400, /*2036:*/ 2082776400,
112 time_t udf_converttime (struct ktm
*tm
)
119 if ( (tm
->tm_year
+TM_YEAR_BASE
< EPOCH_YEAR
) ||
120 (tm
->tm_year
+TM_YEAR_BASE
> EPOCH_YEAR
+MAX_YEAR_SECONDS
) )
122 r
= year_seconds
[tm
->tm_year
-70];
124 yday
= ((__mon_yday
[__isleap (tm
->tm_year
+ TM_YEAR_BASE
)]
127 r
+= ( ( (yday
* 24) + (tm
->tm_hour
-1) ) * 60 + tm
->tm_min
) * 60 + tm
->tm_sec
;
133 extern struct timezone sys_tz
;
135 #define SECS_PER_HOUR (60 * 60)
136 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
139 udf_time_to_stamp(timestamp
*dest
, time_t tv_sec
, long tv_usec
)
141 long int days
, rem
, y
;
142 const unsigned short int *ip
;
143 int offset
= (-sys_tz
.tz_minuteswest
+ (sys_tz
.tz_dsttime
? 60 : 0));
148 dest
->typeAndTimezone
= 0x1000 | (offset
& 0x0FFF);
150 tv_sec
+= offset
* 60;
151 days
= tv_sec
/ SECS_PER_DAY
;
152 rem
= tv_sec
% SECS_PER_DAY
;
153 dest
->hour
= rem
/ SECS_PER_HOUR
;
154 rem
%= SECS_PER_HOUR
;
155 dest
->minute
= rem
/ 60;
156 dest
->second
= rem
% 60;
159 #define DIV(a,b) ((a) / (b) - ((a) % (b) < 0))
160 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
162 while (days
< 0 || days
>= (__isleap(y
) ? 366 : 365))
164 long int yg
= y
+ days
/ 365 - (days
% 365 < 0);
166 /* Adjust DAYS and Y to match the guessed year. */
167 days
-= ((yg
- y
) * 365
168 + LEAPS_THRU_END_OF (yg
- 1)
169 - LEAPS_THRU_END_OF (y
- 1));
173 ip
= __mon_yday
[__isleap(y
)];
174 for (y
= 11; days
< (long int) ip
[y
]; --y
)
178 dest
->day
= days
+ 1;
180 dest
->centiseconds
= tv_usec
/ 10000;
181 dest
->hundredsOfMicroseconds
= (tv_usec
- dest
->centiseconds
* 10000) / 100;
182 dest
->microseconds
= (tv_usec
- dest
->centiseconds
* 10000 -
183 dest
->hundredsOfMicroseconds
* 100);
190 static time_t ydhms_tm_diff (int, int, int, int, int, const struct ktm
*);
193 /* Yield the difference between (YEAR-YDAY HOUR:MIN:SEC) and (*TP),
194 measured in seconds, ignoring leap seconds.
195 YEAR uses the same numbering as TM->tm_year.
196 All values are in range, except possibly YEAR.
197 If overflow occurs, yield the low order bits of the correct answer. */
199 ydhms_tm_diff (int year
, int yday
, int hour
, int min
, int sec
, const struct ktm
*tp
)
203 /* Compute intervening leap days correctly even if year is negative.
204 Take care to avoid int overflow. time_t overflow is OK, since
205 only the low order bits of the correct time_t answer are needed.
206 Don't convert to time_t until after all divisions are done, since
207 time_t might be unsigned. */
208 int a4
= (year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (year
& 3);
209 int b4
= (tp
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (tp
->tm_year
& 3);
210 int a100
= a4
/ 25 - (a4
% 25 < 0);
211 int b100
= b4
/ 25 - (b4
% 25 < 0);
212 int a400
= a100
>> 2;
213 int b400
= b100
>> 2;
214 int intervening_leap_days
= (a4
- b4
) - (a100
- b100
) + (a400
- b400
);
215 time_t years
= year
- (time_t) tp
->tm_year
;
216 time_t days
= (365 * years
+ intervening_leap_days
);
217 result
= (60 * (60 * (24 * days
+ (hour
- tp
->tm_hour
))
218 + (min
- tp
->tm_min
))
219 + (sec
- tp
->tm_sec
));
221 printk(KERN_ERR
"udf: ydhms_tm_diff(%d,%d,%d,%d,%d,) returning %ld\n",
222 year
, yday
, hour
, min
, sec
, result
);
228 /* Convert *TP to a time_t value, inverting
229 the monotonic and mostly-unit-linear conversion function CONVERT.
230 Use *OFFSET to keep track of a guess at the offset of the result,
231 compared to what the result would be for UTC without leap seconds.
232 If *OFFSET's guess is correct, only one CONVERT call is needed. */
234 udf_converttime (struct ktm
*tp
)
239 /* The maximum number of probes (calls to CONVERT) should be enough
240 to handle any combinations of time zone rule changes, solar time,
241 and leap seconds. Posix.1 prohibits leap seconds, but some hosts
243 int remaining_probes
= 4;
245 /* Time requested. Copy it in case CONVERT modifies *TP; this can
246 occur if TP is localtime's returned value and CONVERT is localtime. */
247 int sec
= tp
->tm_sec
;
248 int min
= tp
->tm_min
;
249 int hour
= tp
->tm_hour
;
250 int mday
= tp
->tm_mday
;
251 int mon
= tp
->tm_mon
;
252 int year_requested
= tp
->tm_year
;
253 int isdst
= tp
->tm_isdst
;
255 /* Ensure that mon is in range, and set year accordingly. */
256 int mon_remainder
= mon
% 12;
257 int negative_mon_remainder
= mon_remainder
< 0;
258 int mon_years
= mon
/ 12 - negative_mon_remainder
;
259 int year
= year_requested
+ mon_years
;
261 /* The other values need not be in range:
262 the remaining code handles minor overflows correctly,
263 assuming int and time_t arithmetic wraps around.
264 Major overflows are caught at the end. */
266 /* Calculate day of year from year, month, and day of month.
267 The result need not be in range. */
268 int yday
= ((__mon_yday
[__isleap (year
+ TM_YEAR_BASE
)]
269 [mon_remainder
+ 12 * negative_mon_remainder
])
272 #if LEAP_SECONDS_POSSIBLE
273 /* Handle out-of-range seconds specially,
274 since ydhms_tm_diff assumes every minute has 60 seconds. */
275 int sec_requested
= sec
;
282 /* Invert CONVERT by probing. First assume the same offset as last time.
283 Then repeatedly use the error to improve the guess. */
285 tm
.tm_year
= EPOCH_YEAR
- TM_YEAR_BASE
;
286 tm
.tm_hour
= tm
.tm_min
= tm
.tm_sec
= 0;
288 t0 = ydhms_tm_diff (year, yday, hour, min, sec, &tm);
291 (dt = ydhms_tm_diff (year, yday, hour, min, sec, &tm));
293 if (--remaining_probes == 0)
297 /* Check whether tm.tm_isdst has the requested value, if any. */
298 if (0 <= isdst
&& 0 <= tm
.tm_isdst
)
300 int dst_diff
= (isdst
!= 0) - (tm
.tm_isdst
!= 0);
303 /* Move two hours in the direction indicated by the disagreement,
304 probe some more, and switch to a new time if found.
305 The largest known fallback due to daylight savings is two hours:
306 once, in Newfoundland, 1988-10-30 02:00 -> 00:00. */
307 time_t ot
= t
- 2 * 60 * 60 * dst_diff
;
308 while (--remaining_probes
!= 0)
311 if (! (dt
= ydhms_tm_diff (year
, yday
, hour
, min
, sec
,
319 break; /* Avoid a redundant probe. */
325 #if LEAP_SECONDS_POSSIBLE
326 if (sec_requested
!= tm
.tm_sec
)
328 /* Adjust time to reflect the tm_sec requested, not the normalized value.
329 Also, repair any damage from a false match due to a leap second. */
330 t
+= sec_requested
- sec
+ (sec
== 0 && tm
.tm_sec
== 60);
334 if (TIME_T_MAX
/ INT_MAX
/ 366 / 24 / 60 / 60 < 3)
336 /* time_t isn't large enough to rule out overflows in ydhms_tm_diff,
337 so check for major overflows. A gross check suffices,
338 since if t has overflowed, it is off by a multiple of
339 TIME_T_MAX - TIME_T_MIN + 1. So ignore any component of
340 the difference that is bounded by a small value. */
342 double dyear
= (double) year_requested
+ mon_years
- tm
.tm_year
;
343 double dday
= 366 * dyear
+ mday
;
344 double dsec
= 60 * (60 * (24 * dday
+ hour
) + min
) + sec_requested
;
346 if (TIME_T_MAX
/ 3 - TIME_T_MIN
/ 3 < (dsec
< 0 ? - dsec
: dsec
))
352 udf_debug("returning %ld\n", t
);
358 #ifdef INCLUDE_PRINT_KTM
360 print_ktm (struct ktm
*tp
)
367 "%04d-%02d-%02d %02d:%02d:%02d isdst %d",
368 tp
->tm_year
+ TM_YEAR_BASE
, tp
->tm_mon
+ 1, tp
->tm_mday
,
369 tp
->tm_hour
, tp
->tm_min
, tp
->tm_sec
, tp
->tm_isdst
);