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