fix: cmake install and possible conflicting options
[KDIS.git] / src / DataTypes / TimeStamp.cpp
blobffbd8e8075a96c2e6a188f369a26314fde4f7352
1 /*********************************************************************
2 Copyright 2013 Karl Jones
3 All rights reserved.
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
26 Karljj1@yahoo.com
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
30 #include "KDIS/DataTypes/TimeStamp.hpp"
32 #include <iomanip>
33 #if defined(WIN32) || defined(WIN64)
34 #include <windows.h>
35 #else
36 #include <time.h>
37 #endif
39 using namespace KDIS;
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 //////////////////////////////////////////////////////////////////////////
49 // public:
50 //////////////////////////////////////////////////////////////////////////
52 TimeStamp::TimeStamp() : m_bAutoCalcRel(false) {
53 m_TimeStampUnion.m_ui32TimeStamp = 0;
56 //////////////////////////////////////////////////////////////////////////
58 TimeStamp::TimeStamp(KDataStream& stream) : m_bAutoCalcRel(false) {
59 Decode(stream);
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
108 KINT32 iTs = 0;
110 #if defined(WIN32) | defined(WIN64) // Microsoft Windows
112 SYSTEMTIME now;
113 GetSystemTime(&now);
114 KFLOAT64 f = (now.wMinute * 60) + now.wSecond + (now.wMilliseconds / 1000.0);
115 iTs = f * UNIT_TIME_PER_SEC;
117 #else
119 time_t aclock;
120 time(&aclock);
121 struct tm* newtime = localtime(&aclock);
122 iTs = newtime->tm_sec + (newtime->tm_min * 60);
123 iTs = iTs * UNIT_TIME_PER_SEC;
125 #endif
127 #if defined(linux) // Linux -- Note: You need to include the rt library for
128 // clock_gettime.
130 // Add nano seconds
131 timespec ts;
132 clock_gettime(0, &ts);
133 iTs += ts.tv_nsec * UNIT_TIME_PER_NANOSEC;
135 #endif
137 m_TimeStampUnion.m_ui32Time = iTs;
140 //////////////////////////////////////////////////////////////////////////
142 KString TimeStamp::GetAsString() const {
143 // Each time unit is 3600/(2^31) seconds.
144 KFLOAT64 totalsecs =
145 static_cast<KFLOAT64>(m_TimeStampUnion.m_ui32TimeStamp >> 1) *
146 SEC_PER_UNIT_TIME;
147 KUINT16 minutes = static_cast<KINT16>(totalsecs) / 60;
148 KFLOAT64 seconds = totalsecs - (60 * minutes);
150 KStringStream ss;
152 ss << "Timestamp: ";
153 ss << std::setfill('0') << std::setw(2) << minutes << ":" << std::setfill('0')
154 << std::setw(2) << seconds << " ("
155 << GetEnumAsStringTimeStampType(m_TimeStampUnion.m_ui32TimeStampType)
156 << ") "
157 << "\n";
159 return ss.str();
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 {
174 KDataStream stream;
176 TimeStamp::Encode(stream);
178 return 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)
199 return false;
200 return true;
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 //////////////////////////////////////////////////////////////////////////