[70836] | 1 | /* $Id: Timestamp.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * DHCP server - timestamps
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2017-2023 Oracle and/or its affiliates.
|
---|
[70836] | 8 | *
|
---|
[96407] | 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
|
---|
[70836] | 26 | */
|
---|
| 27 |
|
---|
[79530] | 28 | #ifndef VBOX_INCLUDED_SRC_Dhcpd_Timestamp_h
|
---|
| 29 | #define VBOX_INCLUDED_SRC_Dhcpd_Timestamp_h
|
---|
[76525] | 30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 31 | # pragma once
|
---|
| 32 | #endif
|
---|
[70836] | 33 |
|
---|
| 34 | #include <iprt/time.h>
|
---|
| 35 |
|
---|
| 36 |
|
---|
[79524] | 37 | /**
|
---|
[79526] | 38 | * Wrapper around RTTIMESPEC.
|
---|
[79524] | 39 | *
|
---|
[79526] | 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.
|
---|
[70836] | 45 | */
|
---|
[79524] | 46 | class Timestamp
|
---|
[70836] | 47 | {
|
---|
[79526] | 48 | RTTIMESPEC m_TimeSpec;
|
---|
[70836] | 49 |
|
---|
| 50 | public:
|
---|
[79563] | 51 | Timestamp() RT_NOEXCEPT
|
---|
[79526] | 52 | {
|
---|
| 53 | RTTimeSpecSetNano(&m_TimeSpec, 0);
|
---|
| 54 | }
|
---|
[70836] | 55 |
|
---|
[79563] | 56 | Timestamp(PCRTTIMESPEC a_pTimeSpec) RT_NOEXCEPT
|
---|
[79526] | 57 | {
|
---|
| 58 | m_TimeSpec = *a_pTimeSpec;
|
---|
| 59 | }
|
---|
[70836] | 60 |
|
---|
[79526] | 61 | /** Get a timestamp initialized to current time. */
|
---|
[79563] | 62 | static Timestamp now() RT_NOEXCEPT
|
---|
[70836] | 63 | {
|
---|
[79526] | 64 | RTTIMESPEC Tmp;
|
---|
| 65 | return Timestamp(RTTimeNow(&Tmp));
|
---|
[70836] | 66 | }
|
---|
| 67 |
|
---|
[79526] | 68 | /** Get a timestamp with the given value in seconds since unix epoch. */
|
---|
[79563] | 69 | static Timestamp absSeconds(int64_t secTimestamp) RT_NOEXCEPT
|
---|
[70836] | 70 | {
|
---|
[79526] | 71 | RTTIMESPEC Tmp;
|
---|
| 72 | return Timestamp(RTTimeSpecSetSeconds(&Tmp, secTimestamp));
|
---|
[70836] | 73 | }
|
---|
| 74 |
|
---|
[79563] | 75 | Timestamp &addSeconds(int64_t cSecs) RT_NOEXCEPT
|
---|
[70836] | 76 | {
|
---|
[79526] | 77 | RTTimeSpecAddSeconds(&m_TimeSpec, cSecs);
|
---|
[70836] | 78 | return *this;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[79563] | 81 | Timestamp &subSeconds(int64_t cSecs) RT_NOEXCEPT
|
---|
[70836] | 82 | {
|
---|
[79526] | 83 | RTTimeSpecSubSeconds(&m_TimeSpec, cSecs);
|
---|
[70836] | 84 | return *this;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[79563] | 87 | RTTIMESPEC *getAbsTimeSpec(RTTIMESPEC *pTime) const RT_NOEXCEPT
|
---|
[70836] | 88 | {
|
---|
[79526] | 89 | *pTime = m_TimeSpec;
|
---|
[70836] | 90 | return pTime;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[79563] | 93 | int64_t getAbsSeconds() const RT_NOEXCEPT
|
---|
[70836] | 94 | {
|
---|
[79526] | 95 | return RTTimeSpecGetSeconds(&m_TimeSpec);
|
---|
[70836] | 96 | }
|
---|
| 97 |
|
---|
[79526] | 98 | /** Only for log formatting. */
|
---|
[79563] | 99 | size_t strFormatHelper(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput) const RT_NOEXCEPT;
|
---|
[70836] | 100 |
|
---|
[79563] | 101 | int compare(const Timestamp &a_rRight) const RT_NOEXCEPT
|
---|
[79526] | 102 | {
|
---|
| 103 | return RTTimeSpecCompare(&m_TimeSpec, &a_rRight.m_TimeSpec);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[79563] | 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;
|
---|
[70836] | 112 | };
|
---|
| 113 |
|
---|
| 114 |
|
---|
[79563] | 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; }
|
---|
[70836] | 121 |
|
---|
[79530] | 122 | #endif /* !VBOX_INCLUDED_SRC_Dhcpd_Timestamp_h */
|
---|