Program Listing for File timestamp_base.hpp

Return to documentation for file (/tmp/ws/src/ecl_core/ecl_time/include/ecl/time/timestamp_base.hpp)

/*****************************************************************************
** Ifdefs
*****************************************************************************/

#ifndef ECL_TIME_TIMESTAMP_BASE_HPP_
#define ECL_TIME_TIMESTAMP_BASE_HPP_

/*****************************************************************************
** Includes
*****************************************************************************/

#include <cstdlib>
#include <ecl/time_lite/types.hpp>
#include <ecl/config/macros.hpp>
#include <ecl/exceptions/standard_exception.hpp>
#include "macros.hpp"

/*****************************************************************************
** Namespaces
*****************************************************************************/

namespace ecl {

/*****************************************************************************
** Implementation
*****************************************************************************/
class ecl_time_PUBLIC TimeStampBase {
public:
    /*********************
    ** Constructors
    **********************/
    explicit TimeStampBase() {};
    explicit TimeStampBase (const double& decimal_time_value);
    TimeStampBase (const time_t &seconds, const long &nanoseconds);

    virtual ~TimeStampBase() {}

    /******************************************
    ** Stamps
    *******************************************/
    const TimeStampBase& stamp (const double& decimal_time_value);
    const TimeStampBase& stamp (const time_t &seconds, const long &nanoseconds);

    /******************************************
    ** Accessors
    *******************************************/
    long sec() const { return time.tv_sec; }
    long msec() const { return time.tv_nsec/1000000L; }
    long usec() const { return time.tv_nsec/1000; }
    long nsec() const { return time.tv_nsec; }

    operator double() const { return ( time.tv_sec + time.tv_nsec*0.000000001); }

    /******************************************
    ** Comparison Operators
    *******************************************/
    bool operator==(const TimeStampBase& time_stamp);
    bool operator!=(const TimeStampBase& time_stamp);
    bool operator<=(const TimeStampBase& time_stamp);
    bool operator>=(const TimeStampBase& time_stamp);
    bool operator<(const TimeStampBase& time_stamp);
    bool operator>(const TimeStampBase& time_stamp);

    /******************************************
    ** Mathematical Operators
    *******************************************/
    TimeStampBase operator+(const TimeStampBase& time_stamp );
    void operator+=(const TimeStampBase& time_stamp);
    TimeStampBase operator-(const TimeStampBase& time_stamp );
    void operator-=(const TimeStampBase& time_stamp);

    /******************************************
    ** Insertion Operator
    *******************************************/
    template <typename OutputStream>
    friend OutputStream& operator << ( OutputStream &ostream , const TimeStampBase& time_stamp );

protected:
    TimeStructure time;
};


/*****************************************************************************
** Implementation [Insertion Operator]
*****************************************************************************/

template <typename OutputStream>
OutputStream& operator <<( OutputStream &ostream , const TimeStampBase& time_stamp )
{
    if ( ( time_stamp.time.tv_sec == 0 ) && (time_stamp.time.tv_nsec < 0 ) ) {
      ostream << "-";
    }
    ostream << time_stamp.time.tv_sec << ".";
    long nanoseconds = std::abs(time_stamp.time.tv_nsec);
    if ( nanoseconds < 10 ) {
        ostream << "00000000";
    } else if ( nanoseconds < 100 ) {
        ostream << "0000000";
    } else if ( nanoseconds < 1000 ) {
        ostream << "000000";
    } else if ( nanoseconds < 10000 ) {
        ostream << "00000";
    } else if ( nanoseconds < 100000 ) {
        ostream << "0000";
    } else if ( nanoseconds < 1000000 ) {
        ostream << "000";
    } else if ( nanoseconds < 10000000 ) {
        ostream << "00";
    } else if ( nanoseconds < 100000000 ) {
        ostream << "0";
    }
    ostream << nanoseconds;
    return ostream;
}

} // namespace ecl

#endif /* ECL_TIME_TIMESTAMP_BASE_HPP_ */