1 | /* $Id: Timestamp.h 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DHCP server - timestamps
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef VBOX_INCLUDED_SRC_Dhcpd_Timestamp_h
|
---|
19 | #define VBOX_INCLUDED_SRC_Dhcpd_Timestamp_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include <iprt/time.h>
|
---|
25 |
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Wrapper around RTTIMESPEC.
|
---|
29 | *
|
---|
30 | * @note Originally wanting to use RTTimeNanoTS rather than RTTimeNow. The term
|
---|
31 | * "absolute" was used for when the RTTimeNanoTS() value was converted to
|
---|
32 | * something approximating unix epoch relative time with help of
|
---|
33 | * RTTimeNow(). Code was later changed to just wrap RTTIMESPEC and drop
|
---|
34 | * all usage of RTTimeNanoTS, ASSUMING that system time is stable.
|
---|
35 | */
|
---|
36 | class Timestamp
|
---|
37 | {
|
---|
38 | RTTIMESPEC m_TimeSpec;
|
---|
39 |
|
---|
40 | public:
|
---|
41 | Timestamp() RT_NOEXCEPT
|
---|
42 | {
|
---|
43 | RTTimeSpecSetNano(&m_TimeSpec, 0);
|
---|
44 | }
|
---|
45 |
|
---|
46 | Timestamp(PCRTTIMESPEC a_pTimeSpec) RT_NOEXCEPT
|
---|
47 | {
|
---|
48 | m_TimeSpec = *a_pTimeSpec;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /** Get a timestamp initialized to current time. */
|
---|
52 | static Timestamp now() RT_NOEXCEPT
|
---|
53 | {
|
---|
54 | RTTIMESPEC Tmp;
|
---|
55 | return Timestamp(RTTimeNow(&Tmp));
|
---|
56 | }
|
---|
57 |
|
---|
58 | /** Get a timestamp with the given value in seconds since unix epoch. */
|
---|
59 | static Timestamp absSeconds(int64_t secTimestamp) RT_NOEXCEPT
|
---|
60 | {
|
---|
61 | RTTIMESPEC Tmp;
|
---|
62 | return Timestamp(RTTimeSpecSetSeconds(&Tmp, secTimestamp));
|
---|
63 | }
|
---|
64 |
|
---|
65 | Timestamp &addSeconds(int64_t cSecs) RT_NOEXCEPT
|
---|
66 | {
|
---|
67 | RTTimeSpecAddSeconds(&m_TimeSpec, cSecs);
|
---|
68 | return *this;
|
---|
69 | }
|
---|
70 |
|
---|
71 | Timestamp &subSeconds(int64_t cSecs) RT_NOEXCEPT
|
---|
72 | {
|
---|
73 | RTTimeSpecSubSeconds(&m_TimeSpec, cSecs);
|
---|
74 | return *this;
|
---|
75 | }
|
---|
76 |
|
---|
77 | RTTIMESPEC *getAbsTimeSpec(RTTIMESPEC *pTime) const RT_NOEXCEPT
|
---|
78 | {
|
---|
79 | *pTime = m_TimeSpec;
|
---|
80 | return pTime;
|
---|
81 | }
|
---|
82 |
|
---|
83 | int64_t getAbsSeconds() const RT_NOEXCEPT
|
---|
84 | {
|
---|
85 | return RTTimeSpecGetSeconds(&m_TimeSpec);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /** Only for log formatting. */
|
---|
89 | size_t strFormatHelper(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput) const RT_NOEXCEPT;
|
---|
90 |
|
---|
91 | int compare(const Timestamp &a_rRight) const RT_NOEXCEPT
|
---|
92 | {
|
---|
93 | return RTTimeSpecCompare(&m_TimeSpec, &a_rRight.m_TimeSpec);
|
---|
94 | }
|
---|
95 |
|
---|
96 | friend bool operator<( const Timestamp &, const Timestamp &) RT_NOEXCEPT;
|
---|
97 | friend bool operator>( const Timestamp &, const Timestamp &) RT_NOEXCEPT;
|
---|
98 | friend bool operator==(const Timestamp &, const Timestamp &) RT_NOEXCEPT;
|
---|
99 | friend bool operator!=(const Timestamp &, const Timestamp &) RT_NOEXCEPT;
|
---|
100 | friend bool operator<=(const Timestamp &, const Timestamp &) RT_NOEXCEPT;
|
---|
101 | friend bool operator>=(const Timestamp &, const Timestamp &) RT_NOEXCEPT;
|
---|
102 | };
|
---|
103 |
|
---|
104 |
|
---|
105 | inline bool operator<( const Timestamp &l, const Timestamp &r) RT_NOEXCEPT { return l.compare(r) < 0; }
|
---|
106 | inline bool operator>( const Timestamp &l, const Timestamp &r) RT_NOEXCEPT { return l.compare(r) > 0; }
|
---|
107 | inline bool operator==(const Timestamp &l, const Timestamp &r) RT_NOEXCEPT { return l.compare(r) == 0; }
|
---|
108 | inline bool operator!=(const Timestamp &l, const Timestamp &r) RT_NOEXCEPT { return l.compare(r) != 0; }
|
---|
109 | inline bool operator<=(const Timestamp &l, const Timestamp &r) RT_NOEXCEPT { return l.compare(r) <= 0; }
|
---|
110 | inline bool operator>=(const Timestamp &l, const Timestamp &r) RT_NOEXCEPT { return l.compare(r) >= 0; }
|
---|
111 |
|
---|
112 | #endif /* !VBOX_INCLUDED_SRC_Dhcpd_Timestamp_h */
|
---|