1 | /* $Id: Timestamp.h 79524 2019-07-04 10:14:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DHCP server - timestamps
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2019 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 | * Timestamp API uses unsigned time, but we need to be able to refer
|
---|
29 | * to events in the past. Hide the ugly convertions.
|
---|
30 | *
|
---|
31 | * @todo r=bird: Unnecessary mixing of RTTimeNanoTS and RTTimeNow.
|
---|
32 | */
|
---|
33 | class Timestamp
|
---|
34 | {
|
---|
35 | int64_t m_ns;
|
---|
36 |
|
---|
37 | public:
|
---|
38 | Timestamp()
|
---|
39 | : m_ns(0)
|
---|
40 | {}
|
---|
41 |
|
---|
42 | Timestamp(uint64_t ns)
|
---|
43 | : m_ns(static_cast<int64_t>(ns))
|
---|
44 | {}
|
---|
45 |
|
---|
46 | static Timestamp now()
|
---|
47 | {
|
---|
48 | return Timestamp(RTTimeNanoTS());
|
---|
49 | }
|
---|
50 |
|
---|
51 | static Timestamp absSeconds(int64_t sec)
|
---|
52 | {
|
---|
53 | RTTIMESPEC delta;
|
---|
54 | RTTimeNow(&delta);
|
---|
55 | RTTimeSpecSubSeconds(&delta, sec);
|
---|
56 |
|
---|
57 | uint64_t stampNow = RTTimeNanoTS();
|
---|
58 | return Timestamp(stampNow - RTTimeSpecGetNano(&delta));
|
---|
59 | }
|
---|
60 |
|
---|
61 | Timestamp &addSeconds(int64_t cSecs)
|
---|
62 | {
|
---|
63 | m_ns += cSecs * RT_NS_1SEC;
|
---|
64 | return *this;
|
---|
65 | }
|
---|
66 |
|
---|
67 | Timestamp &subSeconds(int64_t cSecs)
|
---|
68 | {
|
---|
69 | m_ns -= cSecs * RT_NS_1SEC;
|
---|
70 | return *this;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | RTTIMESPEC *getAbsTimeSpec(RTTIMESPEC *pTime) const
|
---|
75 | {
|
---|
76 | RTTimeNow(pTime);
|
---|
77 |
|
---|
78 | uint64_t stampNow = RTTimeNanoTS();
|
---|
79 | uint64_t delta = stampNow - m_ns;
|
---|
80 | RTTimeSpecSubNano(pTime, delta);
|
---|
81 | return pTime;
|
---|
82 | }
|
---|
83 |
|
---|
84 | int64_t getAbsSeconds() const
|
---|
85 | {
|
---|
86 | RTTIMESPEC time;
|
---|
87 | return RTTimeSpecGetSeconds(getAbsTimeSpec(&time));
|
---|
88 | }
|
---|
89 |
|
---|
90 | size_t absStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput) const;
|
---|
91 |
|
---|
92 | friend bool operator<(const Timestamp &l, const Timestamp &r);
|
---|
93 | friend bool operator>(const Timestamp &l, const Timestamp &r);
|
---|
94 | friend bool operator<=(const Timestamp &l, const Timestamp &r);
|
---|
95 | friend bool operator>=(const Timestamp &l, const Timestamp &r);
|
---|
96 | };
|
---|
97 |
|
---|
98 |
|
---|
99 | inline bool operator<(const Timestamp &l, const Timestamp &r) { return l.m_ns < r.m_ns; }
|
---|
100 | inline bool operator>(const Timestamp &l, const Timestamp &r) { return l.m_ns > r.m_ns; }
|
---|
101 | inline bool operator<=(const Timestamp &l, const Timestamp &r) { return l.m_ns <= r.m_ns; }
|
---|
102 | inline bool operator>=(const Timestamp &l, const Timestamp &r) { return l.m_ns >= r.m_ns; }
|
---|
103 |
|
---|
104 | #endif /* !VBOX_INCLUDED_SRC_Dhcpd_TimeStamp_h */
|
---|