chore: KDIS datatypes
[KDIS.git] / KDIS / DataTypes / ClockTime.cpp
blobaafb70cb73568c3b78a4e25d95a718973da825cc
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 "./ClockTime.h"
32 #include <iomanip>
34 using namespace KDIS;
35 using namespace DATA_TYPE;
36 using namespace ENUMS;
38 const KFLOAT64 ClockTime::SEC_PER_UNIT_TIME = ( 3600.0 ) / 2147483648.0;
40 //////////////////////////////////////////////////////////////////////////
41 // Public:
42 //////////////////////////////////////////////////////////////////////////
44 ClockTime::ClockTime() :
45 m_i32Hour( 0 ),
46 m_ui32TimePastHour( 0 )
50 //////////////////////////////////////////////////////////////////////////
52 ClockTime::ClockTime( KINT32 Hour, KUINT32 TimePastHour ) :
53 m_i32Hour( Hour ),
54 m_ui32TimePastHour( TimePastHour )
58 //////////////////////////////////////////////////////////////////////////
60 ClockTime::ClockTime( KDataStream & stream )
62 Decode( stream );
65 //////////////////////////////////////////////////////////////////////////
67 ClockTime::~ClockTime()
71 //////////////////////////////////////////////////////////////////////////
73 void ClockTime::SetHour( KINT32 H )
75 m_i32Hour = H;
78 //////////////////////////////////////////////////////////////////////////
80 KINT32 ClockTime::GetHour() const
82 return m_i32Hour;
85 //////////////////////////////////////////////////////////////////////////
87 void ClockTime::SetTimePastHour( KUINT32 TPH )
89 m_ui32TimePastHour = TPH;
92 //////////////////////////////////////////////////////////////////////////
94 KUINT32 ClockTime::GetTimePastHour() const
96 return m_ui32TimePastHour;
99 //////////////////////////////////////////////////////////////////////////
101 KString ClockTime::GetAsString() const
103 // Each time unit is 3600/(2^31) seconds.
104 KFLOAT64 totalsecs = static_cast<KFLOAT64>( m_ui32TimePastHour >> 1 ) * SEC_PER_UNIT_TIME;
105 KUINT16 minutes = static_cast<KINT16>( totalsecs ) / 60;
106 KFLOAT64 seconds = totalsecs - ( 60 * minutes );
108 KStringStream ss;
110 ss << "Hour: " << std::setfill('0') << std::setw(2) << m_i32Hour << "\n"
111 << "Time Past Hour: " << std::setfill('0') << std::setw(2) << minutes
112 << ":" << std::setfill('0') << std::setw(2) << seconds
113 << " ("
114 << GetEnumAsStringTimeStampType(m_ui32TimePastHour & 1)
115 << ") "
116 << "\n";
118 return ss.str();
121 //////////////////////////////////////////////////////////////////////////
123 void ClockTime::Decode( KDataStream & stream )
125 if( stream.GetBufferSize() < CLOCK_TIME_SIZE )throw KException( __FUNCTION__, NOT_ENOUGH_DATA_IN_BUFFER );
127 stream >> m_i32Hour
128 >> m_ui32TimePastHour;
131 //////////////////////////////////////////////////////////////////////////
133 KDataStream ClockTime::Encode() const
135 KDataStream stream;
137 ClockTime::Encode( stream );
139 return stream;
142 //////////////////////////////////////////////////////////////////////////
144 void ClockTime::Encode( KDataStream & stream ) const
146 stream << m_i32Hour
147 << m_ui32TimePastHour;
150 //////////////////////////////////////////////////////////////////////////
152 KBOOL ClockTime::operator == ( const ClockTime & Value ) const
154 if( m_i32Hour != Value.m_i32Hour ) return false;
155 if( m_ui32TimePastHour != Value.m_ui32TimePastHour ) return false;
156 return true;
159 //////////////////////////////////////////////////////////////////////////
161 KBOOL ClockTime::operator != ( const ClockTime & Value ) const
163 return !( *this == Value );
166 //////////////////////////////////////////////////////////////////////////
168 KBOOL ClockTime::operator < ( const ClockTime & Value ) const
170 if( m_i32Hour < Value.m_i32Hour )return true;
171 if( m_i32Hour > Value.m_i32Hour )return false;
173 return m_ui32TimePastHour < Value.m_ui32TimePastHour;
176 //////////////////////////////////////////////////////////////////////////