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
|
|
|
|
* http://code.lm7.fr/p/owlps/source/tree/master/COPYRIGHT.t2t
|
|
|
|
* 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
|
|
|
#include "timestamp.hh"
|
2012-06-08 12:17:25 +02:00
|
|
|
#include "configuration.hh"
|
2010-02-03 17:03:31 +01:00
|
|
|
|
2012-06-08 13:11:39 +02:00
|
|
|
#include <iostream>
|
2013-07-05 18:43:24 +02:00
|
|
|
#include <iomanip>
|
2010-06-07 15:09:22 +02:00
|
|
|
|
2012-06-08 13:11:39 +02:00
|
|
|
using namespace std ;
|
2010-02-03 17:03:31 +01:00
|
|
|
|
|
|
|
|
2012-06-04 18:07:47 +02:00
|
|
|
Timestamp Timestamp::current_time ;
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-03 17:03:31 +01:00
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
|
|
|
|
|
|
Timestamp::Timestamp()
|
|
|
|
{
|
2010-03-01 16:52:37 +01:00
|
|
|
clear() ;
|
2010-02-03 17:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timestamp::Timestamp(const struct timespec &source)
|
|
|
|
{
|
2010-03-01 16:52:37 +01:00
|
|
|
set(source) ;
|
2010-02-03 17:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-19 14:42:57 +01:00
|
|
|
Timestamp::Timestamp(const owl_timestamp &source)
|
|
|
|
{
|
|
|
|
set(source) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-16 11:33:55 +01:00
|
|
|
Timestamp::Timestamp(const uint_fast32_t source_s,
|
|
|
|
const uint_fast32_t source_ns)
|
|
|
|
{
|
|
|
|
set(source_s, source_ns) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
Timestamp::Timestamp(const uint64_t source)
|
2010-02-03 17:03:31 +01:00
|
|
|
{
|
2010-03-01 16:52:37 +01:00
|
|
|
set(source) ;
|
2010-02-03 17:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timestamp::Timestamp(const Timestamp &source)
|
|
|
|
{
|
2010-03-01 16:52:37 +01:00
|
|
|
set(source.timestamp) ;
|
2010-02-03 17:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
/* *** Internal accessors *** */
|
2010-02-03 17:03:31 +01:00
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
inline void Timestamp::set(const struct timespec &source)
|
2010-02-03 17:03:31 +01:00
|
|
|
{
|
2010-03-01 16:52:37 +01:00
|
|
|
timestamp = source ;
|
2011-03-19 14:42:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void Timestamp::set(const owl_timestamp &source)
|
|
|
|
{
|
|
|
|
set(source.tv_sec, source.tv_nsec) ;
|
2010-02-03 17:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-16 11:33:55 +01:00
|
|
|
inline void Timestamp::set(const uint_fast32_t source_s,
|
|
|
|
const uint_fast32_t source_ns)
|
|
|
|
{
|
|
|
|
timestamp.tv_sec = source_s ;
|
|
|
|
timestamp.tv_nsec = source_ns ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
inline void Timestamp::set(const uint64_t source_ms)
|
2010-02-03 17:03:31 +01:00
|
|
|
{
|
|
|
|
timestamp.tv_sec = source_ms / 1000 ;
|
|
|
|
timestamp.tv_nsec = (source_ms - timestamp.tv_sec * 1000) * 1000000 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
|
|
|
2012-06-04 18:07:47 +02:00
|
|
|
Timestamp Timestamp::get_current_time()
|
|
|
|
{
|
2012-06-08 19:08:56 +02:00
|
|
|
if (! Configuration::bool_value("replay"))
|
2012-06-08 12:17:25 +02:00
|
|
|
current_time.now() ;
|
2012-06-04 18:07:47 +02:00
|
|
|
return current_time ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-06-22 02:10:38 +02:00
|
|
|
* @param source A reference to the new time.
|
2012-06-04 18:07:47 +02:00
|
|
|
*/
|
2012-06-08 12:17:25 +02:00
|
|
|
void Timestamp::update_current_time(const Timestamp &source)
|
2012-06-04 18:07:47 +02:00
|
|
|
{
|
2013-06-22 02:10:38 +02:00
|
|
|
// We should only call this function if we are in replay mode:
|
2012-06-08 19:08:56 +02:00
|
|
|
assert(Configuration::bool_value("replay")) ;
|
2012-06-08 12:17:25 +02:00
|
|
|
|
|
|
|
// According to human perception, time generally goes only forward:
|
|
|
|
if (current_time < source)
|
2012-06-08 13:11:39 +02:00
|
|
|
{
|
|
|
|
current_time = source ;
|
|
|
|
if (Configuration::is_configured("verbose"))
|
|
|
|
cerr << "Current time set to " << current_time << ".\n" ;
|
|
|
|
}
|
|
|
|
else if (Configuration::is_configured("verbose"))
|
|
|
|
cerr << "Current time left unchanged to " << current_time
|
|
|
|
<< " (time proposed: " << source << ").\n" ;
|
2012-06-04 18:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
bool Timestamp::now()
|
|
|
|
{
|
|
|
|
return clock_gettime(CLOCK_REALTIME, ×tamp) == 0 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-28 12:58:20 +01:00
|
|
|
/**
|
|
|
|
* @returns A Timestamp containing the time elapsed, or an empty
|
|
|
|
* Timestamp in case of error.
|
|
|
|
*/
|
|
|
|
Timestamp Timestamp::elapsed() const
|
|
|
|
{
|
2012-06-08 13:02:30 +02:00
|
|
|
return elapsed(get_current_time()) ;
|
2011-12-28 12:58:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timestamp Timestamp::elapsed(const Timestamp &source) const
|
|
|
|
{
|
|
|
|
owl_timestamp
|
|
|
|
d1 = *this,
|
|
|
|
d2 = source,
|
|
|
|
elapsed ;
|
|
|
|
owl_time_elapsed(&d1, &d2, &elapsed) ;
|
|
|
|
return elapsed ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-03 17:03:31 +01:00
|
|
|
/**
|
|
|
|
* #timestamp nanosecond field precision is set to ms, but the value
|
|
|
|
* is still in ns.
|
|
|
|
*/
|
2010-03-01 16:52:37 +01:00
|
|
|
inline void Timestamp::round_to_ms()
|
2010-02-03 17:03:31 +01:00
|
|
|
{
|
|
|
|
timestamp.tv_nsec = timestamp.tv_nsec / 1000000 * 1000000 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
|
|
|
|
/* *** Internal comparison functions *** */
|
|
|
|
|
|
|
|
|
|
|
|
bool Timestamp::equals(const struct timespec &source) const
|
2010-02-03 17:03:31 +01:00
|
|
|
{
|
2010-03-01 16:52:37 +01:00
|
|
|
return
|
|
|
|
timestamp.tv_sec == source.tv_sec &&
|
|
|
|
timestamp.tv_nsec == source.tv_nsec ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Timestamp::equals(const uint64_t source) const
|
|
|
|
{
|
|
|
|
Timestamp tmp(source) ;
|
2011-03-16 11:23:24 +01:00
|
|
|
Timestamp me(*this) ;
|
|
|
|
me.round_to_ms() ;
|
|
|
|
return me == tmp ;
|
2010-03-01 16:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-08 12:05:32 +02:00
|
|
|
bool Timestamp::before(const struct timespec &source) const
|
2010-03-01 16:52:37 +01:00
|
|
|
{
|
|
|
|
if (timestamp.tv_sec < source.tv_sec)
|
|
|
|
return true ;
|
|
|
|
|
|
|
|
if (timestamp.tv_sec > source.tv_sec)
|
2010-02-03 17:03:31 +01:00
|
|
|
return false ;
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
// Second values are equal
|
|
|
|
if (timestamp.tv_nsec < source.tv_nsec)
|
|
|
|
return true ;
|
|
|
|
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-08 12:05:32 +02:00
|
|
|
bool Timestamp::after(const struct timespec &source) const
|
2010-03-01 16:52:37 +01:00
|
|
|
{
|
|
|
|
if (equals(source))
|
|
|
|
return false ;
|
2012-06-08 12:05:32 +02:00
|
|
|
return ! before(source) ;
|
2010-02-03 17:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* *** Operators *** */
|
|
|
|
|
|
|
|
|
2013-06-17 19:23:00 +02:00
|
|
|
Timestamp& Timestamp::operator=(const struct timespec &source)
|
|
|
|
{
|
|
|
|
set(source) ;
|
|
|
|
return *this ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timestamp& Timestamp::operator=(const uint64_t source)
|
|
|
|
{
|
|
|
|
set(source) ;
|
|
|
|
return *this ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-30 20:38:42 +02:00
|
|
|
Timestamp& Timestamp::operator=(const Timestamp &source)
|
2010-02-03 17:03:31 +01:00
|
|
|
{
|
|
|
|
if (this == &source)
|
|
|
|
return *this ;
|
|
|
|
|
2010-03-01 16:52:37 +01:00
|
|
|
set(source.timestamp) ;
|
2010-02-03 17:03:31 +01:00
|
|
|
|
|
|
|
return *this ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Timestamp::operator==(const Timestamp &source) const
|
|
|
|
{
|
|
|
|
if (this == &source)
|
|
|
|
return true ;
|
2010-03-01 16:52:37 +01:00
|
|
|
return equals(source.timestamp) ;
|
2010-02-03 17:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-05 18:43:24 +02:00
|
|
|
ostream& operator<<(ostream &os, const Timestamp &t)
|
2010-02-03 17:03:31 +01:00
|
|
|
{
|
2013-07-05 18:43:24 +02:00
|
|
|
os << t.timestamp.tv_sec << '.'
|
|
|
|
<< setw(9) << setfill('0') // force nanoseconds on 9 digits
|
|
|
|
<< t.timestamp.tv_nsec ;
|
2010-02-03 17:03:31 +01:00
|
|
|
return os ;
|
|
|
|
}
|
2010-06-07 15:09:22 +02:00
|
|
|
|
|
|
|
|
2011-12-28 12:58:20 +01:00
|
|
|
inline Timestamp::operator owl_timestamp(void) const
|
|
|
|
{
|
|
|
|
owl_timestamp ret ;
|
|
|
|
ret.tv_sec = timestamp.tv_sec ;
|
|
|
|
ret.tv_nsec = timestamp.tv_nsec ;
|
|
|
|
return ret ;
|
|
|
|
}
|