TianoCore EDK2 master
Loading...
Searching...
No Matches
TimerWrapper.c
Go to the documentation of this file.
1
10#include <Uefi.h>
14
15typedef int time_t;
16
17//
18// Structures Definitions
19//
20struct tm {
21 int tm_sec; /* seconds after the minute [0-60] */
22 int tm_min; /* minutes after the hour [0-59] */
23 int tm_hour; /* hours since midnight [0-23] */
24 int tm_mday; /* day of the month [1-31] */
25 int tm_mon; /* months since January [0-11] */
26 int tm_year; /* years since 1900 */
27 int tm_wday; /* days since Sunday [0-6] */
28 int tm_yday; /* days since January 1 [0-365] */
29 int tm_isdst; /* Daylight Savings Time flag */
30 long tm_gmtoff; /* offset from CUT in seconds */
31 char *tm_zone; /* timezone abbreviation */
32};
33
34//
35// -- Time Management Routines --
36//
37
38#define IsLeap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
39#define SECSPERMIN (60)
40#define SECSPERHOUR (60 * 60)
41#define SECSPERDAY (24 * SECSPERHOUR)
42
43//
44// The arrays give the cumulative number of days up to the first of the
45// month number used as the index (1 -> 12) for regular and leap years.
46// The value at index 13 is for the whole year.
47//
48UINTN CumulativeDays[2][14] = {
49 {
50 0,
51 0,
52 31,
53 31 + 28,
54 31 + 28 + 31,
55 31 + 28 + 31 + 30,
56 31 + 28 + 31 + 30 + 31,
57 31 + 28 + 31 + 30 + 31 + 30,
58 31 + 28 + 31 + 30 + 31 + 30 + 31,
59 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
60 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
61 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
62 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
63 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31
64 },
65 {
66 0,
67 0,
68 31,
69 31 + 29,
70 31 + 29 + 31,
71 31 + 29 + 31 + 30,
72 31 + 29 + 31 + 30 + 31,
73 31 + 29 + 31 + 30 + 31 + 30,
74 31 + 29 + 31 + 30 + 31 + 30 + 31,
75 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
76 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
77 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
78 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
79 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31
80 }
81};
82
84time_t
85time (
86 time_t *timer
87 )
88{
89 EFI_STATUS Status;
90 EFI_TIME Time;
91 time_t CalTime;
92 UINTN Year;
93
94 //
95 // Get the current time and date information
96 //
97 Status = gRT->GetTime (&Time, NULL);
98 if (EFI_ERROR (Status) || (Time.Year < 1970)) {
99 return 0;
100 }
101
102 //
103 // Years Handling
104 // UTime should now be set to 00:00:00 on Jan 1 of the current year.
105 //
106 for (Year = 1970, CalTime = 0; Year != Time.Year; Year++) {
107 CalTime = CalTime + (time_t)(CumulativeDays[IsLeap (Year)][13] * SECSPERDAY);
108 }
109
110 //
111 // Add in number of seconds for current Month, Day, Hour, Minute, Seconds, and TimeZone adjustment
112 //
113 CalTime = CalTime +
114 (time_t)((Time.TimeZone != EFI_UNSPECIFIED_TIMEZONE) ? (Time.TimeZone * 60) : 0) +
115 (time_t)(CumulativeDays[IsLeap (Time.Year)][Time.Month] * SECSPERDAY) +
116 (time_t)(((Time.Day > 0) ? Time.Day - 1 : 0) * SECSPERDAY) +
117 (time_t)(Time.Hour * SECSPERHOUR) +
118 (time_t)(Time.Minute * 60) +
119 (time_t)Time.Second;
120
121 if (timer != NULL) {
122 *timer = CalTime;
123 }
124
125 return CalTime;
126}
127
129struct tm *
131 const time_t *timer
132 )
133{
134 struct tm *GmTime;
135 UINT16 DayNo;
136 UINT16 DayRemainder;
137 time_t Year;
138 time_t YearNo;
139 UINT16 TotalDays;
140 UINT16 MonthNo;
141
142 if (timer == NULL) {
143 return NULL;
144 }
145
146 GmTime = AllocateZeroPool (sizeof (struct tm));
147 if (GmTime == NULL) {
148 return NULL;
149 }
150
151 ZeroMem ((VOID *)GmTime, (UINTN)sizeof (struct tm));
152
153 DayNo = (UINT16)(*timer / SECSPERDAY);
154 DayRemainder = (UINT16)(*timer % SECSPERDAY);
155
156 GmTime->tm_sec = (int)(DayRemainder % SECSPERMIN);
157 GmTime->tm_min = (int)((DayRemainder % SECSPERHOUR) / SECSPERMIN);
158 GmTime->tm_hour = (int)(DayRemainder / SECSPERHOUR);
159 GmTime->tm_wday = (int)((DayNo + 4) % 7);
160
161 for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {
162 TotalDays = (UINT16)(IsLeap (Year) ? 366 : 365);
163 if (DayNo >= TotalDays) {
164 DayNo = (UINT16)(DayNo - TotalDays);
165 YearNo++;
166 } else {
167 break;
168 }
169 }
170
171 GmTime->tm_year = (int)(YearNo + (1970 - 1900));
172 GmTime->tm_yday = (int)DayNo;
173
174 for (MonthNo = 12; MonthNo > 1; MonthNo--) {
175 if (DayNo >= CumulativeDays[IsLeap (Year)][MonthNo]) {
176 DayNo = (UINT16)(DayNo - (UINT16)(CumulativeDays[IsLeap (Year)][MonthNo]));
177 break;
178 }
179 }
180
181 GmTime->tm_mon = (int)MonthNo - 1;
182 GmTime->tm_mday = (int)DayNo + 1;
183
184 GmTime->tm_isdst = 0;
185 GmTime->tm_gmtoff = 0;
186 GmTime->tm_zone = NULL;
187
188 return GmTime;
189}
190
192time_t
194 time_t *t
195 )
196{
197 return time (t);
198}
UINT64 UINTN
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
time_t time(time_t *)
struct tm * gmtime(const time_t *)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
EFI_RUNTIME_SERVICES * gRT
#define NULL
Definition: Base.h:319
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_UNSPECIFIED_TIMEZONE
Definition: UefiSpec.h:58
time_t _time64(time_t *t)
Definition: TimerWrapper.c:193