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