]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Time/TimeEfi.c
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / StdLib / LibC / Time / TimeEfi.c
CommitLineData
2aa62f2b 1/** @file\r
2 Transformations between the EFI_TIME structure and struct tm or time_t.\r
3\r
4 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials are licensed and made available under\r
6 the terms and conditions of the BSD License that accompanies this distribution.\r
7 The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14#include <Uefi.h>\r
15\r
16#include <LibConfig.h>\r
17\r
18#include <time.h>\r
19#include "tzfile.h"\r
20#include <MainData.h>\r
21\r
0c1992fb 22/** Convert an EFI_TIME structure into a C Standard tm structure.\r
23\r
24 @param[in] ET Pointer to the EFI_TIME structure to convert.\r
25 @param[out] BT Pointer to the tm structure to receive the converted time.\r
26*/\r
2aa62f2b 27void\r
0c1992fb 28Efi2Tm(\r
29 IN EFI_TIME *ET,\r
30 OUT struct tm *BT\r
31 )\r
2aa62f2b 32{\r
33 // Convert EFI time to broken-down time.\r
34 BT->tm_year = ET->Year - TM_YEAR_BASE;\r
35 BT->tm_mon = ET->Month - 1; // BD time is zero based, EFI is 1 based\r
36 BT->tm_mday = ET->Day;\r
37 BT->tm_hour = ET->Hour;\r
38 BT->tm_min = ET->Minute;\r
39 BT->tm_sec = ET->Second;\r
40 BT->tm_isdst = -1;\r
41 BT->tm_zoneoff = ET->TimeZone;\r
42 BT->tm_daylight = ET->Daylight;\r
43 BT->tm_Nano = ET->Nanosecond;\r
44}\r
45\r
0c1992fb 46/** Convert an EFI_TIME structure into a time_t value.\r
47\r
48 @param[in] EfiBDtime Pointer to the EFI_TIME structure to convert.\r
49\r
50 @return The EFI_TIME converted into a time_t value.\r
51*/\r
2aa62f2b 52time_t\r
0c1992fb 53Efi2Time(\r
54 IN EFI_TIME *EfiBDtime\r
55 )\r
2aa62f2b 56{\r
57 Efi2Tm( EfiBDtime, &gMD->BDTime);\r
58\r
59 return mktime( &gMD->BDTime);\r
60}\r
0c1992fb 61\r
62/** Convert a C Standard tm structure into an EFI_TIME structure.\r
63\r
64 @param[in] BT Pointer to the tm structure to convert.\r
65 @param[out] ET Pointer to an EFI_TIME structure to receive the converted time.\r
66*/\r
67void\r
68Tm2Efi(\r
69 IN struct tm *BT,\r
70 OUT EFI_TIME *ET\r
71 )\r
72{\r
73 ET->Year = (UINT16)BT->tm_year + TM_YEAR_BASE;\r
74 ET->Month = (UINT8)BT->tm_mon + 1;\r
75 ET->Day = (UINT8)BT->tm_mday;\r
76 ET->Hour = (UINT8)BT->tm_hour;\r
77 ET->Minute = (UINT8)BT->tm_min;\r
78 ET->Second = (UINT8)BT->tm_sec;\r
79 ET->Nanosecond = (UINT32)BT->tm_Nano;\r
80 ET->TimeZone = (INT16)BT->tm_zoneoff;\r
81 ET->Daylight = (UINT8)BT->tm_daylight;\r
82}\r
83\r
84/** Convert a time_t value into an EFI_TIME structure.\r
85\r
86 @param[in] CalTime Calendar time as a time_t value.\r
87\r
88 @return Returns a newly malloced EFI_TIME structure containing\r
89 the converted calendar time.\r
90\r
91 @post It is the responsibility of the caller to free the\r
92 returned structure before the application exits.\r
93*/\r
94EFI_TIME*\r
95Time2Efi(\r
96 IN time_t CalTime\r
97 )\r
98{\r
99 struct tm *IT;\r
100 EFI_TIME *ET = NULL;\r
101\r
102 IT = gmtime(&CalTime);\r
103 if(IT != NULL) {\r
104 ET = malloc(sizeof(EFI_TIME));\r
105 if(ET != NULL) {\r
106 Tm2Efi(IT, ET);\r
107 }\r
108 }\r
109 return ET;\r
110}\r