]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
7d28446d4b5c6673b35fee3282612843431502fc
2 C Run-Time Libraries (CRT) Time Management Routines Wrapper Implementation
3 for OpenSSL-based Cryptographic Library (used in DXE & RUNTIME).
5 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <CrtLibSupport.h>
12 #include <Library/UefiRuntimeServicesTableLib.h>
15 // -- Time Management Routines --
18 #define IsLeap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
19 #define SECSPERMIN (60)
20 #define SECSPERHOUR (60 * 60)
21 #define SECSPERDAY (24 * SECSPERHOUR)
24 // The arrays give the cumulative number of days up to the first of the
25 // month number used as the index (1 -> 12) for regular and leap years.
26 // The value at index 13 is for the whole year.
28 UINTN CumulativeDays
[2][14] = {
36 31 + 28 + 31 + 30 + 31,
37 31 + 28 + 31 + 30 + 31 + 30,
38 31 + 28 + 31 + 30 + 31 + 30 + 31,
39 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
40 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
41 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
42 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
43 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31
52 31 + 29 + 31 + 30 + 31,
53 31 + 29 + 31 + 30 + 31 + 30,
54 31 + 29 + 31 + 30 + 31 + 30 + 31,
55 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
56 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
57 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
58 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
59 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31
63 /* Get the system time as seconds elapsed since midnight, January 1, 1970. */
78 // Get the current time and date information
80 Status
= gRT
->GetTime (&Time
, NULL
);
81 if (EFI_ERROR (Status
) || (Time
.Year
< 1970)) {
87 // UTime should now be set to 00:00:00 on Jan 1 of the current year.
89 for (Year
= 1970, CalTime
= 0; Year
!= Time
.Year
; Year
++) {
90 CalTime
= CalTime
+ (time_t)(CumulativeDays
[IsLeap (Year
)][13] * SECSPERDAY
);
94 // Add in number of seconds for current Month, Day, Hour, Minute, Seconds, and TimeZone adjustment
97 (time_t)((Time
.TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) ? (Time
.TimeZone
* 60) : 0) +
98 (time_t)(CumulativeDays
[IsLeap (Time
.Year
)][Time
.Month
] * SECSPERDAY
) +
99 (time_t)(((Time
.Day
> 0) ? Time
.Day
- 1 : 0) * SECSPERDAY
) +
100 (time_t)(Time
.Hour
* SECSPERHOUR
) +
101 (time_t)(Time
.Minute
* 60) +
112 // Convert a time value from type time_t to struct tm.
131 GmTime
= malloc (sizeof (struct tm
));
132 if (GmTime
== NULL
) {
136 ZeroMem ((VOID
*)GmTime
, (UINTN
)sizeof (struct tm
));
138 DayNo
= (UINT16
)(*timer
/ SECSPERDAY
);
139 DayRemainder
= (UINT16
)(*timer
% SECSPERDAY
);
141 GmTime
->tm_sec
= (int)(DayRemainder
% SECSPERMIN
);
142 GmTime
->tm_min
= (int)((DayRemainder
% SECSPERHOUR
) / SECSPERMIN
);
143 GmTime
->tm_hour
= (int)(DayRemainder
/ SECSPERHOUR
);
144 GmTime
->tm_wday
= (int)((DayNo
+ 4) % 7);
146 for (Year
= 1970, YearNo
= 0; DayNo
> 0; Year
++) {
147 TotalDays
= (UINT16
)(IsLeap (Year
) ? 366 : 365);
148 if (DayNo
>= TotalDays
) {
149 DayNo
= (UINT16
)(DayNo
- TotalDays
);
156 GmTime
->tm_year
= (int)(YearNo
+ (1970 - 1900));
157 GmTime
->tm_yday
= (int)DayNo
;
159 for (MonthNo
= 12; MonthNo
> 1; MonthNo
--) {
160 if (DayNo
>= CumulativeDays
[IsLeap (Year
)][MonthNo
]) {
161 DayNo
= (UINT16
)(DayNo
- (UINT16
)(CumulativeDays
[IsLeap (Year
)][MonthNo
]));
166 GmTime
->tm_mon
= (int)MonthNo
- 1;
167 GmTime
->tm_mday
= (int)DayNo
+ 1;
169 GmTime
->tm_isdst
= 0;
170 GmTime
->tm_gmtoff
= 0;
171 GmTime
->tm_zone
= NULL
;