1 /* $NetBSD: time.c,v 1.2 2006/04/22 07:58:53 cherry Exp $ */
4 * Copyright (c) 1999, 2000
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
22 * This product includes software developed by Intel Corporation and
25 * 4. Neither the name of Intel Corporation or its contributors may be
26 * used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
30 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
33 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
39 * THE POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/cdefs.h>
44 /* __FBSDID("$FreeBSD: src/sys/boot/efi/libefi/time.c,v 1.4 2003/04/03 21:36:29 obrien Exp $");
52 // Accurate only for the past couple of centuries;
53 // that will probably do.
55 // (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)
58 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
59 #define SECSPERHOUR ( 60*60 )
60 #define SECSPERDAY (24 * SECSPERHOUR)
63 EfiTimeToUnixTime(EFI_TIME
*ETime
)
66 // These arrays give the cumulative number of days up to the first of the
67 // month number used as the index (1 -> 12) for regular and leap years.
68 // The value at index 13 is for the whole year.
70 static time_t CumulativeDays
[2][14] = {
77 31 + 28 + 31 + 30 + 31,
78 31 + 28 + 31 + 30 + 31 + 30,
79 31 + 28 + 31 + 30 + 31 + 30 + 31,
80 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
81 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
82 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
83 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
84 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 },
91 31 + 29 + 31 + 30 + 31,
92 31 + 29 + 31 + 30 + 31 + 30,
93 31 + 29 + 31 + 30 + 31 + 30 + 31,
94 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
95 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
96 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
97 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
98 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};
104 // Do a santity check
106 if ( ETime
->Year
< 1998 || ETime
->Year
> 2099 ||
107 ETime
->Month
== 0 || ETime
->Month
> 12 ||
108 ETime
->Day
== 0 || ETime
->Month
> 31 ||
110 ETime
->Minute
> 59 ||
111 ETime
->Second
> 59 ||
112 ETime
->TimeZone
< -1440 ||
113 (ETime
->TimeZone
> 1440 && ETime
->TimeZone
!= 2047) ) {
121 for (Year
= 1970; Year
!= ETime
->Year
; ++Year
) {
122 UTime
+= (CumulativeDays
[isleap(Year
)][13] * SECSPERDAY
);
126 // UTime should now be set to 00:00:00 on Jan 1 of the file's year.
130 UTime
+= (CumulativeDays
[isleap(ETime
->Year
)][ETime
->Month
] * SECSPERDAY
);
133 // UTime should now be set to 00:00:00 on the first of the file's month and year
135 // Days -- Don't count the file's day
137 UTime
+= (((ETime
->Day
> 0) ? ETime
->Day
-1:0) * SECSPERDAY
);
142 UTime
+= (ETime
->Hour
* SECSPERHOUR
);
147 UTime
+= (ETime
->Minute
* 60);
152 UTime
+= ETime
->Second
;
155 // EFI time is repored in local time. Adjust for any time zone offset to
158 if ( ETime
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
160 // TimeZone is kept in minues...
162 UTime
+= (ETime
->TimeZone
* 60);
170 OUT
struct timeval
*tp
,
171 OUT
struct timezone
*tzp
175 EFI_TIME_CAPABILITIES Capabilities
;
182 Status
= RS
->GetTime( &EfiTime
, &Capabilities
);
183 if (EFI_ERROR(Status
))
187 // Convert to UNIX time (ie seconds since the epoch
190 tp
->tv_sec
= EfiTimeToUnixTime( &EfiTime
);
191 tp
->tv_usec
= 0; /* EfiTime.Nanosecond * 1000; */
194 // Do something with the timezone if needed
198 tzp
->tz_minuteswest
=
199 EfiTime
.TimeZone
== EFI_UNSPECIFIED_TIMEZONE
? 0 : EfiTime
.TimeZone
;
201 // This isn't quit right since it doesn't deal with EFI_TIME_IN_DAYLIGHT
204 EfiTime
.Daylight
& EFI_TIME_ADJUST_DAYLIGHT
? 1 : 0;
214 EFI_GetTimeOfDay(&tv
, 0);