time.c revision 1.1.2.2 1 1.1.2.2 yamt /* $NetBSD: time.c,v 1.1.2.2 2006/04/11 11:53:42 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.2 yamt
45 1.1.2.2 yamt #include <efi.h>
46 1.1.2.2 yamt #include <efilib.h>
47 1.1.2.2 yamt
48 1.1.2.2 yamt #include <sys/time.h>
49 1.1.2.2 yamt
50 1.1.2.2 yamt /*
51 1.1.2.2 yamt // Accurate only for the past couple of centuries;
52 1.1.2.2 yamt // that will probably do.
53 1.1.2.2 yamt //
54 1.1.2.2 yamt // (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)
55 1.1.2.2 yamt */
56 1.1.2.2 yamt
57 1.1.2.2 yamt #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
58 1.1.2.2 yamt #define SECSPERHOUR ( 60*60 )
59 1.1.2.2 yamt #define SECSPERDAY (24 * SECSPERHOUR)
60 1.1.2.2 yamt
61 1.1.2.2 yamt time_t
62 1.1.2.2 yamt EfiTimeToUnixTime(EFI_TIME *ETime)
63 1.1.2.2 yamt {
64 1.1.2.2 yamt /*
65 1.1.2.2 yamt // These arrays give the cumulative number of days up to the first of the
66 1.1.2.2 yamt // month number used as the index (1 -> 12) for regular and leap years.
67 1.1.2.2 yamt // The value at index 13 is for the whole year.
68 1.1.2.2 yamt */
69 1.1.2.2 yamt static time_t CumulativeDays[2][14] = {
70 1.1.2.2 yamt {0,
71 1.1.2.2 yamt 0,
72 1.1.2.2 yamt 31,
73 1.1.2.2 yamt 31 + 28,
74 1.1.2.2 yamt 31 + 28 + 31,
75 1.1.2.2 yamt 31 + 28 + 31 + 30,
76 1.1.2.2 yamt 31 + 28 + 31 + 30 + 31,
77 1.1.2.2 yamt 31 + 28 + 31 + 30 + 31 + 30,
78 1.1.2.2 yamt 31 + 28 + 31 + 30 + 31 + 30 + 31,
79 1.1.2.2 yamt 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
80 1.1.2.2 yamt 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
81 1.1.2.2 yamt 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
82 1.1.2.2 yamt 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
83 1.1.2.2 yamt 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 },
84 1.1.2.2 yamt {0,
85 1.1.2.2 yamt 0,
86 1.1.2.2 yamt 31,
87 1.1.2.2 yamt 31 + 29,
88 1.1.2.2 yamt 31 + 29 + 31,
89 1.1.2.2 yamt 31 + 29 + 31 + 30,
90 1.1.2.2 yamt 31 + 29 + 31 + 30 + 31,
91 1.1.2.2 yamt 31 + 29 + 31 + 30 + 31 + 30,
92 1.1.2.2 yamt 31 + 29 + 31 + 30 + 31 + 30 + 31,
93 1.1.2.2 yamt 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
94 1.1.2.2 yamt 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
95 1.1.2.2 yamt 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
96 1.1.2.2 yamt 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
97 1.1.2.2 yamt 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};
98 1.1.2.2 yamt
99 1.1.2.2 yamt time_t UTime;
100 1.1.2.2 yamt int Year;
101 1.1.2.2 yamt
102 1.1.2.2 yamt /*
103 1.1.2.2 yamt // Do a santity check
104 1.1.2.2 yamt */
105 1.1.2.2 yamt if ( ETime->Year < 1998 || ETime->Year > 2099 ||
106 1.1.2.2 yamt ETime->Month == 0 || ETime->Month > 12 ||
107 1.1.2.2 yamt ETime->Day == 0 || ETime->Month > 31 ||
108 1.1.2.2 yamt ETime->Hour > 23 ||
109 1.1.2.2 yamt ETime->Minute > 59 ||
110 1.1.2.2 yamt ETime->Second > 59 ||
111 1.1.2.2 yamt ETime->TimeZone < -1440 ||
112 1.1.2.2 yamt (ETime->TimeZone > 1440 && ETime->TimeZone != 2047) ) {
113 1.1.2.2 yamt return (0);
114 1.1.2.2 yamt }
115 1.1.2.2 yamt
116 1.1.2.2 yamt /*
117 1.1.2.2 yamt // Years
118 1.1.2.2 yamt */
119 1.1.2.2 yamt UTime = 0;
120 1.1.2.2 yamt for (Year = 1970; Year != ETime->Year; ++Year) {
121 1.1.2.2 yamt UTime += (CumulativeDays[isleap(Year)][13] * SECSPERDAY);
122 1.1.2.2 yamt }
123 1.1.2.2 yamt
124 1.1.2.2 yamt /*
125 1.1.2.2 yamt // UTime should now be set to 00:00:00 on Jan 1 of the file's year.
126 1.1.2.2 yamt //
127 1.1.2.2 yamt // Months
128 1.1.2.2 yamt */
129 1.1.2.2 yamt UTime += (CumulativeDays[isleap(ETime->Year)][ETime->Month] * SECSPERDAY);
130 1.1.2.2 yamt
131 1.1.2.2 yamt /*
132 1.1.2.2 yamt // UTime should now be set to 00:00:00 on the first of the file's month and year
133 1.1.2.2 yamt //
134 1.1.2.2 yamt // Days -- Don't count the file's day
135 1.1.2.2 yamt */
136 1.1.2.2 yamt UTime += (((ETime->Day > 0) ? ETime->Day-1:0) * SECSPERDAY);
137 1.1.2.2 yamt
138 1.1.2.2 yamt /*
139 1.1.2.2 yamt // Hours
140 1.1.2.2 yamt */
141 1.1.2.2 yamt UTime += (ETime->Hour * SECSPERHOUR);
142 1.1.2.2 yamt
143 1.1.2.2 yamt /*
144 1.1.2.2 yamt // Minutes
145 1.1.2.2 yamt */
146 1.1.2.2 yamt UTime += (ETime->Minute * 60);
147 1.1.2.2 yamt
148 1.1.2.2 yamt /*
149 1.1.2.2 yamt // Seconds
150 1.1.2.2 yamt */
151 1.1.2.2 yamt UTime += ETime->Second;
152 1.1.2.2 yamt
153 1.1.2.2 yamt /*
154 1.1.2.2 yamt // EFI time is repored in local time. Adjust for any time zone offset to
155 1.1.2.2 yamt // get true UT
156 1.1.2.2 yamt */
157 1.1.2.2 yamt if ( ETime->TimeZone != EFI_UNSPECIFIED_TIMEZONE ) {
158 1.1.2.2 yamt /*
159 1.1.2.2 yamt // TimeZone is kept in minues...
160 1.1.2.2 yamt */
161 1.1.2.2 yamt UTime += (ETime->TimeZone * 60);
162 1.1.2.2 yamt }
163 1.1.2.2 yamt
164 1.1.2.2 yamt return UTime;
165 1.1.2.2 yamt }
166 1.1.2.2 yamt
167 1.1.2.2 yamt int
168 1.1.2.2 yamt EFI_GetTimeOfDay(
169 1.1.2.2 yamt OUT struct timeval *tp,
170 1.1.2.2 yamt OUT struct timezone *tzp
171 1.1.2.2 yamt )
172 1.1.2.2 yamt {
173 1.1.2.2 yamt EFI_TIME EfiTime;
174 1.1.2.2 yamt EFI_TIME_CAPABILITIES Capabilities;
175 1.1.2.2 yamt EFI_STATUS Status;
176 1.1.2.2 yamt
177 1.1.2.2 yamt /*
178 1.1.2.2 yamt // Get time from EFI
179 1.1.2.2 yamt */
180 1.1.2.2 yamt
181 1.1.2.2 yamt Status = RS->GetTime( &EfiTime, &Capabilities );
182 1.1.2.2 yamt if (EFI_ERROR(Status))
183 1.1.2.2 yamt return (-1);
184 1.1.2.2 yamt
185 1.1.2.2 yamt /*
186 1.1.2.2 yamt // Convert to UNIX time (ie seconds since the epoch
187 1.1.2.2 yamt */
188 1.1.2.2 yamt
189 1.1.2.2 yamt tp->tv_sec = EfiTimeToUnixTime( &EfiTime );
190 1.1.2.2 yamt tp->tv_usec = 0; /* EfiTime.Nanosecond * 1000; */
191 1.1.2.2 yamt
192 1.1.2.2 yamt /*
193 1.1.2.2 yamt // Do something with the timezone if needed
194 1.1.2.2 yamt */
195 1.1.2.2 yamt
196 1.1.2.2 yamt if (tzp) {
197 1.1.2.2 yamt tzp->tz_minuteswest =
198 1.1.2.2 yamt EfiTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE ? 0 : EfiTime.TimeZone;
199 1.1.2.2 yamt /*
200 1.1.2.2 yamt // This isn't quit right since it doesn't deal with EFI_TIME_IN_DAYLIGHT
201 1.1.2.2 yamt */
202 1.1.2.2 yamt tzp->tz_dsttime =
203 1.1.2.2 yamt EfiTime.Daylight & EFI_TIME_ADJUST_DAYLIGHT ? 1 : 0;
204 1.1.2.2 yamt }
205 1.1.2.2 yamt
206 1.1.2.2 yamt return (0);
207 1.1.2.2 yamt }
208 1.1.2.2 yamt
209 1.1.2.2 yamt time_t
210 1.1.2.2 yamt time(time_t *tloc)
211 1.1.2.2 yamt {
212 1.1.2.2 yamt struct timeval tv;
213 1.1.2.2 yamt EFI_GetTimeOfDay(&tv, 0);
214 1.1.2.2 yamt
215 1.1.2.2 yamt if (tloc)
216 1.1.2.2 yamt *tloc = tv.tv_sec;
217 1.1.2.2 yamt return tv.tv_sec;
218 1.1.2.2 yamt }
219 1.1.2.2 yamt
220 1.1.2.2 yamt time_t
221 1.1.2.2 yamt getsecs()
222 1.1.2.2 yamt {
223 1.1.2.2 yamt return time(0);
224 1.1.2.2 yamt }
225