/** @file\r
*\r
* Copyright (c) 2016, Hisilicon Limited. All rights reserved.\r
-* Copyright (c) 2016, Linaro Limited. All rights reserved.\r
+* Copyright (c) 2016-2019, Linaro Limited. All rights reserved.\r
+* Copyright (c) 2021, Ampere Computing LLC. All rights reserved.\r
*\r
-* This program and the accompanying materials\r
-* are licensed and made available under the terms and conditions of the BSD License\r
-* which accompanies this distribution. The full text of the license may be found at\r
-* http://opensource.org/licenses/bsd-license.php\r
-*\r
-* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+* SPDX-License-Identifier: BSD-2-Clause-Patent\r
*\r
**/\r
\r
#include <Library/TimeBaseLib.h>\r
\r
/**\r
- Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to EFI_TIME\r
- **/\r
+ Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to EFI_TIME.\r
+\r
+ @param EpochSeconds Epoch seconds.\r
+ @param Time The time converted to UEFI format.\r
+\r
+**/\r
VOID\r
EFIAPI\r
EpochToEfiTime (\r
m = (((da * 5) + 308) / 153) - 2;\r
d = da - (((m + 4) * 153) / 5) + 122;\r
\r
- Time->Year = y - 4800 + ((m + 2) / 12);\r
+ Time->Year = (UINT16)(y - 4800 + ((m + 2) / 12));\r
Time->Month = ((m + 2) % 12) + 1;\r
- Time->Day = d + 1;\r
+ Time->Day = (UINT8)(d + 1);\r
\r
ss = EpochSeconds % 60;\r
a = (EpochSeconds - ss) / 60;\r
b = (a - mm) / 60;\r
hh = b % 24;\r
\r
- Time->Hour = hh;\r
- Time->Minute = mm;\r
- Time->Second = ss;\r
+ Time->Hour = (UINT8)hh;\r
+ Time->Minute = (UINT8)mm;\r
+ Time->Second = (UINT8)ss;\r
Time->Nanosecond = 0;\r
\r
}\r
\r
/**\r
- Calculate Epoch days\r
- **/\r
+ Calculate Epoch days.\r
+\r
+ @param Time The UEFI time to be calculated.\r
+\r
+ @return Number of days.\r
+\r
+**/\r
UINTN\r
EFIAPI\r
EfiGetEpochDays (\r
\r
return EpochDays;\r
}\r
+\r
/**\r
- Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC)\r
- **/\r
+ Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC).\r
+\r
+ @param Time The UEFI time to be converted.\r
+\r
+ @return Number of seconds.\r
+\r
+**/\r
UINTN\r
EFIAPI\r
EfiTimeToEpoch (\r
}\r
\r
/**\r
- returns Day of the week [0-6] 0=Sunday\r
- **/\r
+ Get the day of the week from the UEFI time.\r
+\r
+ @param Time The UEFI time to be calculated.\r
+\r
+ @return The day of the week: Sunday=0, Monday=1, ... Saturday=6\r
+\r
+**/\r
UINTN\r
EfiTimeToWday (\r
IN EFI_TIME *Time\r
return (EpochDays + 4) % 7;\r
}\r
\r
+/**\r
+ Check if it is a leap year.\r
+\r
+ @param Time The UEFI time to be checked.\r
+\r
+ @retval TRUE It is a leap year.\r
+ @retval FALSE It is NOT a leap year.\r
+\r
+**/\r
BOOLEAN\r
EFIAPI\r
IsLeapYear (\r
}\r
}\r
\r
+/**\r
+ Check if the day in the UEFI time is valid.\r
+\r
+ @param Time The UEFI time to be checked.\r
+\r
+ @retval TRUE Valid.\r
+ @retval FALSE Invalid.\r
+\r
+**/\r
BOOLEAN\r
EFIAPI\r
IsDayValid (\r
return TRUE;\r
}\r
\r
+/**\r
+ Check if the time zone is valid.\r
+ Valid values are between -1440 and 1440 or 2047 (EFI_UNSPECIFIED_TIMEZONE).\r
+\r
+ @param TimeZone The time zone to be checked.\r
+\r
+ @retval TRUE Valid.\r
+ @retval FALSE Invalid.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+IsValidTimeZone (\r
+ IN INT16 TimeZone\r
+ )\r
+{\r
+ return TimeZone == EFI_UNSPECIFIED_TIMEZONE ||\r
+ (TimeZone >= -1440 && TimeZone <= 1440);\r
+}\r
+\r
+/**\r
+ Check if the daylight is valid.\r
+ Valid values are:\r
+ 0 : Time is not affected.\r
+ 1 : Time is affected, and has not been adjusted for daylight savings.\r
+ 3 : Time is affected, and has been adjusted for daylight savings.\r
+ All other values are invalid.\r
+\r
+ @param Daylight The daylight to be checked.\r
+\r
+ @retval TRUE Valid.\r
+ @retval FALSE Invalid.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+IsValidDaylight (\r
+ IN INT8 Daylight\r
+ )\r
+{\r
+ return Daylight == 0 ||\r
+ Daylight == EFI_TIME_ADJUST_DAYLIGHT ||\r
+ Daylight == (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);\r
+}\r
+\r
+/**\r
+ Check if the UEFI time is valid.\r
+\r
+ @param Time The UEFI time to be checked.\r
+\r
+ @retval TRUE Valid.\r
+ @retval FALSE Invalid.\r
+\r
+**/\r
BOOLEAN\r
EFIAPI\r
-IsTimeValid(\r
+IsTimeValid (\r
IN EFI_TIME *Time\r
)\r
{\r
// Check the input parameters are within the range specified by UEFI\r
- if ((Time->Year < 2000) ||\r
- (Time->Year > 2099) ||\r
- (Time->Month < 1 ) ||\r
- (Time->Month > 12 ) ||\r
- (!IsDayValid (Time) ) ||\r
- (Time->Hour > 23 ) ||\r
- (Time->Minute > 59 ) ||\r
- (Time->Second > 59 ) ||\r
- (Time->Nanosecond > 999999999) ||\r
- (!((Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE) || ((Time->TimeZone >= -1440) && (Time->TimeZone <= 1440)))) ||\r
- (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)))\r
- ) {\r
+ if ((Time->Year < 2000) ||\r
+ (Time->Year > 2099) ||\r
+ (Time->Month < 1 ) ||\r
+ (Time->Month > 12 ) ||\r
+ (!IsDayValid (Time) ) ||\r
+ (Time->Hour > 23 ) ||\r
+ (Time->Minute > 59 ) ||\r
+ (Time->Second > 59 ) ||\r
+ (Time->Nanosecond > 999999999) ||\r
+ (!IsValidTimeZone(Time->TimeZone)) ||\r
+ (!IsValidDaylight(Time->Daylight))) {\r
return FALSE;\r
}\r
\r