Merge from vendor branch ZLIB:
[dragonfly.git] / sys / boot / ia64 / libski / time.c
1 /*
2  * Copyright (c) 1999, 2000
3  * Intel Corporation.
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  * 
20  *    This product includes software developed by Intel Corporation and
21  *    its contributors.
22  * 
23  * 4. Neither the name of Intel Corporation or its contributors may be
24  *    used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  * 
27  * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
37  * THE POSSIBILITY OF SUCH DAMAGE.
38  *
39  * $FreeBSD: src/sys/boot/ia64/libski/time.c,v 1.4 2003/09/08 09:11:32 obrien Exp $
40  * $DragonFly: src/sys/boot/ia64/libski/time.c,v 1.1 2003/11/10 06:08:37 dillon Exp $
41  */
42
43 #include <time.h>
44 #include <sys/time.h>
45 #include <stand.h>
46
47 #include "libski.h"
48
49 /*
50 // Accurate only for the past couple of centuries;
51 // that will probably do.
52 //
53 // (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)
54 */
55
56 #define isleap(y)       (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
57 #define SECSPERHOUR ( 60*60 )
58 #define SECSPERDAY      (24 * SECSPERHOUR)
59
60 struct ssc_time {
61         int     Year;
62         int     Month;
63         int     Day;
64         int     Hour;
65         int     Minute;
66         int     Second;
67         int     Msec;
68         int     Wday;
69 };
70
71 time_t
72 EfiTimeToUnixTime(struct ssc_time *ETime)
73 {
74     /*
75     //  These arrays give the cumulative number of days up to the first of the
76     //  month number used as the index (1 -> 12) for regular and leap years.
77     //  The value at index 13 is for the whole year.
78     */
79     static time_t CumulativeDays[2][14] = {
80     {0,
81      0,
82      31,
83      31 + 28,
84      31 + 28 + 31,
85      31 + 28 + 31 + 30,
86      31 + 28 + 31 + 30 + 31,
87      31 + 28 + 31 + 30 + 31 + 30,
88      31 + 28 + 31 + 30 + 31 + 30 + 31,
89      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
90      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
91      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
92      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
93      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 },
94     {0,
95      0,
96      31,
97      31 + 29,
98      31 + 29 + 31,
99      31 + 29 + 31 + 30,
100      31 + 29 + 31 + 30 + 31,
101      31 + 29 + 31 + 30 + 31 + 30,
102      31 + 29 + 31 + 30 + 31 + 30 + 31,
103      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
104      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
105      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
106      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
107      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};
108
109     time_t  UTime; 
110     int     Year;
111
112     ETime->Year += 1900;
113
114     /*
115     //  Do a santity check
116     */
117     if ( ETime->Year  <  1998 || ETime->Year   > 2099 ||
118          ETime->Month ==    0 || ETime->Month  >   12 ||
119          ETime->Day   ==    0 || ETime->Month  >   31 ||
120                                  ETime->Hour   >   23 ||
121                                  ETime->Minute >   59 ||
122                                  ETime->Second >   59 ) {
123         return (0);
124     }
125
126     /*
127     // Years
128     */
129     UTime = 0;
130     for (Year = 1970; Year != ETime->Year; ++Year) {
131         UTime += (CumulativeDays[isleap(Year)][13] * SECSPERDAY);
132     }
133
134     /*
135     // UTime should now be set to 00:00:00 on Jan 1 of the file's year.
136     //
137     // Months  
138     */
139     UTime += (CumulativeDays[isleap(ETime->Year)][ETime->Month] * SECSPERDAY);
140
141     /*
142     // UTime should now be set to 00:00:00 on the first of the file's month and year
143     //
144     // Days -- Don't count the file's day
145     */
146     UTime += (((ETime->Day > 0) ? ETime->Day-1:0) * SECSPERDAY);
147
148     /*
149     // Hours
150     */
151     UTime += (ETime->Hour * SECSPERHOUR);
152
153     /*
154     // Minutes
155     */
156     UTime += (ETime->Minute * 60);
157
158     /*
159     // Seconds
160     */
161     UTime += ETime->Second;
162
163     return UTime;
164 }
165
166 time_t
167 time(time_t *tloc)
168 {
169         struct ssc_time time;
170
171         ssc((u_int64_t) &time, 0, 0, 0, SSC_GET_RTC);
172
173         return *tloc = EfiTimeToUnixTime(&time);
174 }