032b958a0db3ce36454c7fa02c5dd14e908db774
[platform/framework/native/appfw.git] / src / base / FBaseTimeSpan.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                FBaseTimeSpan.cpp
19  * @brief               This is the implementation file of TimeSpan class.
20  */
21
22 #include <FBaseTimeSpan.h>
23 #include <FBaseSysLog.h>
24
25 namespace Tizen { namespace Base
26 {
27
28 TimeSpan::TimeSpan(const TimeSpan& value)
29         : __ticks(value.__ticks)
30         , __pTimeSpanImpl(null)
31 {
32 }
33
34 TimeSpan::TimeSpan(int hours, int minutes, int seconds)
35 {
36         __pTimeSpanImpl = null;
37         __ticks = CalcTicks(0, hours, minutes, seconds, 0);
38 }
39
40 TimeSpan::TimeSpan(int days, int hours, int minutes, int seconds)
41 {
42         __pTimeSpanImpl = null;
43         __ticks = CalcTicks(days, hours, minutes, seconds, 0);
44 }
45
46 TimeSpan::TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds)
47 {
48         __pTimeSpanImpl = null;
49         __ticks = CalcTicks(days, hours, minutes, seconds, milliseconds);
50 }
51
52 TimeSpan::TimeSpan(long long ticks)
53         : __ticks(ticks)
54         , __pTimeSpanImpl(null)
55 {
56 }
57
58 TimeSpan::~TimeSpan(void)
59 {
60 }
61
62 TimeSpan
63 TimeSpan::operator +(const TimeSpan& rhs) const
64 {
65         return TimeSpan(__ticks + rhs.__ticks);
66 }
67
68 TimeSpan
69 TimeSpan::operator -(const TimeSpan& rhs) const
70 {
71         return TimeSpan(__ticks - rhs.__ticks);
72 }
73
74 TimeSpan&
75 TimeSpan::operator =(const TimeSpan& rhs)
76 {
77         if (&rhs != this)
78         {
79                 __ticks = rhs.__ticks;
80         }
81         return *this;
82 }
83
84 bool
85 TimeSpan::operator ==(const TimeSpan& rhs) const
86 {
87         return __ticks == rhs.__ticks;
88 }
89
90 bool
91 TimeSpan::operator !=(const TimeSpan& rhs) const
92 {
93         return __ticks != rhs.__ticks;
94 }
95
96 bool
97 TimeSpan::operator <(const TimeSpan& rhs) const
98 {
99         return __ticks < rhs.__ticks;
100 }
101
102 bool
103 TimeSpan::operator >(const TimeSpan& rhs) const
104 {
105         return __ticks > rhs.__ticks;
106 }
107
108 bool
109 TimeSpan::operator <=(const TimeSpan& rhs) const
110 {
111         return __ticks <= rhs.__ticks;
112 }
113
114 bool
115 TimeSpan::operator >=(const TimeSpan& rhs) const
116 {
117         return __ticks >= rhs.__ticks;
118 }
119
120 int
121 TimeSpan::Compare(const TimeSpan& t1, const TimeSpan& t2)
122 {
123         if ((t1.__ticks - t2.__ticks) > 0)
124         {
125                 return 1;
126         }
127         else if ((t1.__ticks - t2.__ticks) < 0)
128         {
129                 return -1;
130         }
131
132         return 0;
133 }
134
135 int
136 TimeSpan::CompareTo(const TimeSpan& value) const
137 {
138         return TimeSpan::Compare(*this, value);
139 }
140
141 bool
142 TimeSpan::Equals(const Object& obj) const
143 {
144         const TimeSpan* pOther = dynamic_cast <const TimeSpan*>(&obj);
145         if (pOther == null)
146         {
147                 return false;
148         }
149
150         return *this == *pOther;
151 }
152
153 TimeSpan
154 TimeSpan::Duration(void) const
155 {
156         return TimeSpan((__ticks >= 0 ? __ticks : -__ticks));
157 }
158
159 int
160 TimeSpan::GetHashCode(void) const
161 {
162         return(int((__ticks >> 32) ^ ((__ticks >> 32) >> 16)) ^ int((__ticks & 0x0000FFFF)));
163 }
164
165 TimeSpan
166 TimeSpan::Negate(void) const
167 {
168         return TimeSpan(-__ticks);
169 }
170
171 long long
172 TimeSpan::CalcTicks(int days, int hours, int minutes, int seconds, int milliseconds)
173 {
174         if (days < 0)
175         {
176                 days = 0;
177         }
178         if (hours < 0)
179         {
180                 hours = 0;
181         }
182         if (minutes < 0)
183         {
184                 minutes = 0;
185         }
186         if (seconds < 0)
187         {
188                 seconds = 0;
189         }
190         if (milliseconds < 0)
191         {
192                 milliseconds = 0;
193         }
194
195         return (long long) ((NUM_OF_TICKS_IN_DAY * days) + (NUM_OF_TICKS_IN_HOUR * hours) + (NUM_OF_TICKS_IN_MINUTE * minutes) +
196                                                 (NUM_OF_TICKS_IN_SECOND * seconds) + milliseconds);
197 }
198
199 long long
200 TimeSpan::GetDays(void) const
201 {
202         return (long long) (__ticks / NUM_OF_TICKS_IN_DAY);
203 }
204
205 long long
206 TimeSpan::GetHours(void) const
207 {
208         long long r = __ticks % NUM_OF_TICKS_IN_DAY;
209         if (r != 0)
210         {
211                 return (long long) (r / NUM_OF_TICKS_IN_HOUR);
212         }
213         return 0;
214 }
215
216 long long
217 TimeSpan::GetMinutes(void) const
218 {
219         long long r = __ticks % NUM_OF_TICKS_IN_DAY;
220         if (r != 0)
221         {
222                 r = r % NUM_OF_TICKS_IN_HOUR;
223                 if (r != 0)
224                 {
225                         return (long long) (r / NUM_OF_TICKS_IN_MINUTE);
226                 }
227                 return 0;
228         }
229         return 0;
230 }
231
232 long long
233 TimeSpan::GetSeconds(void) const
234 {
235         long long r = __ticks % NUM_OF_TICKS_IN_DAY;
236         if (r != 0)
237         {
238                 r = r % NUM_OF_TICKS_IN_HOUR;
239                 if (r != 0)
240                 {
241                         r = r % NUM_OF_TICKS_IN_MINUTE;
242                         if (r != 0)
243                         {
244                                 return (long long) (r / NUM_OF_TICKS_IN_SECOND);
245                         }
246                         return 0;
247                 }
248                 return 0;
249         }
250         return 0;
251 }
252
253 long long
254 TimeSpan::GetMilliseconds(void) const
255 {
256         long long r = __ticks % NUM_OF_TICKS_IN_DAY;
257         if (r != 0)
258         {
259                 r = r % NUM_OF_TICKS_IN_HOUR;
260                 if (r != 0)
261                 {
262                         r = r % NUM_OF_TICKS_IN_MINUTE;
263                         if (r != 0)
264                         {
265                                 r = r % NUM_OF_TICKS_IN_SECOND;
266                                 return(r);
267                         }
268                         return 0;
269                 }
270                 return 0;
271         }
272         return 0;
273 }
274
275 long long
276 TimeSpan::GetTicks(void) const
277 {
278         return(__ticks);
279 }
280
281 }} //Tizen::Base