287 lines
6.1 KiB
C++
287 lines
6.1 KiB
C++
/*
|
|
* This file is part of the Owl Positioning System (OwlPS).
|
|
* OwlPS is a project of the University of Franche-Comte
|
|
* (Université de Franche-Comté), France.
|
|
*
|
|
* Copyright © Université de Franche-Comté 2007-2012.
|
|
*
|
|
* Corresponding author: Matteo Cypriani <mcy@lm7.fr>
|
|
*
|
|
***********************************************************************
|
|
*
|
|
* This software is governed by the CeCILL license under French law and
|
|
* abiding by the rules of distribution of free software. You can use,
|
|
* modify and/or redistribute the software under the terms of the CeCILL
|
|
* license as circulated by CEA, CNRS and INRIA at the following URL:
|
|
* http://www.cecill.info
|
|
*
|
|
* As a counterpart to the access to the source code and rights to copy,
|
|
* modify and redistribute granted by the license, users are provided
|
|
* only with a limited warranty and the software's authors, the holder
|
|
* of the economic rights, and the successive licensors have only
|
|
* limited liability.
|
|
*
|
|
* In this respect, the user's attention is drawn to the risks
|
|
* associated with loading, using, modifying and/or developing or
|
|
* reproducing the software by the user in light of its specific status
|
|
* of free software, that may mean that it is complicated to manipulate,
|
|
* and that also therefore means that it is reserved for developers and
|
|
* experienced professionals having in-depth computer knowledge. Users
|
|
* are therefore encouraged to load and test the software's suitability
|
|
* as regards their requirements in conditions enabling the security of
|
|
* their systems and/or data to be ensured and, more generally, to use
|
|
* and operate it in the same conditions as regards security.
|
|
*
|
|
* The fact that you are presently reading this means that you have had
|
|
* knowledge of the CeCILL license and that you accept its terms.
|
|
*
|
|
***********************************************************************
|
|
*/
|
|
|
|
|
|
#include "timestamp.hh"
|
|
#include "configuration.hh"
|
|
|
|
#include <boost/functional/hash.hpp>
|
|
#include <iostream>
|
|
|
|
using namespace std ;
|
|
|
|
|
|
Timestamp Timestamp::current_time ;
|
|
|
|
|
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
Timestamp::Timestamp()
|
|
{
|
|
clear() ;
|
|
}
|
|
|
|
|
|
Timestamp::Timestamp(const struct timespec &source)
|
|
{
|
|
set(source) ;
|
|
}
|
|
|
|
|
|
Timestamp::Timestamp(const owl_timestamp &source)
|
|
{
|
|
set(source) ;
|
|
}
|
|
|
|
|
|
Timestamp::Timestamp(const uint_fast32_t source_s,
|
|
const uint_fast32_t source_ns)
|
|
{
|
|
set(source_s, source_ns) ;
|
|
}
|
|
|
|
|
|
Timestamp::Timestamp(const uint64_t source)
|
|
{
|
|
set(source) ;
|
|
}
|
|
|
|
|
|
Timestamp::Timestamp(const Timestamp &source)
|
|
{
|
|
set(source.timestamp) ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Internal accessors *** */
|
|
|
|
|
|
inline void Timestamp::set(const struct timespec &source)
|
|
{
|
|
timestamp = source ;
|
|
}
|
|
|
|
|
|
inline void Timestamp::set(const owl_timestamp &source)
|
|
{
|
|
set(source.tv_sec, source.tv_nsec) ;
|
|
}
|
|
|
|
|
|
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 ;
|
|
}
|
|
|
|
|
|
inline void Timestamp::set(const uint64_t source_ms)
|
|
{
|
|
timestamp.tv_sec = source_ms / 1000 ;
|
|
timestamp.tv_nsec = (source_ms - timestamp.tv_sec * 1000) * 1000000 ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
Timestamp Timestamp::get_current_time()
|
|
{
|
|
if (! Configuration::bool_value("replay"))
|
|
current_time.now() ;
|
|
return current_time ;
|
|
}
|
|
|
|
|
|
/**
|
|
* @source A pointer to the new time. If NULL, #current_time is updated
|
|
* to the current time.
|
|
*/
|
|
void Timestamp::update_current_time(const Timestamp &source)
|
|
{
|
|
// We should never call this function if we are in replay mode:
|
|
assert(Configuration::bool_value("replay")) ;
|
|
|
|
// According to human perception, time generally goes only forward:
|
|
if (current_time < source)
|
|
{
|
|
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" ;
|
|
}
|
|
|
|
|
|
bool Timestamp::now()
|
|
{
|
|
return clock_gettime(CLOCK_REALTIME, ×tamp) == 0 ;
|
|
}
|
|
|
|
|
|
/**
|
|
* @returns A Timestamp containing the time elapsed, or an empty
|
|
* Timestamp in case of error.
|
|
*/
|
|
Timestamp Timestamp::elapsed() const
|
|
{
|
|
return elapsed(get_current_time()) ;
|
|
}
|
|
|
|
|
|
Timestamp Timestamp::elapsed(const Timestamp &source) const
|
|
{
|
|
owl_timestamp
|
|
d1 = *this,
|
|
d2 = source,
|
|
elapsed ;
|
|
owl_time_elapsed(&d1, &d2, &elapsed) ;
|
|
return elapsed ;
|
|
}
|
|
|
|
|
|
/**
|
|
* #timestamp nanosecond field precision is set to ms, but the value
|
|
* is still in ns.
|
|
*/
|
|
inline void Timestamp::round_to_ms()
|
|
{
|
|
timestamp.tv_nsec = timestamp.tv_nsec / 1000000 * 1000000 ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Internal comparison functions *** */
|
|
|
|
|
|
bool Timestamp::equals(const struct timespec &source) const
|
|
{
|
|
return
|
|
timestamp.tv_sec == source.tv_sec &&
|
|
timestamp.tv_nsec == source.tv_nsec ;
|
|
}
|
|
|
|
|
|
bool Timestamp::equals(const uint64_t source) const
|
|
{
|
|
Timestamp tmp(source) ;
|
|
Timestamp me(*this) ;
|
|
me.round_to_ms() ;
|
|
return me == tmp ;
|
|
}
|
|
|
|
|
|
bool Timestamp::before(const struct timespec &source) const
|
|
{
|
|
if (timestamp.tv_sec < source.tv_sec)
|
|
return true ;
|
|
|
|
if (timestamp.tv_sec > source.tv_sec)
|
|
return false ;
|
|
|
|
// Second values are equal
|
|
if (timestamp.tv_nsec < source.tv_nsec)
|
|
return true ;
|
|
|
|
return false ;
|
|
}
|
|
|
|
|
|
bool Timestamp::after(const struct timespec &source) const
|
|
{
|
|
if (equals(source))
|
|
return false ;
|
|
return ! before(source) ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Operators *** */
|
|
|
|
|
|
Timestamp& Timestamp::operator=(const Timestamp &source)
|
|
{
|
|
if (this == &source)
|
|
return *this ;
|
|
|
|
set(source.timestamp) ;
|
|
|
|
return *this ;
|
|
}
|
|
|
|
|
|
bool Timestamp::operator==(const Timestamp &source) const
|
|
{
|
|
if (this == &source)
|
|
return true ;
|
|
return equals(source.timestamp) ;
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream &os, const Timestamp &t)
|
|
{
|
|
os << t.timestamp.tv_sec << '.' << t.timestamp.tv_nsec ;
|
|
return os ;
|
|
}
|
|
|
|
|
|
inline Timestamp::operator owl_timestamp(void) const
|
|
{
|
|
owl_timestamp ret ;
|
|
ret.tv_sec = timestamp.tv_sec ;
|
|
ret.tv_nsec = timestamp.tv_nsec ;
|
|
return ret ;
|
|
}
|
|
|
|
|
|
|
|
size_t hash_value(const Timestamp &source)
|
|
{
|
|
return boost::hash_value(static_cast<uint64_t>(source)) ;
|
|
}
|