1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkRealTimeInterval_h
19 #define itkRealTimeInterval_h
20 
21 #include "itkIntTypes.h"
22 #include "itkMacro.h"
23 #include <iostream>
24 
25 namespace itk
26 {
27 /** \class RealTimeInterval
28  * \brief A data structure for representing the time
29  * span between two RealTimeStamps, with similar high precision and a large
30  * dynamic range to what the RealTimeStamps offer.
31  *
32  * This class represents the difference between two time points, typically for
33  * applications that need to mark the time of acquisition of data with high
34  * precision (microseconds) and a large dynamic range (years). This class will
35  * be the natural representation for the duration of a video sequence, or for
36  * the time that has passed between the acquisition of one images and a
37  * subsequent one.
38  *
39  * \sa RealTimeStamp
40  * \sa RealTimeClock
41  *
42  * \ingroup ITKCommon
43  */
44 
45 class ITKCommon_EXPORT RealTimeInterval
46 {
47 public:
48 
49   using Self = RealTimeInterval;
50 
51   /** Internal types used to represent seconds and microseconds. */
52   using SecondsDifferenceType = int64_t;
53   using MicroSecondsDifferenceType = int64_t;
54 
55   /** Constructor */
56   RealTimeInterval();
57 
58   /** Constructor with values. Intentionally made public */
59   RealTimeInterval( SecondsDifferenceType, MicroSecondsDifferenceType );
60 
61   /** Destructor */
62   ~RealTimeInterval();
63 
64   /** Native type used to represent the time in different time units. */
65   using TimeRepresentationType = double;
66 
67   /** Return time in multiple units. */
68   TimeRepresentationType GetTimeInMicroSeconds() const;
69   TimeRepresentationType GetTimeInMilliSeconds() const;
70   TimeRepresentationType GetTimeInSeconds() const;
71   TimeRepresentationType GetTimeInMinutes() const;
72   TimeRepresentationType GetTimeInHours() const;
73   TimeRepresentationType GetTimeInDays() const;
74 
75   /** Arithmetic operations between RealTimeInterval and RealTimeInterval. */
76   Self operator-( const Self & ) const;
77   Self operator+( const Self & ) const;
78   const Self & operator-=( const Self & );
79   const Self & operator+=( const Self & );
80 
81   /** Comparison operations. */
82   bool operator>( const Self & ) const;
83   bool operator<( const Self & ) const;
84   bool operator==( const Self & ) const;
85   bool operator!=( const Self & ) const;
86   bool operator<=( const Self & ) const;
87   bool operator>=( const Self & ) const;
88 
89   /** Set with values. The units and signs of the seconds and microseconds will
90    * be harmonized internally. */
91   void Set( SecondsDifferenceType, MicroSecondsDifferenceType );
92 
93   /** Default print out of a RealTimeInterval */
94   friend ITKCommon_EXPORT std::ostream & operator<<(std::ostream & os, const RealTimeInterval & v);
95 
96 private:
97 
98   friend class RealTimeStamp;
99 
100   /** Number of Seconds and Microseconds since... */
101   SecondsDifferenceType        m_Seconds;
102 
103   /** Number of Microseconds since the second.
104    *  Should be in the range -999,999 to 999,999
105    *  and it must always have the same sign as
106    *  the m_Seconds member variable. */
107   MicroSecondsDifferenceType   m_MicroSeconds;
108 
109 };
110 
111 } // end of namespace itk
112 
113 #endif  // itkRealTimeInterval_h
114