VirtualBox

Changeset 79526 in vbox


Ignore:
Timestamp:
Jul 4, 2019 10:54:56 AM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
131810
Message:

Dhcpd: Use RTTimeNow instead of the weird RTTimeNanoTS + RTTimeNow conversion combo for implementing timestamps. bugref:9288

Location:
trunk/src/VBox/NetworkServices/Dhcpd
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/NetworkServices/Dhcpd/Db.cpp

    r79524 r79526  
    100100
    101101        Timestamp tsIssued = b->issued();
    102         cb += tsIssued.absStrFormat(pfnOutput, pvArgOutput);
     102        cb += tsIssued.strFormatHelper(pfnOutput, pvArgOutput);
    103103
    104104        cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
     
    108108        Timestamp tsValid = b->issued();
    109109        tsValid.addSeconds(b->leaseTime());
    110         cb += tsValid.absStrFormat(pfnOutput, pvArgOutput);
     110        cb += tsValid.strFormatHelper(pfnOutput, pvArgOutput);
    111111    }
    112112
  • trunk/src/VBox/NetworkServices/Dhcpd/Timestamp.cpp

    r79524 r79526  
    2323#include "Timestamp.h"
    2424
    25 #include <iprt/string.h>
    2625
     26size_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}
    2735
    28 size_t Timestamp::absStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput) const
    29 {
    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  
    2626
    2727/**
    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.
    3029 *
    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.
    3235 */
    3336class Timestamp
    3437{
    35     int64_t m_ns;
     38    RTTIMESPEC m_TimeSpec;
    3639
    3740public:
    3841    Timestamp()
    39       : m_ns(0)
    40     {}
     42    {
     43        RTTimeSpecSetNano(&m_TimeSpec, 0);
     44    }
    4145
    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    }
    4550
     51    /** Get a timestamp initialized to current time. */
    4652    static Timestamp now()
    4753    {
    48         return Timestamp(RTTimeNanoTS());
     54        RTTIMESPEC Tmp;
     55        return Timestamp(RTTimeNow(&Tmp));
    4956    }
    5057
    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)
    5260    {
    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));
    5963    }
    6064
    6165    Timestamp &addSeconds(int64_t cSecs)
    6266    {
    63         m_ns += cSecs * RT_NS_1SEC;
     67        RTTimeSpecAddSeconds(&m_TimeSpec, cSecs);
    6468        return *this;
    6569    }
     
    6771    Timestamp &subSeconds(int64_t cSecs)
    6872    {
    69         m_ns -= cSecs * RT_NS_1SEC;
     73        RTTimeSpecSubSeconds(&m_TimeSpec, cSecs);
    7074        return *this;
    7175    }
    7276
    73 
    7477    RTTIMESPEC *getAbsTimeSpec(RTTIMESPEC *pTime) const
    7578    {
    76         RTTimeNow(pTime);
    77 
    78         uint64_t stampNow = RTTimeNanoTS();
    79         uint64_t delta = stampNow - m_ns;
    80         RTTimeSpecSubNano(pTime, delta);
     79        *pTime = m_TimeSpec;
    8180        return pTime;
    8281    }
     
    8483    int64_t getAbsSeconds() const
    8584    {
    86         RTTIMESPEC time;
    87         return RTTimeSpecGetSeconds(getAbsTimeSpec(&time));
     85        return RTTimeSpecGetSeconds(&m_TimeSpec);
    8886    }
    8987
    90     size_t absStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput) const;
     88    /** Only for log formatting. */
     89    size_t strFormatHelper(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput) const;
    9190
    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 &);
    96102};
    97103
    98104
    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; }
     105inline bool operator<( const Timestamp &l, const Timestamp &r) { return l.compare(r) < 0; }
     106inline bool operator>( const Timestamp &l, const Timestamp &r) { return l.compare(r) > 0; }
     107inline bool operator==(const Timestamp &l, const Timestamp &r) { return l.compare(r) == 0; }
     108inline bool operator!=(const Timestamp &l, const Timestamp &r) { return l.compare(r) != 0; }
     109inline bool operator<=(const Timestamp &l, const Timestamp &r) { return l.compare(r) <= 0; }
     110inline bool operator>=(const Timestamp &l, const Timestamp &r) { return l.compare(r) >= 0; }
    103111
    104112#endif /* !VBOX_INCLUDED_SRC_Dhcpd_TimeStamp_h */
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette