1 /*********************************************************************
2 Copyright 2013 Karl Jones
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 For Further Information Please Contact me at
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
30 #include "KDIS/DataTypes/TimeStamp.hpp"
33 #if defined(WIN32) || defined(WIN64)
40 using namespace DATA_TYPE
;
41 using namespace ENUMS
;
43 const KFLOAT64
TimeStamp::SEC_PER_UNIT_TIME
= (3600.0) / 2147483648.0;
44 const KFLOAT64
TimeStamp::UNIT_TIME_PER_SEC
= 1.0 / SEC_PER_UNIT_TIME
;
45 const KFLOAT64
TimeStamp::NANOSEC_PER_UNIT_TIME
= SEC_PER_UNIT_TIME
* 1.0e9
;
46 const KFLOAT64
TimeStamp::UNIT_TIME_PER_NANOSEC
= 1.0 / NANOSEC_PER_UNIT_TIME
;
48 //////////////////////////////////////////////////////////////////////////
50 //////////////////////////////////////////////////////////////////////////
52 TimeStamp::TimeStamp() : m_bAutoCalcRel(false) {
53 m_TimeStampUnion
.m_ui32TimeStamp
= 0;
56 //////////////////////////////////////////////////////////////////////////
58 TimeStamp::TimeStamp(KDataStream
& stream
) : m_bAutoCalcRel(false) {
62 //////////////////////////////////////////////////////////////////////////
64 TimeStamp::TimeStamp(TimeStampType T
, KUINT32 Time
,
65 KBOOL AutoCalcRelative
/*= false*/)
66 : m_bAutoCalcRel(AutoCalcRelative
) {
67 m_TimeStampUnion
.m_ui32TimeStampType
= T
;
68 m_TimeStampUnion
.m_ui32Time
= Time
;
71 //////////////////////////////////////////////////////////////////////////
73 TimeStamp::~TimeStamp() {}
75 //////////////////////////////////////////////////////////////////////////
77 void TimeStamp::SetTimeStampType(TimeStampType T
) {
78 m_TimeStampUnion
.m_ui32TimeStampType
= T
;
81 //////////////////////////////////////////////////////////////////////////
83 TimeStampType
TimeStamp::GetTimeStampType() const {
84 return (TimeStampType
)m_TimeStampUnion
.m_ui32TimeStampType
;
87 //////////////////////////////////////////////////////////////////////////
89 void TimeStamp::SetTime(KUINT32 T
) { m_TimeStampUnion
.m_ui32Time
= T
; }
91 //////////////////////////////////////////////////////////////////////////
93 KUINT32
TimeStamp::GetTime() const { return m_TimeStampUnion
.m_ui32Time
; }
95 //////////////////////////////////////////////////////////////////////////
97 void TimeStamp::SetTimeStampAutoCalculate(KBOOL A
) { m_bAutoCalcRel
= A
; }
99 //////////////////////////////////////////////////////////////////////////
101 KBOOL
TimeStamp::IsTimeStampAutoCalculated() const { return m_bAutoCalcRel
; }
103 //////////////////////////////////////////////////////////////////////////
105 void TimeStamp::CalculateTimeStamp() {
106 // Calculate the time by taking the minutes, seconds and milliseconds(if
107 // supported by os) since the hour and dividing them by 0.000001676
110 #if defined(WIN32) | defined(WIN64) // Microsoft Windows
114 KFLOAT64 f
= (now
.wMinute
* 60) + now
.wSecond
+ (now
.wMilliseconds
/ 1000.0);
115 iTs
= f
* UNIT_TIME_PER_SEC
;
121 struct tm
* newtime
= localtime(&aclock
);
122 iTs
= newtime
->tm_sec
+ (newtime
->tm_min
* 60);
123 iTs
= iTs
* UNIT_TIME_PER_SEC
;
127 #if defined(linux) // Linux -- Note: You need to include the rt library for
132 clock_gettime(0, &ts
);
133 iTs
+= ts
.tv_nsec
* UNIT_TIME_PER_NANOSEC
;
137 m_TimeStampUnion
.m_ui32Time
= iTs
;
140 //////////////////////////////////////////////////////////////////////////
142 KString
TimeStamp::GetAsString() const {
143 // Each time unit is 3600/(2^31) seconds.
145 static_cast<KFLOAT64
>(m_TimeStampUnion
.m_ui32TimeStamp
>> 1) *
147 KUINT16 minutes
= static_cast<KINT16
>(totalsecs
) / 60;
148 KFLOAT64 seconds
= totalsecs
- (60 * minutes
);
153 ss
<< std::setfill('0') << std::setw(2) << minutes
<< ":" << std::setfill('0')
154 << std::setw(2) << seconds
<< " ("
155 << GetEnumAsStringTimeStampType(m_TimeStampUnion
.m_ui32TimeStampType
)
162 //////////////////////////////////////////////////////////////////////////
164 void TimeStamp::Decode(KDataStream
& stream
) {
165 if (stream
.GetBufferSize() < TIME_STAMP_SIZE
)
166 throw KException(__FUNCTION__
, NOT_ENOUGH_DATA_IN_BUFFER
);
168 stream
>> m_TimeStampUnion
.m_ui32TimeStamp
;
171 //////////////////////////////////////////////////////////////////////////
173 KDataStream
TimeStamp::Encode() const {
176 TimeStamp::Encode(stream
);
181 //////////////////////////////////////////////////////////////////////////
183 void TimeStamp::Encode(KDataStream
& stream
) const {
184 // Do we need to calculate the timestamp first?
185 if (m_bAutoCalcRel
) {
186 // We need to cast away the const so we can make the change.
187 TimeStamp
* self
= const_cast<TimeStamp
*>(this);
188 self
->CalculateTimeStamp();
191 stream
<< m_TimeStampUnion
.m_ui32TimeStamp
;
194 //////////////////////////////////////////////////////////////////////////
196 KBOOL
TimeStamp::operator==(const TimeStamp
& Value
) const {
197 if (m_TimeStampUnion
.m_ui32TimeStamp
!=
198 Value
.m_TimeStampUnion
.m_ui32TimeStamp
)
203 //////////////////////////////////////////////////////////////////////////
205 KBOOL
TimeStamp::operator!=(const TimeStamp
& Value
) const {
206 return !(*this == Value
);
209 //////////////////////////////////////////////////////////////////////////
211 KBOOL
TimeStamp::operator<(const TimeStamp
& Value
) const {
212 // Note: We don't check if the time stamps are the same type.
213 return m_TimeStampUnion
.m_ui32Time
< Value
.m_TimeStampUnion
.m_ui32Time
;
216 //////////////////////////////////////////////////////////////////////////