#ifndef __JDAQUTCEXTENDED__ #define __JDAQUTCEXTENDED__ /** * \author rbruijn */ //#include // only in C++0x, will give uint32_t #include #include #include #include #include "km3net-dataformat/online/JDAQRoot.hh" namespace KM3NETDAQ { /** * Data structure for UTC time. */ class JDAQUTCExtended { public: typedef unsigned int JUINT32_t; // preferably uint32_t friend size_t getSizeof(); friend JReader& operator>>(JReader&, JDAQUTCExtended&); friend JWriter& operator<<(JWriter&, const JDAQUTCExtended&); /** * Default constructor. */ JDAQUTCExtended() : UTC_seconds(0), UTC_16nanosecondcycles(0) {} /** * Constructor. * * \param seconds seconds [s] * \param cycles cycles [16 ns] */ JDAQUTCExtended(const JUINT32_t seconds, const JUINT32_t cycles): UTC_seconds(seconds), UTC_16nanosecondcycles(cycles) {} /** * Constructor. * * \param nanoseconds time [ns] */ JDAQUTCExtended(const double nanoseconds) { setTimeNanoSecond(nanoseconds); } /** * Virtual destructor. */ virtual ~JDAQUTCExtended() {} /** * Get time. * * \return time [s] */ JUINT32_t getUTCseconds() const { return (UTC_seconds & getMask()); } /** * Get time. * * \return time [16 ns] */ JUINT32_t getUTC16nanosecondcycles() const { return UTC_16nanosecondcycles; } /** * Get time (limited to 16 ns cycles). * * \return time [ns] */ double getTimeNanoSecond() const { return getUTCseconds() * 1.0e9 + getUTC16nanosecondcycles() * getTick(); } /** * Set time. * * \param utc_ns time [ns] */ void setTimeNanoSecond(const double utc_ns) { UTC_seconds = (unsigned int) ( utc_ns / 1.0e9); UTC_16nanosecondcycles = (unsigned int) ((utc_ns - UTC_seconds*1.0e9) / getTick()); } /** * Get minimum possible value. * * \return minimum possible value */ static JDAQUTCExtended min() { return JDAQUTCExtended(0,0); } /** * Get maximum possible value. * * \return maximum possible value */ static JDAQUTCExtended max() { return JDAQUTCExtended(std::numeric_limits::max(), std::numeric_limits::max()); } /** * Get mask for seconds data. * * \return mask */ static JUINT32_t getMask() { return 0x7FFFFFFF; } /** * Get number of nano-seconds per tick. * * \return time [ns] */ static double getTick() { return 16.0; } /** * Read UTC time. * * \param in intput stream * \param utc UTC extended time * \return intput stream */ friend inline std::istream& operator>>(std::istream& in, JDAQUTCExtended& utc) { in >> utc.UTC_seconds; in.get(); in >> utc.UTC_16nanosecondcycles; return in; } /** * Write UTC time. * * \param out output stream * \param utc UTC extended time * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JDAQUTCExtended& utc) { using namespace std; const char c = out.fill(); out << setw(10) << utc.getUTCseconds(); out << ':'; out << setw(10) << setfill('0') << utc.getUTC16nanosecondcycles() << setfill(c); return out; } ClassDef(JDAQUTCExtended,1); protected: JUINT32_t UTC_seconds; JUINT32_t UTC_16nanosecondcycles; }; /** * Less than operator for UTC times. * * \param first UTC time * \param second UTC time * \result true if first UTC time earlier than second UTC time; else false */ inline bool operator<(const JDAQUTCExtended& first, const JDAQUTCExtended& second) { if (first.getUTCseconds() == second.getUTCseconds()) return first.getUTC16nanosecondcycles() < second.getUTC16nanosecondcycles(); else return first.getUTCseconds() < second.getUTCseconds(); } /** * Equal operator for UTC times. * * \param first UTC time * \param second UTC time * \result true if first UTC time equal second UTC time; else false */ inline bool operator==(const JDAQUTCExtended& first, const JDAQUTCExtended& second) { return (first.getUTCseconds() == second.getUTCseconds() && first.getUTC16nanosecondcycles() == second.getUTC16nanosecondcycles()); } /** * Not equal operator for UTC times. * * \param first UTC time * \param second UTC time * \result true if first UTC time not equal second UTC time; else false */ inline bool operator!=(const JDAQUTCExtended& first, const JDAQUTCExtended& second) { return !(first == second); } } #endif