- pre4:
[davej-history.git] / fs / udf / udftime.c
blob8f46e508ffe1f52a1f54d1f93487b2f6dfd3cde6
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/02/98: ripped this from glibc source to help convert timestamps to unix time
22 * 10/04/98: added new table-based lookup after seeing how ugly the gnu code is
23 * blf 09/27/99: ripped out all the old code and inserted new table from
24 * John Brockmeyer (without leap second corrections)
25 * rewrote udf_stamp_to_time and fixed timezone accounting in
26 udf_time_to_stamp.
30 * We don't take into account leap seconds. This may be correct or incorrect.
31 * For more NIST information (especially dealing with leap seconds), see:
32 * http://www.boulder.nist.gov/timefreq/pubs/bulletin/leapsecond.htm
35 #if defined(__linux__) && defined(__KERNEL__)
36 #include <linux/types.h>
37 #include <linux/kernel.h>
38 #else
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <sys/time.h>
42 #endif
44 #include "udfdecl.h"
46 #define EPOCH_YEAR 1970
48 #ifndef __isleap
49 /* Nonzero if YEAR is a leap year (every 4 years,
50 except every 100th isn't, and every 400th is). */
51 #define __isleap(year) \
52 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
53 #endif
55 /* How many days come before each month (0-12). */
56 const unsigned short int __mon_yday[2][13] =
58 /* Normal years. */
59 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
60 /* Leap years. */
61 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
64 #define MAX_YEAR_SECONDS 69
65 #define SPD 0x15180 /*3600*24*/
66 #define SPY(y,l,s) (SPD * (365*y+l)+s)
68 time_t year_seconds[MAX_YEAR_SECONDS]= {
69 /*1970*/ SPY( 0, 0,0), SPY( 1, 0,0), SPY( 2, 0,0), SPY( 3, 1,0),
70 /*1974*/ SPY( 4, 1,0), SPY( 5, 1,0), SPY( 6, 1,0), SPY( 7, 2,0),
71 /*1978*/ SPY( 8, 2,0), SPY( 9, 2,0), SPY(10, 2,0), SPY(11, 3,0),
72 /*1982*/ SPY(12, 3,0), SPY(13, 3,0), SPY(14, 3,0), SPY(15, 4,0),
73 /*1986*/ SPY(16, 4,0), SPY(17, 4,0), SPY(18, 4,0), SPY(19, 5,0),
74 /*1990*/ SPY(20, 5,0), SPY(21, 5,0), SPY(22, 5,0), SPY(23, 6,0),
75 /*1994*/ SPY(24, 6,0), SPY(25, 6,0), SPY(26, 6,0), SPY(27, 7,0),
76 /*1998*/ SPY(28, 7,0), SPY(29, 7,0), SPY(30, 7,0), SPY(31, 8,0),
77 /*2002*/ SPY(32, 8,0), SPY(33, 8,0), SPY(34, 8,0), SPY(35, 9,0),
78 /*2006*/ SPY(36, 9,0), SPY(37, 9,0), SPY(38, 9,0), SPY(39,10,0),
79 /*2010*/ SPY(40,10,0), SPY(41,10,0), SPY(42,10,0), SPY(43,11,0),
80 /*2014*/ SPY(44,11,0), SPY(45,11,0), SPY(46,11,0), SPY(47,12,0),
81 /*2018*/ SPY(48,12,0), SPY(49,12,0), SPY(50,12,0), SPY(51,13,0),
82 /*2022*/ SPY(52,13,0), SPY(53,13,0), SPY(54,13,0), SPY(55,14,0),
83 /*2026*/ SPY(56,14,0), SPY(57,14,0), SPY(58,14,0), SPY(59,15,0),
84 /*2030*/ SPY(60,15,0), SPY(61,15,0), SPY(62,15,0), SPY(63,16,0),
85 /*2034*/ SPY(64,16,0), SPY(65,16,0), SPY(66,16,0), SPY(67,17,0),
86 /*2038*/ SPY(68,17,0)
89 #ifdef __KERNEL__
90 extern struct timezone sys_tz;
91 #endif
93 #define SECS_PER_HOUR (60 * 60)
94 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
96 time_t *
97 udf_stamp_to_time(time_t *dest, long *dest_usec, timestamp src)
99 int yday;
100 Uint8 type = src.typeAndTimezone >> 12;
101 Sint16 offset;
103 if (type == 1)
105 offset = src.typeAndTimezone << 4;
106 /* sign extent offset */
107 offset = (offset >> 4);
109 else
110 offset = 0;
112 if ((src.year < EPOCH_YEAR) ||
113 (src.year > EPOCH_YEAR+MAX_YEAR_SECONDS))
115 *dest = -1;
116 *dest_usec = -1;
117 return NULL;
119 *dest = year_seconds[src.year - EPOCH_YEAR];
120 *dest -= offset * 60;
122 yday = ((__mon_yday[__isleap (src.year)]
123 [src.month-1]) + (src.day-1));
124 *dest += ( ( (yday* 24) + src.hour ) * 60 + src.minute ) * 60 + src.second;
125 *dest_usec = src.centiseconds * 10000 + src.hundredsOfMicroseconds * 100 + src.microseconds;
126 return dest;
130 timestamp *
131 udf_time_to_stamp(timestamp *dest, time_t tv_sec, long tv_usec)
133 long int days, rem, y;
134 const unsigned short int *ip;
135 Sint16 offset;
136 #ifndef __KERNEL__
137 struct timeval tv;
138 struct timezone sys_tz;
140 gettimeofday(&tv, &sys_tz);
141 #endif
142 offset = -sys_tz.tz_minuteswest;
144 if (!dest)
145 return NULL;
147 dest->typeAndTimezone = 0x1000 | (offset & 0x0FFF);
149 tv_sec += offset * 60;
150 days = tv_sec / SECS_PER_DAY;
151 rem = tv_sec % SECS_PER_DAY;
152 dest->hour = rem / SECS_PER_HOUR;
153 rem %= SECS_PER_HOUR;
154 dest->minute = rem / 60;
155 dest->second = rem % 60;
156 y = 1970;
158 #define DIV(a,b) ((a) / (b) - ((a) % (b) < 0))
159 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
161 while (days < 0 || days >= (__isleap(y) ? 366 : 365))
163 long int yg = y + days / 365 - (days % 365 < 0);
165 /* Adjust DAYS and Y to match the guessed year. */
166 days -= ((yg - y) * 365
167 + LEAPS_THRU_END_OF (yg - 1)
168 - LEAPS_THRU_END_OF (y - 1));
169 y = yg;
171 dest->year = y;
172 ip = __mon_yday[__isleap(y)];
173 for (y = 11; days < (long int) ip[y]; --y)
174 continue;
175 days -= ip[y];
176 dest->month = y + 1;
177 dest->day = days + 1;
179 dest->centiseconds = tv_usec / 10000;
180 dest->hundredsOfMicroseconds = (tv_usec - dest->centiseconds * 10000) / 100;
181 dest->microseconds = (tv_usec - dest->centiseconds * 10000 -
182 dest->hundredsOfMicroseconds * 100);
183 return dest;
186 /* EOF */