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