]>
Commit | Line | Data |
---|---|---|
97f98500 HT |
1 | /** @file\r |
2 | C Run-Time Libraries (CRT) Time Management Routines Wrapper Implementation\r | |
3 | for OpenSSL-based Cryptographic Library (used in DXE & RUNTIME).\r | |
4 | \r | |
ab187ae2 | 5 | Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r |
2009f6b4 | 6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
97f98500 HT |
7 | \r |
8 | **/\r | |
9 | \r | |
10 | #include <Uefi.h>\r | |
fc9fa685 | 11 | #include <CrtLibSupport.h>\r |
97f98500 HT |
12 | #include <Library/UefiRuntimeServicesTableLib.h>\r |
13 | \r | |
14 | //\r | |
15 | // -- Time Management Routines --\r | |
16 | //\r | |
17 | \r | |
7c342378 MK |
18 | #define IsLeap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))\r |
19 | #define SECSPERMIN (60)\r | |
20 | #define SECSPERHOUR (60 * 60)\r | |
21 | #define SECSPERDAY (24 * SECSPERHOUR)\r | |
97f98500 HT |
22 | \r |
23 | //\r | |
24 | // The arrays give the cumulative number of days up to the first of the\r | |
25 | // month number used as the index (1 -> 12) for regular and leap years.\r | |
26 | // The value at index 13 is for the whole year.\r | |
27 | //\r | |
7c342378 | 28 | UINTN CumulativeDays[2][14] = {\r |
97f98500 HT |
29 | {\r |
30 | 0,\r | |
31 | 0,\r | |
32 | 31,\r | |
33 | 31 + 28,\r | |
34 | 31 + 28 + 31,\r | |
35 | 31 + 28 + 31 + 30,\r | |
36 | 31 + 28 + 31 + 30 + 31,\r | |
37 | 31 + 28 + 31 + 30 + 31 + 30,\r | |
38 | 31 + 28 + 31 + 30 + 31 + 30 + 31,\r | |
39 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,\r | |
40 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,\r | |
41 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,\r | |
42 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,\r | |
43 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31\r | |
44 | },\r | |
45 | {\r | |
46 | 0,\r | |
47 | 0,\r | |
48 | 31,\r | |
49 | 31 + 29,\r | |
50 | 31 + 29 + 31,\r | |
51 | 31 + 29 + 31 + 30,\r | |
52 | 31 + 29 + 31 + 30 + 31,\r | |
53 | 31 + 29 + 31 + 30 + 31 + 30,\r | |
54 | 31 + 29 + 31 + 30 + 31 + 30 + 31,\r | |
55 | 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,\r | |
56 | 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,\r | |
57 | 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,\r | |
58 | 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,\r | |
630f67dd | 59 | 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31\r |
97f98500 HT |
60 | }\r |
61 | };\r | |
62 | \r | |
63 | /* Get the system time as seconds elapsed since midnight, January 1, 1970. */\r | |
7c342378 | 64 | // INTN time(\r |
97f98500 HT |
65 | // INTN *timer\r |
66 | // )\r | |
7c342378 MK |
67 | time_t\r |
68 | time (\r | |
69 | time_t *timer\r | |
70 | )\r | |
97f98500 | 71 | {\r |
ab187ae2 LQ |
72 | EFI_STATUS Status;\r |
73 | EFI_TIME Time;\r | |
74 | time_t CalTime;\r | |
75 | UINTN Year;\r | |
97f98500 HT |
76 | \r |
77 | //\r | |
78 | // Get the current time and date information\r | |
79 | //\r | |
ab187ae2 LQ |
80 | Status = gRT->GetTime (&Time, NULL);\r |
81 | if (EFI_ERROR (Status) || (Time.Year < 1970)) {\r | |
82 | return 0;\r | |
83 | }\r | |
97f98500 HT |
84 | \r |
85 | //\r | |
86 | // Years Handling\r | |
87 | // UTime should now be set to 00:00:00 on Jan 1 of the current year.\r | |
88 | //\r | |
5e2318dd | 89 | for (Year = 1970, CalTime = 0; Year != Time.Year; Year++) {\r |
7c342378 | 90 | CalTime = CalTime + (time_t)(CumulativeDays[IsLeap (Year)][13] * SECSPERDAY);\r |
97f98500 HT |
91 | }\r |
92 | \r | |
93 | //\r | |
94 | // Add in number of seconds for current Month, Day, Hour, Minute, Seconds, and TimeZone adjustment\r | |
95 | //\r | |
630f67dd | 96 | CalTime = CalTime +\r |
5e2318dd | 97 | (time_t)((Time.TimeZone != EFI_UNSPECIFIED_TIMEZONE) ? (Time.TimeZone * 60) : 0) +\r |
7c342378 | 98 | (time_t)(CumulativeDays[IsLeap (Time.Year)][Time.Month] * SECSPERDAY) +\r |
630f67dd LG |
99 | (time_t)(((Time.Day > 0) ? Time.Day - 1 : 0) * SECSPERDAY) +\r |
100 | (time_t)(Time.Hour * SECSPERHOUR) +\r | |
101 | (time_t)(Time.Minute * 60) +\r | |
5e2318dd JW |
102 | (time_t)Time.Second;\r |
103 | \r | |
104 | if (timer != NULL) {\r | |
105 | *timer = CalTime;\r | |
106 | }\r | |
107 | \r | |
108 | return CalTime;\r | |
97f98500 | 109 | }\r |
b7d320f8 | 110 | \r |
111 | //\r | |
112 | // Convert a time value from type time_t to struct tm.\r | |
113 | //\r | |
7c342378 MK |
114 | struct tm *\r |
115 | gmtime (\r | |
116 | const time_t *timer\r | |
117 | )\r | |
b7d320f8 | 118 | {\r |
119 | struct tm *GmTime;\r | |
120 | UINT16 DayNo;\r | |
3c9e2f23 | 121 | UINT32 DayRemainder;\r |
b7d320f8 | 122 | time_t Year;\r |
123 | time_t YearNo;\r | |
124 | UINT16 TotalDays;\r | |
125 | UINT16 MonthNo;\r | |
126 | \r | |
127 | if (timer == NULL) {\r | |
128 | return NULL;\r | |
129 | }\r | |
130 | \r | |
131 | GmTime = malloc (sizeof (struct tm));\r | |
132 | if (GmTime == NULL) {\r | |
133 | return NULL;\r | |
134 | }\r | |
135 | \r | |
7c342378 | 136 | ZeroMem ((VOID *)GmTime, (UINTN)sizeof (struct tm));\r |
b7d320f8 | 137 | \r |
7c342378 | 138 | DayNo = (UINT16)(*timer / SECSPERDAY);\r |
3c9e2f23 | 139 | DayRemainder = (UINT32)(*timer % SECSPERDAY);\r |
b7d320f8 | 140 | \r |
7c342378 MK |
141 | GmTime->tm_sec = (int)(DayRemainder % SECSPERMIN);\r |
142 | GmTime->tm_min = (int)((DayRemainder % SECSPERHOUR) / SECSPERMIN);\r | |
143 | GmTime->tm_hour = (int)(DayRemainder / SECSPERHOUR);\r | |
144 | GmTime->tm_wday = (int)((DayNo + 4) % 7);\r | |
b7d320f8 | 145 | \r |
146 | for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {\r | |
7c342378 | 147 | TotalDays = (UINT16)(IsLeap (Year) ? 366 : 365);\r |
b7d320f8 | 148 | if (DayNo >= TotalDays) {\r |
7c342378 | 149 | DayNo = (UINT16)(DayNo - TotalDays);\r |
b7d320f8 | 150 | YearNo++;\r |
151 | } else {\r | |
152 | break;\r | |
153 | }\r | |
154 | }\r | |
155 | \r | |
7c342378 MK |
156 | GmTime->tm_year = (int)(YearNo + (1970 - 1900));\r |
157 | GmTime->tm_yday = (int)DayNo;\r | |
b7d320f8 | 158 | \r |
159 | for (MonthNo = 12; MonthNo > 1; MonthNo--) {\r | |
7c342378 MK |
160 | if (DayNo >= CumulativeDays[IsLeap (Year)][MonthNo]) {\r |
161 | DayNo = (UINT16)(DayNo - (UINT16)(CumulativeDays[IsLeap (Year)][MonthNo]));\r | |
b7d320f8 | 162 | break;\r |
163 | }\r | |
164 | }\r | |
165 | \r | |
7c342378 MK |
166 | GmTime->tm_mon = (int)MonthNo - 1;\r |
167 | GmTime->tm_mday = (int)DayNo + 1;\r | |
b7d320f8 | 168 | \r |
169 | GmTime->tm_isdst = 0;\r | |
170 | GmTime->tm_gmtoff = 0;\r | |
171 | GmTime->tm_zone = NULL;\r | |
172 | \r | |
173 | return GmTime;\r | |
174 | }\r |