📄 timestamp.hh
字号:
The current time is measured in seconds since January 1, 1970 GMT. Returns the most precise timestamp available. @sa now() */inline voidTimestamp::set_now(){#if TIMESTAMP_NANOSEC && (CLICK_LINUXMODULE || CLICK_BSDMODULE || HAVE_USE_CLOCK_GETTIME) // nanosecond precision# if TIMESTAMP_PUNS_TIMESPEC struct timespec *tsp = (struct timespec *) this;# else struct timespec ts, *tsp = &ts;# endif# if CLICK_LINUXMODULE getnstimeofday(tsp);# elif CLICK_BSDMODULE nanotime(tsp); // This is the more precise function# elif CLICK_USERLEVEL || CLICK_TOOL clock_gettime(CLOCK_REALTIME, tsp);# else# error "unknown driver"# endif# if !TIMESTAMP_PUNS_TIMESPEC assign(ts.tv_sec, nsec_to_subsec(ts.tv_nsec));# endif#else // microsecond precision# if TIMESTAMP_PUNS_TIMEVAL struct timeval *tvp = (struct timeval *) this;# else struct timeval tv, *tvp = &tv;# endif# if CLICK_LINUXMODULE do_gettimeofday(tvp);# elif CLICK_BSDMODULE microtime(tvp);# elif CLICK_NS simclick_gettimeofday(tvp);# elif CLICK_USERLEVEL || CLICK_TOOL gettimeofday(tvp, (struct timezone *) 0);# else# error "unknown driver"# endif# if !TIMESTAMP_PUNS_TIMEVAL assign(tv.tv_sec, usec_to_subsec(tv.tv_usec));# endif#endif}/** @brief Return a timestamp representing the current time. The current time is measured in seconds since January 1, 1970 GMT. @sa set_now() */inline TimestampTimestamp::now(){ Timestamp t = Timestamp::uninitialized_t(); t.set_now(); return t;}/** @brief Set this timestamp's seconds component. The subseconds component is left unchanged. */inline voidTimestamp::set_sec(seconds_type sec){#if TIMESTAMP_REP_FLAT64 uint32_t ss = subsec(); _t.x = (int64_t) sec * subsec_per_sec + ss;#else _t.sec = sec;#endif}/** @brief Set this timestamp's subseconds component. @param subsec number of subseconds The seconds component is left unchanged. */inline voidTimestamp::set_subsec(uint32_t subsec){#if TIMESTAMP_REP_FLAT64 seconds_type s = sec(); _t.x = (int64_t) s * subsec_per_sec + subsec;#else _t.subsec = subsec;#endif}/** @brief Return this timestamp's seconds component. */inline Timestamp::seconds_typeTimestamp::sec() const{#if TIMESTAMP_REP_FLAT64 if (unlikely(_t.x < 0)) return -value_div(-_t.x - 1, subsec_per_sec) - 1; else return value_div(_t.x, subsec_per_sec);#else return _t.sec;#endif}/** @brief Return this timestamp's subseconds component. */inline uint32_tTimestamp::subsec() const{#if TIMESTAMP_REP_FLAT64 return _t.x - sec() * subsec_per_sec;#else return _t.subsec;#endif}/** @brief Return this timestamp's subseconds component, converted to milliseconds. */inline uint32_tTimestamp::msec() const{ return subsec_to_msec(subsec());}/** @brief Return this timestamp's subseconds component, converted to microseconds. */inline uint32_tTimestamp::usec() const{ return subsec_to_usec(subsec());}/** @brief Return this timestamp's subseconds component, converted to nanoseconds. */inline uint32_tTimestamp::nsec() const{ return subsec_to_nsec(subsec());}/** @brief Return this timestamp's interval length, converted to milliseconds. Will overflow on intervals of more than 2147483.647 seconds. */inline Timestamp::seconds_typeTimestamp::msec1() const{#if TIMESTAMP_REP_FLAT64 return value_div(_t.x, subsec_per_sec / msec_per_sec);#else return _t.sec * msec_per_sec + subsec_to_msec(_t.subsec);#endif}/** @brief Return this timestamp's interval length, converted to microseconds. Will overflow on intervals of more than 2147.483647 seconds. */inline Timestamp::seconds_typeTimestamp::usec1() const{#if TIMESTAMP_REP_FLAT64 return value_div(_t.x, subsec_per_sec / usec_per_sec);#else return _t.sec * usec_per_sec + subsec_to_usec(_t.subsec);#endif}/** @brief Return this timestamp's interval length, converted to nanoseconds. Will overflow on intervals of more than 2.147483647 seconds. */inline Timestamp::seconds_typeTimestamp::nsec1() const{#if TIMESTAMP_REP_FLAT64 return _t.x * (nsec_per_sec / subsec_per_sec);#else return _t.sec * nsec_per_sec + subsec_to_nsec(_t.subsec);#endif}#if TIMESTAMP_PUNS_TIMEVALinline const struct timeval &Timestamp::timeval() const{ return *(const struct timeval*) this;}#else/** @brief Return a struct timeval with the same value as this timestamp. If Timestamp and struct timeval have the same size and representation, then this operation returns a "const struct timeval &" whose address is the same as this Timestamp. */inline struct timevalTimestamp::timeval() const{ struct timeval tv; tv.tv_sec = sec(); tv.tv_usec = usec(); return tv;}#endif#if HAVE_STRUCT_TIMESPEC# if TIMESTAMP_PUNS_TIMESPECinline const struct timespec &Timestamp::timespec() const{ return *(const struct timespec*) this;}# else/** @brief Return a struct timespec with the same value as this timestamp. If Timestamp and struct timespec have the same size and representation, then this operation returns a "const struct timespec &" whose address is the same as this Timestamp. */inline struct timespecTimestamp::timespec() const{ struct timespec tv; tv.tv_sec = sec(); tv.tv_nsec = nsec(); return tv;}# endif#endif#if !CLICK_TOOL/** @brief Returns this timestamp, converted to a jiffies value. */inline click_jiffies_tTimestamp::jiffies() const{# if TIMESTAMP_REP_FLAT64 // This is not very precise when CLICK_HZ doesn't divide NSUBSEC evenly. return value_div(_t.x, subsec_per_sec / CLICK_HZ);# else click_jiffies_t j = ((click_jiffies_t) sec()) * CLICK_HZ;# if CLICK_HZ == 100 || CLICK_HZ == 1000 || CLICK_HZ == 10000 || CLICK_HZ == 100000 || CLICK_HZ == 1000000 return j + ((click_jiffies_t) subsec()) / (subsec_per_sec / CLICK_HZ);# else // This is not very precise when CLICK_HZ doesn't divide NSUBSEC evenly. return j + ((click_jiffies_t) subsec()) / (subsec_per_sec / CLICK_HZ);# endif# endif}inline TimestampTimestamp::make_jiffies(click_jiffies_t jiffies){# if TIMESTAMP_REP_FLAT64 // Not very precise when CLICK_HZ doesn't evenly divide subsec_per_sec. Timestamp t = Timestamp::uninitialized_t(); t._t.x = (int64_t) jiffies * (subsec_per_sec / CLICK_HZ); return t;# else# if CLICK_HZ == 100 || CLICK_HZ == 1000 || CLICK_HZ == 10000 || CLICK_HZ == 100000 || CLICK_HZ == 1000000 uint32_t sec = jiffies / CLICK_HZ; uint32_t subsec = (jiffies - sec * CLICK_HZ) * (subsec_per_sec / CLICK_HZ); return Timestamp(sec, subsec);# else // Not very precise when CLICK_HZ doesn't evenly divide subsec_per_sec. uint32_t sec = jiffies / CLICK_HZ; uint32_t subsec = (jiffies - sec * CLICK_HZ) * (subsec_per_sec / CLICK_HZ); return Timestamp(sec, subsec);# endif# endif}#endifinline void Timestamp::set(seconds_type sec, uint32_t subsec) { assign(sec, subsec);}inline void Timestamp::set_usec(seconds_type sec, uint32_t usec) { assign_usec(sec, usec);}inline void Timestamp::set_nsec(seconds_type sec, uint32_t nsec) { assign_nsec(sec, nsec);}/** @relates Timestamp @brief Compare two timestamps for equality. Returns true iff the two operands have the same seconds and subseconds components. */inline booloperator==(const Timestamp &a, const Timestamp &b){#if TIMESTAMP_REP_FLAT64 || TIMESTAMP_MATH_FLAT64 return a._t.x == b._t.x;#else return a.sec() == b.sec() && a.subsec() == b.subsec();#endif}/** @relates Timestamp @brief Compare two timestamps for inequality. Returns true iff !(@a a == @a b). */inline booloperator!=(const Timestamp &a, const Timestamp &b){ return !(a == b);}/** @relates Timestamp @brief Compare two timestamps. Returns true iff @a a represents a shorter interval than @a b, or considered as absolute time, @a a happened before @a b. */inline booloperator<(const Timestamp &a, const Timestamp &b){#if TIMESTAMP_REP_FLAT64 || TIMESTAMP_MATH_FLAT64 return a._t.x < b._t.x;#else return a.sec() < b.sec() || (a.sec() == b.sec() && a.subsec() < b.subsec());#endif}/** @relates Timestamp @brief Compare two timestamps. Returns true iff @a a measures an interval no larger than @a b, or considered as absolute time, @a a happened at or before @a b. */inline booloperator<=(const Timestamp &a, const Timestamp &b){ return !(b < a);}/** @relates Timestamp @brief Compare two timestamps. Returns true iff @a a measures an interval no shorter than @a b, or considered as absolute time, @a a happened at or after @a b. */inline booloperator>=(const Timestamp &a, const Timestamp &b){ return !(a < b);}/** @relates Timestamp @brief Compare two timestamps. Returns true iff @a a measures a longer interval than @a b, or considered as absolute time, @a a happened after @a b. */inline booloperator>(const Timestamp &a, const Timestamp &b){ return b < a;}/** @brief Add @a b to @a a. Returns the result (the new value of @a a). */inline Timestamp &operator+=(Timestamp &a, const Timestamp &b){#if TIMESTAMP_REP_FLAT64 || TIMESTAMP_MATH_FLAT64 a._t.x += b._t.x;#else a._t.sec += b._t.sec; a._t.subsec += b._t.subsec;#endif a.add_fix(); return a;}/** @brief Subtract @a b from @a a. Returns the result (the new value of @a a). */inline Timestamp &operator-=(Timestamp &a, const Timestamp &b){#if TIMESTAMP_REP_FLAT64 || TIMESTAMP_MATH_FLAT64 a._t.x -= b._t.x;#else a._t.sec -= b._t.sec; a._t.subsec -= b._t.subsec;#endif a.sub_fix(); return a;}/** @brief Add the two operands and return the result. */inline Timestampoperator+(Timestamp a, const Timestamp &b){ a += b; return a;}/** @brief Subtract @a b from @a a and return the result. */inline Timestampoperator-(Timestamp a, const Timestamp &b){ a -= b; return a;}/** @brief Negate @a a and return the result. */inline Timestampoperator-(const Timestamp &a){#if TIMESTAMP_REP_FLAT64 Timestamp t = Timestamp::uninitialized_t(); t._t.x = -a._t.x; return t;#else if (a.subsec()) return Timestamp(-a.sec() - 1, Timestamp::subsec_per_sec - a.subsec()); else return Timestamp(-a.sec(), 0);#endif}#if HAVE_FLOAT_TYPES/** @brief Return this timestamp's value, converted to a real number. */inline doubleTimestamp::doubleval() const{# if TIMESTAMP_REP_FLAT64 return _t.x / (double) subsec_per_sec;# else return _t.sec + (_t.subsec / (double) subsec_per_sec);# endif}/** @brief Create a timestamp measuring @a d seconds. */inlineTimestamp::Timestamp(double d){# if TIMESTAMP_REP_FLAT64 _t.x = (int64_t) floor(d * subsec_per_sec + 0.5);# else double dfloor = floor(d); _t.sec = (seconds_type) dfloor; _t.subsec = (uint32_t) ((d - dfloor) * subsec_per_sec + 0.5); add_fix();# endif}/** @brief Scale @a a by a factor of @a b and return the result. */inline Timestampoperator*(const Timestamp &a, double b){ return Timestamp(a.doubleval() * b);}inline Timestampoperator*(const Timestamp &a, int b){ return Timestamp(a.doubleval() * b);}inline Timestampoperator*(const Timestamp &a, unsigned b){ return Timestamp(a.doubleval() * b);}inline Timestampoperator*(double a, const Timestamp &b){ return Timestamp(b.doubleval() * a);}inline Timestampoperator*(int a, const Timestamp &b){ return Timestamp(b.doubleval() * a);}inline Timestampoperator*(unsigned a, const Timestamp &b){ return Timestamp(b.doubleval() * a);}/** @brief Scale @a a down by a factor of @a b and return the result. */inline Timestampoperator/(const Timestamp &a, double b){ return Timestamp(a.doubleval() / b);}inline Timestampoperator/(const Timestamp &a, int b){ return Timestamp(a.doubleval() / b);}inline Timestampoperator/(const Timestamp &a, unsigned b){ return Timestamp(a.doubleval() / b);}/** @brief Divide @a a by @a b and return the result. */inline doubleoperator/(const Timestamp &a, const Timestamp &b){ return a.doubleval() / b.doubleval();}#endif /* HAVE_FLOAT_TYPES */StringAccum& operator<<(StringAccum&, const Timestamp&);CLICK_ENDDECLS#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -