Deleted Added
full compact
time.c (77943) time.c (78328)
1/* $FreeBSD: head/sys/boot/efi/libefi/time.c 77943 2001-06-09 16:49:51Z dfr $ */
2/*
3 * Copyright (c) 1999, 2000
4 * Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:

--- 24 unchanged lines hidden (view full) ---

34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
38 * THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 */
41
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:

--- 24 unchanged lines hidden (view full) ---

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 */
40
41#ifndef lint
42static const char rcsid[] =
43 "$FreeBSD: head/sys/boot/efi/libefi/time.c 78328 2001-06-16 05:58:54Z obrien $";
44#endif /* not lint */
45
42#include <efi.h>
43#include <efilib.h>
44
45#include <time.h>
46#include <sys/time.h>
47
46#include <efi.h>
47#include <efilib.h>
48
49#include <time.h>
50#include <sys/time.h>
51
48//
52/*
49// Accurate only for the past couple of centuries;
50// that will probably do.
51//
52// (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)
53// Accurate only for the past couple of centuries;
54// that will probably do.
55//
56// (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)
53//
57*/
54
55#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
56#define SECSPERHOUR ( 60*60 )
57#define SECSPERDAY (24 * SECSPERHOUR)
58
59time_t
60EfiTimeToUnixTime(EFI_TIME *ETime)
61{
58
59#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
60#define SECSPERHOUR ( 60*60 )
61#define SECSPERDAY (24 * SECSPERHOUR)
62
63time_t
64EfiTimeToUnixTime(EFI_TIME *ETime)
65{
62 //
66 /*
63 // These arrays give the cumulative number of days up to the first of the
64 // month number used as the index (1 -> 12) for regular and leap years.
65 // The value at index 13 is for the whole year.
67 // These arrays give the cumulative number of days up to the first of the
68 // month number used as the index (1 -> 12) for regular and leap years.
69 // The value at index 13 is for the whole year.
66 //
70 */
67 static time_t CumulativeDays[2][14] = {
68 {0,
69 0,
70 31,
71 31 + 28,
72 31 + 28 + 31,
73 31 + 28 + 31 + 30,
74 31 + 28 + 31 + 30 + 31,

--- 17 unchanged lines hidden (view full) ---

92 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
93 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
94 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
95 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};
96
97 time_t UTime;
98 int Year;
99
71 static time_t CumulativeDays[2][14] = {
72 {0,
73 0,
74 31,
75 31 + 28,
76 31 + 28 + 31,
77 31 + 28 + 31 + 30,
78 31 + 28 + 31 + 30 + 31,

--- 17 unchanged lines hidden (view full) ---

96 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
97 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
98 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
99 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};
100
101 time_t UTime;
102 int Year;
103
100 //
104 /*
101 // Do a santity check
105 // Do a santity check
102 //
106 */
103 if ( ETime->Year < 1998 || ETime->Year > 2099 ||
104 ETime->Month == 0 || ETime->Month > 12 ||
105 ETime->Day == 0 || ETime->Month > 31 ||
106 ETime->Hour > 23 ||
107 ETime->Minute > 59 ||
108 ETime->Second > 59 ||
109 ETime->TimeZone < -1440 ||
110 (ETime->TimeZone > 1440 && ETime->TimeZone != 2047) ) {
111 return (0);
112 }
113
107 if ( ETime->Year < 1998 || ETime->Year > 2099 ||
108 ETime->Month == 0 || ETime->Month > 12 ||
109 ETime->Day == 0 || ETime->Month > 31 ||
110 ETime->Hour > 23 ||
111 ETime->Minute > 59 ||
112 ETime->Second > 59 ||
113 ETime->TimeZone < -1440 ||
114 (ETime->TimeZone > 1440 && ETime->TimeZone != 2047) ) {
115 return (0);
116 }
117
114 //
118 /*
115 // Years
119 // Years
116 //
120 */
117 UTime = 0;
118 for (Year = 1970; Year != ETime->Year; ++Year) {
119 UTime += (CumulativeDays[isleap(Year)][13] * SECSPERDAY);
120 }
121
121 UTime = 0;
122 for (Year = 1970; Year != ETime->Year; ++Year) {
123 UTime += (CumulativeDays[isleap(Year)][13] * SECSPERDAY);
124 }
125
122 //
126 /*
123 // UTime should now be set to 00:00:00 on Jan 1 of the file's year.
127 // UTime should now be set to 00:00:00 on Jan 1 of the file's year.
124 //
125 // Months
126 //
128 //
129 // Months
130 */
127 UTime += (CumulativeDays[isleap(ETime->Year)][ETime->Month] * SECSPERDAY);
128
131 UTime += (CumulativeDays[isleap(ETime->Year)][ETime->Month] * SECSPERDAY);
132
129 //
133 /*
130 // UTime should now be set to 00:00:00 on the first of the file's month and year
131 //
132 // Days -- Don't count the file's day
134 // UTime should now be set to 00:00:00 on the first of the file's month and year
135 //
136 // Days -- Don't count the file's day
133 //
137 */
134 UTime += (((ETime->Day > 0) ? ETime->Day-1:0) * SECSPERDAY);
135
138 UTime += (((ETime->Day > 0) ? ETime->Day-1:0) * SECSPERDAY);
139
136 //
140 /*
137 // Hours
141 // Hours
138 //
142 */
139 UTime += (ETime->Hour * SECSPERHOUR);
140
143 UTime += (ETime->Hour * SECSPERHOUR);
144
141 //
145 /*
142 // Minutes
146 // Minutes
143 //
147 */
144 UTime += (ETime->Minute * 60);
145
148 UTime += (ETime->Minute * 60);
149
146 //
150 /*
147 // Seconds
151 // Seconds
148 //
152 */
149 UTime += ETime->Second;
150
153 UTime += ETime->Second;
154
151 //
155 /*
152 // EFI time is repored in local time. Adjust for any time zone offset to
153 // get true UT
156 // EFI time is repored in local time. Adjust for any time zone offset to
157 // get true UT
154 //
158 */
155 if ( ETime->TimeZone != EFI_UNSPECIFIED_TIMEZONE ) {
159 if ( ETime->TimeZone != EFI_UNSPECIFIED_TIMEZONE ) {
156 //
160 /*
157 // TimeZone is kept in minues...
161 // TimeZone is kept in minues...
158 //
162 */
159 UTime += (ETime->TimeZone * 60);
160 }
161
162 return UTime;
163}
164
165int
166EFI_GetTimeOfDay(
167 OUT struct timeval *tp,
168 OUT struct timezone *tzp
169 )
170{
171 EFI_TIME EfiTime;
172 EFI_TIME_CAPABILITIES Capabilities;
173 EFI_STATUS Status;
174
163 UTime += (ETime->TimeZone * 60);
164 }
165
166 return UTime;
167}
168
169int
170EFI_GetTimeOfDay(
171 OUT struct timeval *tp,
172 OUT struct timezone *tzp
173 )
174{
175 EFI_TIME EfiTime;
176 EFI_TIME_CAPABILITIES Capabilities;
177 EFI_STATUS Status;
178
175 //
179 /*
176 // Get time from EFI
180 // Get time from EFI
177 //
181 */
178
179 Status = RS->GetTime( &EfiTime, &Capabilities );
180 if (EFI_ERROR(Status))
181 return (-1);
182
182
183 Status = RS->GetTime( &EfiTime, &Capabilities );
184 if (EFI_ERROR(Status))
185 return (-1);
186
183 //
187 /*
184 // Convert to UNIX time (ie seconds since the epoch
188 // Convert to UNIX time (ie seconds since the epoch
185 //
189 */
186
187 tp->tv_sec = EfiTimeToUnixTime( &EfiTime );
190
191 tp->tv_sec = EfiTimeToUnixTime( &EfiTime );
188 tp->tv_usec = 0; // EfiTime.Nanosecond * 1000;
192 tp->tv_usec = 0; /* EfiTime.Nanosecond * 1000; */
189
193
190 //
194 /*
191 // Do something with the timezone if needed
195 // Do something with the timezone if needed
192 //
196 */
193
194 if (tzp) {
195 tzp->tz_minuteswest =
196 EfiTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE ? 0 : EfiTime.TimeZone;
197
198 if (tzp) {
199 tzp->tz_minuteswest =
200 EfiTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE ? 0 : EfiTime.TimeZone;
197 //
201 /*
198 // This isn't quit right since it doesn't deal with EFI_TIME_IN_DAYLIGHT
202 // This isn't quit right since it doesn't deal with EFI_TIME_IN_DAYLIGHT
199 //
203 */
200 tzp->tz_dsttime =
201 EfiTime.Daylight & EFI_TIME_ADJUST_DAYLIGHT ? 1 : 0;
202 }
203
204 return (0);
205}
206
207time_t
208time(time_t *tloc)
209{
210 struct timeval tv;
211 EFI_GetTimeOfDay(&tv, 0);
212
213 if (tloc)
214 *tloc = tv.tv_sec;
215 return tv.tv_sec;
216}
204 tzp->tz_dsttime =
205 EfiTime.Daylight & EFI_TIME_ADJUST_DAYLIGHT ? 1 : 0;
206 }
207
208 return (0);
209}
210
211time_t
212time(time_t *tloc)
213{
214 struct timeval tv;
215 EFI_GetTimeOfDay(&tv, 0);
216
217 if (tloc)
218 *tloc = tv.tv_sec;
219 return tv.tv_sec;
220}