1 | /* $Id: Timestamp.h 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DHCP server - timestamps
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-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 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef VBOX_INCLUDED_SRC_Dhcpd_Timestamp_h
|
---|
29 | #define VBOX_INCLUDED_SRC_Dhcpd_Timestamp_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <iprt/time.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Wrapper around RTTIMESPEC.
|
---|
39 | *
|
---|
40 | * @note Originally wanting to use RTTimeNanoTS rather than RTTimeNow. The term
|
---|
41 | * "absolute" was used for when the RTTimeNanoTS() value was converted to
|
---|
42 | * something approximating unix epoch relative time with help of
|
---|
43 | * RTTimeNow(). Code was later changed to just wrap RTTIMESPEC and drop
|
---|
44 | * all usage of RTTimeNanoTS, ASSUMING that system time is stable.
|
---|
45 | */
|
---|
46 | class Timestamp
|
---|
47 | {
|
---|
48 | RTTIMESPEC m_TimeSpec;
|
---|
49 |
|
---|
50 | public:
|
---|
51 | Timestamp() RT_NOEXCEPT
|
---|
52 | {
|
---|
53 | RTTimeSpecSetNano(&m_TimeSpec, 0);
|
---|
54 | }
|
---|
55 |
|
---|
56 | Timestamp(PCRTTIMESPEC a_pTimeSpec) RT_NOEXCEPT
|
---|
57 | {
|
---|
58 | m_TimeSpec = *a_pTimeSpec;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /** Get a timestamp initialized to current time. */
|
---|
62 | static Timestamp now() RT_NOEXCEPT
|
---|
63 | {
|
---|
64 | RTTIMESPEC Tmp;
|
---|
65 | return Timestamp(RTTimeNow(&Tmp));
|
---|
66 | }
|
---|
67 |
|
---|
68 | /** Get a timestamp with the given value in seconds since unix epoch. */
|
---|
69 | static Timestamp absSeconds(int64_t secTimestamp) RT_NOEXCEPT
|
---|
70 | {
|
---|
71 | RTTIMESPEC Tmp;
|
---|
72 | return Timestamp(RTTimeSpecSetSeconds(&Tmp, secTimestamp));
|
---|
73 | }
|
---|
74 |
|
---|
75 | Timestamp &addSeconds(int64_t cSecs) RT_NOEXCEPT
|
---|
76 | {
|
---|
77 | RTTimeSpecAddSeconds(&m_TimeSpec, cSecs);
|
---|
78 | return *this;
|
---|
79 | }
|
---|
80 |
|
---|
81 | Timestamp &subSeconds(int64_t cSecs) RT_NOEXCEPT
|
---|
82 | {
|
---|
83 | RTTimeSpecSubSeconds(&m_TimeSpec, cSecs);
|
---|
84 | return *this;
|
---|
85 | }
|
---|
86 |
|
---|
87 | RTTIMESPEC *getAbsTimeSpec(RTTIMESPEC *pTime) const RT_NOEXCEPT
|
---|
88 | {
|
---|
89 | *pTime = m_TimeSpec;
|
---|
90 | return pTime;
|
---|
91 | }
|
---|
92 |
|
---|
93 | int64_t getAbsSeconds() const RT_NOEXCEPT
|
---|
94 | {
|
---|
95 | return RTTimeSpecGetSeconds(&m_TimeSpec);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Only for log formatting. */
|
---|
99 | size_t strFormatHelper(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput) const RT_NOEXCEPT;
|
---|
100 |
|
---|
101 | int compare(const Timestamp &a_rRight) const RT_NOEXCEPT
|
---|
102 | {
|
---|
103 | return RTTimeSpecCompare(&m_TimeSpec, &a_rRight.m_TimeSpec);
|
---|
104 | }
|
---|
105 |
|
---|
106 | friend bool operator<( const Timestamp &, const Timestamp &) RT_NOEXCEPT;
|
---|
107 | friend bool operator>( const Timestamp &, const Timestamp &) RT_NOEXCEPT;
|
---|
108 | friend bool operator==(const Timestamp &, const Timestamp &) RT_NOEXCEPT;
|
---|
109 | friend bool operator!=(const Timestamp &, const Timestamp &) RT_NOEXCEPT;
|
---|
110 | friend bool operator<=(const Timestamp &, const Timestamp &) RT_NOEXCEPT;
|
---|
111 | friend bool operator>=(const Timestamp &, const Timestamp &) RT_NOEXCEPT;
|
---|
112 | };
|
---|
113 |
|
---|
114 |
|
---|
115 | inline bool operator<( const Timestamp &l, const Timestamp &r) RT_NOEXCEPT { return l.compare(r) < 0; }
|
---|
116 | inline bool operator>( const Timestamp &l, const Timestamp &r) RT_NOEXCEPT { return l.compare(r) > 0; }
|
---|
117 | inline bool operator==(const Timestamp &l, const Timestamp &r) RT_NOEXCEPT { return l.compare(r) == 0; }
|
---|
118 | inline bool operator!=(const Timestamp &l, const Timestamp &r) RT_NOEXCEPT { return l.compare(r) != 0; }
|
---|
119 | inline bool operator<=(const Timestamp &l, const Timestamp &r) RT_NOEXCEPT { return l.compare(r) <= 0; }
|
---|
120 | inline bool operator>=(const Timestamp &l, const Timestamp &r) RT_NOEXCEPT { return l.compare(r) >= 0; }
|
---|
121 |
|
---|
122 | #endif /* !VBOX_INCLUDED_SRC_Dhcpd_Timestamp_h */
|
---|