Changeset 79526 in vbox
- Timestamp:
- Jul 4, 2019 10:54:56 AM (6 years ago)
- svn:sync-xref-src-repo-rev:
- 131810
- Location:
- trunk/src/VBox/NetworkServices/Dhcpd
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/NetworkServices/Dhcpd/Db.cpp
r79524 r79526 100 100 101 101 Timestamp tsIssued = b->issued(); 102 cb += tsIssued. absStrFormat(pfnOutput, pvArgOutput);102 cb += tsIssued.strFormatHelper(pfnOutput, pvArgOutput); 103 103 104 104 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, … … 108 108 Timestamp tsValid = b->issued(); 109 109 tsValid.addSeconds(b->leaseTime()); 110 cb += tsValid. absStrFormat(pfnOutput, pvArgOutput);110 cb += tsValid.strFormatHelper(pfnOutput, pvArgOutput); 111 111 } 112 112 -
trunk/src/VBox/NetworkServices/Dhcpd/Timestamp.cpp
r79524 r79526 23 23 #include "Timestamp.h" 24 24 25 #include <iprt/string.h>26 25 26 size_t Timestamp::strFormatHelper(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput) const 27 { 28 RTTIMESPEC TimeSpec; 29 RTTIME Time; 30 char szBuf[64]; 31 ssize_t cchBuf = RTTimeToStringEx(RTTimeExplode(&Time, getAbsTimeSpec(&TimeSpec)), szBuf, sizeof(szBuf), 0); 32 Assert(cchBuf > 0); 33 return pfnOutput(pvArgOutput, szBuf, cchBuf); 34 } 27 35 28 size_t Timestamp::absStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput) const29 {30 RTTIMESPEC Spec;31 getAbsTimeSpec(&Spec);32 33 RTTIME Time;34 RTTimeExplode(&Time, &Spec);35 36 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,37 "%RI32-%02u-%02uT%02u:%02u:%02uZ",38 Time.i32Year, Time.u8Month, Time.u8MonthDay,39 Time.u8Hour, Time.u8Minute, Time.u8Second);40 } -
trunk/src/VBox/NetworkServices/Dhcpd/Timestamp.h
r79524 r79526 26 26 27 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. 28 * Wrapper around RTTIMESPEC. 30 29 * 31 * @todo r=bird: Unnecessary mixing of RTTimeNanoTS and RTTimeNow. 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. 32 35 */ 33 36 class Timestamp 34 37 { 35 int64_t m_ns;38 RTTIMESPEC m_TimeSpec; 36 39 37 40 public: 38 41 Timestamp() 39 : m_ns(0) 40 {} 42 { 43 RTTimeSpecSetNano(&m_TimeSpec, 0); 44 } 41 45 42 Timestamp(uint64_t ns) 43 : m_ns(static_cast<int64_t>(ns)) 44 {} 46 Timestamp(PCRTTIMESPEC a_pTimeSpec) 47 { 48 m_TimeSpec = *a_pTimeSpec; 49 } 45 50 51 /** Get a timestamp initialized to current time. */ 46 52 static Timestamp now() 47 53 { 48 return Timestamp(RTTimeNanoTS()); 54 RTTIMESPEC Tmp; 55 return Timestamp(RTTimeNow(&Tmp)); 49 56 } 50 57 51 static Timestamp absSeconds(int64_t sec) 58 /** Get a timestamp with the given value in seconds since unix epoch. */ 59 static Timestamp absSeconds(int64_t secTimestamp) 52 60 { 53 RTTIMESPEC delta; 54 RTTimeNow(&delta); 55 RTTimeSpecSubSeconds(&delta, sec); 56 57 uint64_t stampNow = RTTimeNanoTS(); 58 return Timestamp(stampNow - RTTimeSpecGetNano(&delta)); 61 RTTIMESPEC Tmp; 62 return Timestamp(RTTimeSpecSetSeconds(&Tmp, secTimestamp)); 59 63 } 60 64 61 65 Timestamp &addSeconds(int64_t cSecs) 62 66 { 63 m_ns += cSecs * RT_NS_1SEC;67 RTTimeSpecAddSeconds(&m_TimeSpec, cSecs); 64 68 return *this; 65 69 } … … 67 71 Timestamp &subSeconds(int64_t cSecs) 68 72 { 69 m_ns -= cSecs * RT_NS_1SEC;73 RTTimeSpecSubSeconds(&m_TimeSpec, cSecs); 70 74 return *this; 71 75 } 72 76 73 74 77 RTTIMESPEC *getAbsTimeSpec(RTTIMESPEC *pTime) const 75 78 { 76 RTTimeNow(pTime); 77 78 uint64_t stampNow = RTTimeNanoTS(); 79 uint64_t delta = stampNow - m_ns; 80 RTTimeSpecSubNano(pTime, delta); 79 *pTime = m_TimeSpec; 81 80 return pTime; 82 81 } … … 84 83 int64_t getAbsSeconds() const 85 84 { 86 RTTIMESPEC time; 87 return RTTimeSpecGetSeconds(getAbsTimeSpec(&time)); 85 return RTTimeSpecGetSeconds(&m_TimeSpec); 88 86 } 89 87 90 size_t absStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput) const; 88 /** Only for log formatting. */ 89 size_t strFormatHelper(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput) const; 91 90 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); 91 int compare(const Timestamp &a_rRight) const 92 { 93 return RTTimeSpecCompare(&m_TimeSpec, &a_rRight.m_TimeSpec); 94 } 95 96 friend bool operator<( const Timestamp &, const Timestamp &); 97 friend bool operator>( const Timestamp &, const Timestamp &); 98 friend bool operator==(const Timestamp &, const Timestamp &); 99 friend bool operator!=(const Timestamp &, const Timestamp &); 100 friend bool operator<=(const Timestamp &, const Timestamp &); 101 friend bool operator>=(const Timestamp &, const Timestamp &); 96 102 }; 97 103 98 104 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; } 105 inline bool operator<( const Timestamp &l, const Timestamp &r) { return l.compare(r) < 0; } 106 inline bool operator>( const Timestamp &l, const Timestamp &r) { return l.compare(r) > 0; } 107 inline bool operator==(const Timestamp &l, const Timestamp &r) { return l.compare(r) == 0; } 108 inline bool operator!=(const Timestamp &l, const Timestamp &r) { return l.compare(r) != 0; } 109 inline bool operator<=(const Timestamp &l, const Timestamp &r) { return l.compare(r) <= 0; } 110 inline bool operator>=(const Timestamp &l, const Timestamp &r) { return l.compare(r) >= 0; } 103 111 104 112 #endif /* !VBOX_INCLUDED_SRC_Dhcpd_TimeStamp_h */
Note:
See TracChangeset
for help on using the changeset viewer.