1 | /* $Id: efitime.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - EFI time conversion helpers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2021-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #define LOG_GROUP RTLOGGROUP_TIME
|
---|
42 | #include <iprt/efi.h>
|
---|
43 |
|
---|
44 | #include <iprt/cdefs.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Defined Constants And Macros *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 |
|
---|
52 |
|
---|
53 | /*********************************************************************************************************************************
|
---|
54 | * Structures and Typedefs *
|
---|
55 | *********************************************************************************************************************************/
|
---|
56 |
|
---|
57 |
|
---|
58 | /*********************************************************************************************************************************
|
---|
59 | * Internal Functions *
|
---|
60 | *********************************************************************************************************************************/
|
---|
61 |
|
---|
62 | RTDECL(PRTTIMESPEC) RTEfiTimeToTimeSpec(PRTTIMESPEC pTimeSpec, PCEFI_TIME pEfiTime)
|
---|
63 | {
|
---|
64 | RTTIME Time; RT_ZERO(Time);
|
---|
65 |
|
---|
66 | Time.i32Year = pEfiTime->u16Year;
|
---|
67 | Time.u8Month = pEfiTime->u8Month;
|
---|
68 | Time.u8MonthDay = pEfiTime->u8Day;
|
---|
69 | Time.u8Hour = pEfiTime->u8Hour;
|
---|
70 | Time.u8Minute = pEfiTime->u8Minute;
|
---|
71 | Time.u8Second = pEfiTime->u8Second;
|
---|
72 | Time.u32Nanosecond = pEfiTime->u32Nanosecond;
|
---|
73 | if (pEfiTime->iTimezone != EFI_TIME_TIMEZONE_UNSPECIFIED)
|
---|
74 | Time.offUTC = pEfiTime->iTimezone;
|
---|
75 | Time.fFlags = RTTIME_FLAGS_TYPE_LOCAL;
|
---|
76 | if (RTTimeIsLeapYear(Time.i32Year))
|
---|
77 | Time.fFlags |= RTTIME_FLAGS_LEAP_YEAR;
|
---|
78 | else
|
---|
79 | Time.fFlags |= RTTIME_FLAGS_COMMON_YEAR;
|
---|
80 | if (pEfiTime->u8Daylight & EFI_TIME_DAYLIGHT_ADJUST)
|
---|
81 | {
|
---|
82 | if (pEfiTime->u8Daylight & EFI_TIME_DAYLIGHT_INDST)
|
---|
83 | Time.fFlags |= RTTIME_FLAGS_DST;
|
---|
84 | }
|
---|
85 | else
|
---|
86 | Time.fFlags |= RTTIME_FLAGS_NO_DST_DATA;
|
---|
87 |
|
---|
88 | if (!RTTimeLocalNormalize(&Time))
|
---|
89 | return NULL;
|
---|
90 |
|
---|
91 | return RTTimeImplode(pTimeSpec, &Time);
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | RTDECL(PEFI_TIME) RTEfiTimeFromTimeSpec(PEFI_TIME pEfiTime, PCRTTIMESPEC pTimeSpec)
|
---|
96 | {
|
---|
97 | RTTIME Time; RT_ZERO(Time);
|
---|
98 | if (!RTTimeExplode(&Time, pTimeSpec))
|
---|
99 | return NULL;
|
---|
100 |
|
---|
101 | RT_ZERO(*pEfiTime);
|
---|
102 | pEfiTime->u16Year = Time.i32Year < 0
|
---|
103 | ? 0
|
---|
104 | : (uint16_t)Time.i32Year;
|
---|
105 | pEfiTime->u8Month = Time.u8Month;
|
---|
106 | pEfiTime->u8Day = Time.u8MonthDay;
|
---|
107 | pEfiTime->u8Hour = Time.u8Hour;
|
---|
108 | pEfiTime->u8Minute = Time.u8Minute;
|
---|
109 | pEfiTime->u8Second = Time.u8Second;
|
---|
110 | pEfiTime->u32Nanosecond = Time.u32Nanosecond;
|
---|
111 | if ((Time.fFlags & RTTIME_FLAGS_TYPE_MASK) == RTTIME_FLAGS_TYPE_LOCAL)
|
---|
112 | pEfiTime->iTimezone = Time.offUTC;
|
---|
113 | else
|
---|
114 | pEfiTime->iTimezone = EFI_TIME_TIMEZONE_UNSPECIFIED;
|
---|
115 | if (!(Time.fFlags & RTTIME_FLAGS_NO_DST_DATA))
|
---|
116 | {
|
---|
117 | pEfiTime->u8Daylight = EFI_TIME_DAYLIGHT_ADJUST;
|
---|
118 | if (Time.fFlags & RTTIME_FLAGS_DST)
|
---|
119 | pEfiTime->u8Daylight |= EFI_TIME_DAYLIGHT_INDST;
|
---|
120 | }
|
---|
121 | return pEfiTime;
|
---|
122 | }
|
---|
123 |
|
---|