time.c revision 1.1.4.3 1 1.1.4.3 elad /* $NetBSD: time.c,v 1.1.4.3 2006/05/11 23:26:48 elad Exp $ */
2 1.1.4.2 elad
3 1.1.4.2 elad /*-
4 1.1.4.2 elad * Copyright (c) 1999, 2000
5 1.1.4.2 elad * Intel Corporation.
6 1.1.4.2 elad * All rights reserved.
7 1.1.4.2 elad *
8 1.1.4.2 elad * Redistribution and use in source and binary forms, with or without
9 1.1.4.2 elad * modification, are permitted provided that the following conditions
10 1.1.4.2 elad * are met:
11 1.1.4.2 elad *
12 1.1.4.2 elad * 1. Redistributions of source code must retain the above copyright
13 1.1.4.2 elad * notice, this list of conditions and the following disclaimer.
14 1.1.4.2 elad *
15 1.1.4.2 elad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 elad * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 elad * documentation and/or other materials provided with the distribution.
18 1.1.4.2 elad *
19 1.1.4.2 elad * 3. All advertising materials mentioning features or use of this software
20 1.1.4.2 elad * must display the following acknowledgement:
21 1.1.4.2 elad *
22 1.1.4.2 elad * This product includes software developed by Intel Corporation and
23 1.1.4.2 elad * its contributors.
24 1.1.4.2 elad *
25 1.1.4.2 elad * 4. Neither the name of Intel Corporation or its contributors may be
26 1.1.4.2 elad * used to endorse or promote products derived from this software
27 1.1.4.2 elad * without specific prior written permission.
28 1.1.4.2 elad *
29 1.1.4.2 elad * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
30 1.1.4.2 elad * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 1.1.4.2 elad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.1.4.2 elad * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
33 1.1.4.2 elad * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 1.1.4.2 elad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 1.1.4.2 elad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 1.1.4.2 elad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 1.1.4.2 elad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 1.1.4.2 elad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
39 1.1.4.2 elad * THE POSSIBILITY OF SUCH DAMAGE.
40 1.1.4.2 elad *
41 1.1.4.2 elad */
42 1.1.4.2 elad
43 1.1.4.2 elad #include <sys/cdefs.h>
44 1.1.4.3 elad /* __FBSDID("$FreeBSD: src/sys/boot/efi/libefi/time.c,v 1.4 2003/04/03 21:36:29 obrien Exp $");
45 1.1.4.3 elad */
46 1.1.4.2 elad #include <efi.h>
47 1.1.4.2 elad #include <efilib.h>
48 1.1.4.2 elad
49 1.1.4.2 elad #include <sys/time.h>
50 1.1.4.2 elad
51 1.1.4.2 elad /*
52 1.1.4.2 elad // Accurate only for the past couple of centuries;
53 1.1.4.2 elad // that will probably do.
54 1.1.4.2 elad //
55 1.1.4.2 elad // (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)
56 1.1.4.2 elad */
57 1.1.4.2 elad
58 1.1.4.2 elad #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
59 1.1.4.2 elad #define SECSPERHOUR ( 60*60 )
60 1.1.4.2 elad #define SECSPERDAY (24 * SECSPERHOUR)
61 1.1.4.2 elad
62 1.1.4.2 elad time_t
63 1.1.4.2 elad EfiTimeToUnixTime(EFI_TIME *ETime)
64 1.1.4.2 elad {
65 1.1.4.2 elad /*
66 1.1.4.2 elad // These arrays give the cumulative number of days up to the first of the
67 1.1.4.2 elad // month number used as the index (1 -> 12) for regular and leap years.
68 1.1.4.2 elad // The value at index 13 is for the whole year.
69 1.1.4.2 elad */
70 1.1.4.2 elad static time_t CumulativeDays[2][14] = {
71 1.1.4.2 elad {0,
72 1.1.4.2 elad 0,
73 1.1.4.2 elad 31,
74 1.1.4.2 elad 31 + 28,
75 1.1.4.2 elad 31 + 28 + 31,
76 1.1.4.2 elad 31 + 28 + 31 + 30,
77 1.1.4.2 elad 31 + 28 + 31 + 30 + 31,
78 1.1.4.2 elad 31 + 28 + 31 + 30 + 31 + 30,
79 1.1.4.2 elad 31 + 28 + 31 + 30 + 31 + 30 + 31,
80 1.1.4.2 elad 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
81 1.1.4.2 elad 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
82 1.1.4.2 elad 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
83 1.1.4.2 elad 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
84 1.1.4.2 elad 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 },
85 1.1.4.2 elad {0,
86 1.1.4.2 elad 0,
87 1.1.4.2 elad 31,
88 1.1.4.2 elad 31 + 29,
89 1.1.4.2 elad 31 + 29 + 31,
90 1.1.4.2 elad 31 + 29 + 31 + 30,
91 1.1.4.2 elad 31 + 29 + 31 + 30 + 31,
92 1.1.4.2 elad 31 + 29 + 31 + 30 + 31 + 30,
93 1.1.4.2 elad 31 + 29 + 31 + 30 + 31 + 30 + 31,
94 1.1.4.2 elad 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
95 1.1.4.2 elad 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
96 1.1.4.2 elad 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
97 1.1.4.2 elad 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
98 1.1.4.2 elad 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};
99 1.1.4.2 elad
100 1.1.4.2 elad time_t UTime;
101 1.1.4.2 elad int Year;
102 1.1.4.2 elad
103 1.1.4.2 elad /*
104 1.1.4.2 elad // Do a santity check
105 1.1.4.2 elad */
106 1.1.4.2 elad if ( ETime->Year < 1998 || ETime->Year > 2099 ||
107 1.1.4.2 elad ETime->Month == 0 || ETime->Month > 12 ||
108 1.1.4.2 elad ETime->Day == 0 || ETime->Month > 31 ||
109 1.1.4.2 elad ETime->Hour > 23 ||
110 1.1.4.2 elad ETime->Minute > 59 ||
111 1.1.4.2 elad ETime->Second > 59 ||
112 1.1.4.2 elad ETime->TimeZone < -1440 ||
113 1.1.4.2 elad (ETime->TimeZone > 1440 && ETime->TimeZone != 2047) ) {
114 1.1.4.2 elad return (0);
115 1.1.4.2 elad }
116 1.1.4.2 elad
117 1.1.4.2 elad /*
118 1.1.4.2 elad // Years
119 1.1.4.2 elad */
120 1.1.4.2 elad UTime = 0;
121 1.1.4.2 elad for (Year = 1970; Year != ETime->Year; ++Year) {
122 1.1.4.2 elad UTime += (CumulativeDays[isleap(Year)][13] * SECSPERDAY);
123 1.1.4.2 elad }
124 1.1.4.2 elad
125 1.1.4.2 elad /*
126 1.1.4.2 elad // UTime should now be set to 00:00:00 on Jan 1 of the file's year.
127 1.1.4.2 elad //
128 1.1.4.2 elad // Months
129 1.1.4.2 elad */
130 1.1.4.2 elad UTime += (CumulativeDays[isleap(ETime->Year)][ETime->Month] * SECSPERDAY);
131 1.1.4.2 elad
132 1.1.4.2 elad /*
133 1.1.4.2 elad // UTime should now be set to 00:00:00 on the first of the file's month and year
134 1.1.4.2 elad //
135 1.1.4.2 elad // Days -- Don't count the file's day
136 1.1.4.2 elad */
137 1.1.4.2 elad UTime += (((ETime->Day > 0) ? ETime->Day-1:0) * SECSPERDAY);
138 1.1.4.2 elad
139 1.1.4.2 elad /*
140 1.1.4.2 elad // Hours
141 1.1.4.2 elad */
142 1.1.4.2 elad UTime += (ETime->Hour * SECSPERHOUR);
143 1.1.4.2 elad
144 1.1.4.2 elad /*
145 1.1.4.2 elad // Minutes
146 1.1.4.2 elad */
147 1.1.4.2 elad UTime += (ETime->Minute * 60);
148 1.1.4.2 elad
149 1.1.4.2 elad /*
150 1.1.4.2 elad // Seconds
151 1.1.4.2 elad */
152 1.1.4.2 elad UTime += ETime->Second;
153 1.1.4.2 elad
154 1.1.4.2 elad /*
155 1.1.4.2 elad // EFI time is repored in local time. Adjust for any time zone offset to
156 1.1.4.2 elad // get true UT
157 1.1.4.2 elad */
158 1.1.4.2 elad if ( ETime->TimeZone != EFI_UNSPECIFIED_TIMEZONE ) {
159 1.1.4.2 elad /*
160 1.1.4.2 elad // TimeZone is kept in minues...
161 1.1.4.2 elad */
162 1.1.4.2 elad UTime += (ETime->TimeZone * 60);
163 1.1.4.2 elad }
164 1.1.4.2 elad
165 1.1.4.2 elad return UTime;
166 1.1.4.2 elad }
167 1.1.4.2 elad
168 1.1.4.2 elad int
169 1.1.4.2 elad EFI_GetTimeOfDay(
170 1.1.4.2 elad OUT struct timeval *tp,
171 1.1.4.2 elad OUT struct timezone *tzp
172 1.1.4.2 elad )
173 1.1.4.2 elad {
174 1.1.4.2 elad EFI_TIME EfiTime;
175 1.1.4.2 elad EFI_TIME_CAPABILITIES Capabilities;
176 1.1.4.2 elad EFI_STATUS Status;
177 1.1.4.2 elad
178 1.1.4.2 elad /*
179 1.1.4.2 elad // Get time from EFI
180 1.1.4.2 elad */
181 1.1.4.2 elad
182 1.1.4.2 elad Status = RS->GetTime( &EfiTime, &Capabilities );
183 1.1.4.2 elad if (EFI_ERROR(Status))
184 1.1.4.2 elad return (-1);
185 1.1.4.2 elad
186 1.1.4.2 elad /*
187 1.1.4.2 elad // Convert to UNIX time (ie seconds since the epoch
188 1.1.4.2 elad */
189 1.1.4.2 elad
190 1.1.4.2 elad tp->tv_sec = EfiTimeToUnixTime( &EfiTime );
191 1.1.4.2 elad tp->tv_usec = 0; /* EfiTime.Nanosecond * 1000; */
192 1.1.4.2 elad
193 1.1.4.2 elad /*
194 1.1.4.2 elad // Do something with the timezone if needed
195 1.1.4.2 elad */
196 1.1.4.2 elad
197 1.1.4.2 elad if (tzp) {
198 1.1.4.2 elad tzp->tz_minuteswest =
199 1.1.4.2 elad EfiTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE ? 0 : EfiTime.TimeZone;
200 1.1.4.2 elad /*
201 1.1.4.2 elad // This isn't quit right since it doesn't deal with EFI_TIME_IN_DAYLIGHT
202 1.1.4.2 elad */
203 1.1.4.2 elad tzp->tz_dsttime =
204 1.1.4.2 elad EfiTime.Daylight & EFI_TIME_ADJUST_DAYLIGHT ? 1 : 0;
205 1.1.4.2 elad }
206 1.1.4.2 elad
207 1.1.4.2 elad return (0);
208 1.1.4.2 elad }
209 1.1.4.2 elad
210 1.1.4.2 elad time_t
211 1.1.4.2 elad time(time_t *tloc)
212 1.1.4.2 elad {
213 1.1.4.2 elad struct timeval tv;
214 1.1.4.2 elad EFI_GetTimeOfDay(&tv, 0);
215 1.1.4.2 elad
216 1.1.4.2 elad if (tloc)
217 1.1.4.2 elad *tloc = tv.tv_sec;
218 1.1.4.2 elad return tv.tv_sec;
219 1.1.4.2 elad }
220 1.1.4.2 elad
221 1.1.4.2 elad time_t
222 1.1.4.2 elad getsecs()
223 1.1.4.2 elad {
224 1.1.4.2 elad return time(0);
225 1.1.4.2 elad }
226