2012-02-06 16:44:09 +01:00
|
|
|
/*
|
2013-05-29 18:43:35 +02:00
|
|
|
* This file is part of the Owl Positioning System (OwlPS) project.
|
|
|
|
* It is subject to the copyright notice and license terms in the
|
|
|
|
* COPYRIGHT.t2t file found in the top-level directory of this
|
|
|
|
* distribution and at
|
2016-11-03 05:10:34 +01:00
|
|
|
* https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
|
2013-05-29 18:43:35 +02:00
|
|
|
* No part of the OwlPS Project, including this file, may be copied,
|
|
|
|
* modified, propagated, or distributed except according to the terms
|
|
|
|
* contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
|
|
|
|
* distributed along with this file, either separately or by replacing
|
2013-06-22 23:30:17 +02:00
|
|
|
* this notice by the COPYRIGHT.t2t file's contents.
|
2012-02-06 16:44:09 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-02-03 17:03:31 +01:00
|
|
|
#ifndef _OWLPS_POSITIONING_TIMESTAMP_HH_
|
|
|
|
#define _OWLPS_POSITIONING_TIMESTAMP_HH_
|
|
|
|
|
2011-03-19 14:42:57 +01:00
|
|
|
#include <owlps.h>
|
|
|
|
|
2010-02-03 17:03:31 +01:00
|
|
|
#include <ctime>
|
|
|
|
#include <stdint.h> // <cstdint> is not C++ 98 compliant
|
|
|
|
#include <ostream>
|
|
|
|
|
2011-03-16 11:23:24 +01:00
|
|
|
/// Represents a timestamp with a nanosecond precision
|
2010-02-03 17:03:31 +01:00
|
|
|
/**
|
2010-03-05 17:24:18 +01:00
|
|
|
* This class boxes <em>struct timespec</em> from ctime (time.h) to
|
|
|
|
* integrate time manipulation functions.
|
2010-02-03 17:03:31 +01:00
|
|
|
*/
|
|
|
|
class Timestamp
|
|
|
|
{
|
2012-06-04 18:07:47 +02:00
|
|
|
private:
|
|
|
|
static Timestamp current_time ;
|
|
|
|
|
2010-02-03 17:03:31 +01:00
|
|
|
protected:
|
|
|
|
/// Time data
|
|
|
|
struct timespec timestamp ;
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
/** @name Internal accessors */
|
|
|
|
//@{
|
|
|
|
void set(const struct timespec &source) ;
|
2011-03-19 14:42:57 +01:00
|
|
|
void set(const owl_timestamp &source) ;
|
2011-03-16 11:33:55 +01:00
|
|
|
void set(const uint_fast32_t source_s, const uint_fast32_t source_ns) ;
|
2011-03-16 11:23:24 +01:00
|
|
|
/// Initialises the Timestamp with a value in milliseconds (deprecated)
|
2010-03-01 16:52:37 +01:00
|
|
|
void set(const uint64_t source_ms) ;
|
|
|
|
//@}
|
|
|
|
|
|
|
|
/** @name Comparison functions */
|
|
|
|
//@{
|
|
|
|
bool equals(const struct timespec &source) const ;
|
2011-06-20 12:11:20 +02:00
|
|
|
/// Compares to a millisecond value (deprecated)
|
2010-03-01 16:52:37 +01:00
|
|
|
bool equals(const uint64_t source) const ;
|
2012-06-08 12:05:32 +02:00
|
|
|
bool before(const struct timespec &source) const ;
|
|
|
|
bool after(const struct timespec &source) const ;
|
2010-03-01 16:52:37 +01:00
|
|
|
//@}
|
|
|
|
|
2010-02-03 17:03:31 +01:00
|
|
|
/** @name Operations */
|
|
|
|
//@{
|
2011-03-16 11:23:24 +01:00
|
|
|
/// Lowers the precision of #timestamp to ms (deprecated)
|
2010-02-03 17:03:31 +01:00
|
|
|
void round_to_ms(void) ;
|
|
|
|
//@}
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
Timestamp(void) ;
|
2016-11-04 15:30:03 +01:00
|
|
|
explicit Timestamp(const struct timespec &source);
|
2016-11-04 16:01:25 +01:00
|
|
|
explicit Timestamp(const owl_timestamp &source);
|
2011-03-16 11:33:55 +01:00
|
|
|
Timestamp(const uint_fast32_t source_s, const uint_fast32_t source_ns) ;
|
2011-03-16 11:23:24 +01:00
|
|
|
/// Constructs a Timsestamp from a value in milliseconds (deprecated)
|
2016-11-04 15:30:03 +01:00
|
|
|
explicit Timestamp(const uint64_t source);
|
2010-02-03 17:03:31 +01:00
|
|
|
Timestamp(const Timestamp &source) ;
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
~Timestamp(void) {}
|
2010-02-03 17:03:31 +01:00
|
|
|
|
2011-12-28 12:58:20 +01:00
|
|
|
/** @name Operations */
|
|
|
|
//@{
|
2012-06-04 18:07:47 +02:00
|
|
|
/// Returns a Timestamp containing the current time
|
|
|
|
static Timestamp get_current_time(void) ;
|
|
|
|
/// Sets #current_time to a new timestamp
|
2012-06-08 12:17:25 +02:00
|
|
|
static void update_current_time(const Timestamp &source) ;
|
2013-06-04 00:39:10 +02:00
|
|
|
/// Initialises #timestamp at the current time whith a nanosecond
|
|
|
|
/// precision
|
2010-02-03 17:03:31 +01:00
|
|
|
bool now(void) ;
|
2011-12-28 12:58:20 +01:00
|
|
|
/// Returns the time elapsed until now
|
|
|
|
Timestamp elapsed(void) const ;
|
2013-05-30 21:23:05 +02:00
|
|
|
/// Returns the time elapsed since or until `source`
|
2011-12-28 12:58:20 +01:00
|
|
|
Timestamp elapsed(const Timestamp &source) const ;
|
|
|
|
/// Resets #timestamp
|
2010-03-01 16:52:37 +01:00
|
|
|
void clear(void) ;
|
2011-12-28 12:58:20 +01:00
|
|
|
//@}
|
2010-02-03 17:03:31 +01:00
|
|
|
|
|
|
|
/** @name Operators */
|
|
|
|
//@{
|
2011-07-30 20:38:42 +02:00
|
|
|
Timestamp& operator=(const Timestamp &source) ;
|
|
|
|
Timestamp& operator=(const struct timespec &source) ;
|
|
|
|
Timestamp& operator=(const uint64_t source) ; //< deprecated
|
2010-02-03 17:03:31 +01:00
|
|
|
bool operator==(const Timestamp &source) const ;
|
2010-03-01 16:52:37 +01:00
|
|
|
bool operator==(const struct timespec &source) const ;
|
2011-03-16 11:23:24 +01:00
|
|
|
bool operator==(const uint64_t source) const ; //< deprecated
|
2010-02-03 17:03:31 +01:00
|
|
|
bool operator!=(const Timestamp &source) const ;
|
2010-03-01 16:52:37 +01:00
|
|
|
bool operator!=(const struct timespec &source) const ;
|
2011-03-16 11:23:24 +01:00
|
|
|
bool operator!=(const uint64_t source) const ; //< deprecated
|
2010-02-03 17:03:31 +01:00
|
|
|
bool operator<(const Timestamp &source) const ;
|
2010-03-01 16:52:37 +01:00
|
|
|
bool operator<(const struct timespec &source) const ;
|
2011-03-16 11:23:24 +01:00
|
|
|
bool operator<(const uint64_t source) const ; //< deprecated
|
2010-02-03 17:03:31 +01:00
|
|
|
bool operator>(const Timestamp &source) const ;
|
2010-03-01 16:52:37 +01:00
|
|
|
bool operator>(const struct timespec &source) const ;
|
2011-03-16 11:23:24 +01:00
|
|
|
bool operator>(const uint64_t source) const ; //< deprecated
|
2010-02-03 17:03:31 +01:00
|
|
|
bool operator<=(const Timestamp &source) const ;
|
2010-03-01 16:52:37 +01:00
|
|
|
bool operator<=(const struct timespec &source) const ;
|
2011-03-16 11:23:24 +01:00
|
|
|
bool operator<=(const uint64_t source) const ; //< deprecated
|
2010-02-03 17:03:31 +01:00
|
|
|
bool operator>=(const Timestamp &source) const ;
|
2010-03-01 16:52:37 +01:00
|
|
|
bool operator>=(const struct timespec &source) const ;
|
2011-03-16 11:23:24 +01:00
|
|
|
bool operator>=(const uint64_t source) const ; //< deprecated
|
2010-03-01 16:52:37 +01:00
|
|
|
operator bool(void) const ;
|
|
|
|
operator const struct timespec&(void) const ;
|
2011-06-20 12:11:20 +02:00
|
|
|
/// Casts to milliseconds
|
2010-03-01 16:52:37 +01:00
|
|
|
operator uint64_t(void) const ;
|
2011-12-28 12:58:20 +01:00
|
|
|
/// Casts to the owl_timestamp type from libowlps
|
|
|
|
operator owl_timestamp(void) const ;
|
2010-02-03 17:03:31 +01:00
|
|
|
//@}
|
|
|
|
|
2011-06-20 12:11:20 +02:00
|
|
|
/// Displays a Timestamp
|
2010-02-03 17:03:31 +01:00
|
|
|
friend std::ostream& operator<<(std::ostream &os, const Timestamp &t) ;
|
2010-06-07 15:09:22 +02:00
|
|
|
|
2011-06-20 12:11:20 +02:00
|
|
|
// Hashes a Timestamp
|
2010-06-07 15:09:22 +02:00
|
|
|
friend size_t hash_value(const Timestamp &source) ;
|
2010-02-03 17:03:31 +01:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
inline void Timestamp::clear(void)
|
|
|
|
{
|
|
|
|
timestamp.tv_sec = 0 ;
|
|
|
|
timestamp.tv_nsec = 0 ;
|
|
|
|
}
|
2010-02-03 17:03:31 +01:00
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
|
|
|
|
/* *** Operators *** */
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Timestamp::operator==(const struct timespec &source) const
|
|
|
|
{
|
|
|
|
return equals(source) ;
|
|
|
|
}
|
2010-02-03 17:03:31 +01:00
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
inline bool Timestamp::operator==(const uint64_t source) const
|
2010-02-03 17:03:31 +01:00
|
|
|
{
|
2010-03-01 16:52:37 +01:00
|
|
|
return equals(source) ;
|
2010-02-03 17:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
inline bool Timestamp::operator!=(const Timestamp &source) const
|
|
|
|
{
|
|
|
|
return !(*this == source) ;
|
|
|
|
}
|
2010-02-03 17:03:31 +01:00
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
|
|
|
|
inline bool Timestamp::operator!=(const struct timespec &source) const
|
|
|
|
{
|
|
|
|
return !(*this == source) ;
|
|
|
|
}
|
2010-02-03 17:03:31 +01:00
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
inline bool Timestamp::operator!=(const uint64_t source) const
|
2010-02-03 17:03:31 +01:00
|
|
|
{
|
|
|
|
return !(*this == source) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
inline bool Timestamp::operator<(const Timestamp &source) const
|
|
|
|
{
|
2012-06-08 12:05:32 +02:00
|
|
|
return before(source.timestamp) ;
|
2010-03-01 16:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Timestamp::operator<(const struct timespec &source) const
|
|
|
|
{
|
2012-06-08 12:05:32 +02:00
|
|
|
return before(source) ;
|
2010-03-01 16:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Timestamp::operator<(const uint64_t source) const
|
|
|
|
{
|
|
|
|
return static_cast<uint64_t>(*this) < source ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-03 17:03:31 +01:00
|
|
|
inline bool Timestamp::operator>(const Timestamp &source) const
|
|
|
|
{
|
2012-06-08 12:05:32 +02:00
|
|
|
return after(source.timestamp) ;
|
2010-03-01 16:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Timestamp::operator>(const struct timespec &source) const
|
|
|
|
{
|
2012-06-08 12:05:32 +02:00
|
|
|
return after(source) ;
|
2010-03-01 16:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Timestamp::operator>(const uint64_t source) const
|
|
|
|
{
|
|
|
|
return static_cast<uint64_t>(*this) > source ;
|
2010-02-03 17:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Timestamp::operator<=(const Timestamp &source) const
|
|
|
|
{
|
|
|
|
return *this == source || *this < source ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
inline bool Timestamp::operator<=(const struct timespec &source) const
|
|
|
|
{
|
|
|
|
return *this == source || *this < source ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Timestamp::operator<=(const uint64_t source) const
|
|
|
|
{
|
|
|
|
return *this == source || *this < source ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-03 17:03:31 +01:00
|
|
|
inline bool Timestamp::operator>=(const Timestamp &source) const
|
|
|
|
{
|
|
|
|
return source <= *this ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
inline bool Timestamp::operator>=(const struct timespec &source) const
|
|
|
|
{
|
|
|
|
return *this == source || !(*this < source) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Timestamp::operator>=(const uint64_t source) const
|
|
|
|
{
|
|
|
|
return *this == source || !(*this < source) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline Timestamp::operator bool() const
|
|
|
|
{
|
2011-04-26 14:14:01 +02:00
|
|
|
return timestamp.tv_sec > 0 || timestamp.tv_nsec > 0 ;
|
2010-03-01 16:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline Timestamp::operator const struct timespec&() const
|
|
|
|
{
|
|
|
|
return timestamp ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline Timestamp::operator uint64_t(void) const
|
|
|
|
{
|
2010-03-31 11:09:55 +02:00
|
|
|
return
|
|
|
|
static_cast<uint64_t>(timestamp.tv_sec) * 1000 +
|
|
|
|
static_cast<uint64_t>(timestamp.tv_nsec) / 1000000 ;
|
2010-03-01 16:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-30 20:13:44 +02:00
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template<> struct hash<Timestamp>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
size_t operator()(const Timestamp &source) const
|
|
|
|
{
|
|
|
|
hash<uint64_t> h ;
|
|
|
|
return h(source) ;
|
|
|
|
}
|
|
|
|
} ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-03 17:03:31 +01:00
|
|
|
#endif // _OWLPS_POSITIONING_TIMESTAMP_HH_
|