Merge "Apply string localization for web" into tizen_2.2
[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 #include <FBaseTimeSpan.h>
22 #include <FBaseSysLog.h>
23
24 namespace Tizen { namespace Base
25 {
26
27 TimeSpan::TimeSpan(const TimeSpan& value)
28         : __ticks(value.__ticks)
29         , __pTimeSpanImpl(null)
30 {
31 }
32
33 TimeSpan::TimeSpan(int hours, int minutes, int seconds)
34 {
35         __pTimeSpanImpl = null;
36         __ticks = CalcTicks(0, hours, minutes, seconds, 0);
37 }
38
39 TimeSpan::TimeSpan(int days, int hours, int minutes, int seconds)
40 {
41         __pTimeSpanImpl = null;
42         __ticks = CalcTicks(days, hours, minutes, seconds, 0);
43 }
44
45 TimeSpan::TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds)
46 {
47         __pTimeSpanImpl = null;
48         __ticks = CalcTicks(days, hours, minutes, seconds, milliseconds);
49 }
50
51 TimeSpan::TimeSpan(long long ticks)
52         : __ticks(ticks)
53         , __pTimeSpanImpl(null)
54 {
55 }
56
57 TimeSpan::~TimeSpan(void)
58 {
59 }
60
61 TimeSpan
62 TimeSpan::operator +(const TimeSpan& rhs) const
63 {
64         return TimeSpan(__ticks + rhs.__ticks);
65 }
66
67 TimeSpan
68 TimeSpan::operator -(const TimeSpan& rhs) const
69 {
70         return TimeSpan(__ticks - rhs.__ticks);
71 }
72
73 TimeSpan&
74 TimeSpan::operator =(const TimeSpan& rhs)
75 {
76         if (&rhs != this)
77         {
78                 __ticks = rhs.__ticks;
79         }
80         return *this;
81 }
82
83 bool
84 TimeSpan::operator ==(const TimeSpan& rhs) const
85 {
86         return __ticks == rhs.__ticks;
87 }
88
89 bool
90 TimeSpan::operator !=(const TimeSpan& rhs) const
91 {
92         return __ticks != rhs.__ticks;
93 }
94
95 bool
96 TimeSpan::operator <(const TimeSpan& rhs) const
97 {
98         return __ticks < rhs.__ticks;
99 }
100
101 bool
102 TimeSpan::operator >(const TimeSpan& rhs) const
103 {
104         return __ticks > rhs.__ticks;
105 }
106
107 bool
108 TimeSpan::operator <=(const TimeSpan& rhs) const
109 {
110         return __ticks <= rhs.__ticks;
111 }
112
113 bool
114 TimeSpan::operator >=(const TimeSpan& rhs) const
115 {
116         return __ticks >= rhs.__ticks;
117 }
118
119 int
120 TimeSpan::Compare(const TimeSpan& t1, const TimeSpan& t2)
121 {
122         if ((t1.__ticks - t2.__ticks) > 0)
123         {
124                 return 1;
125         }
126         else if ((t1.__ticks - t2.__ticks) < 0)
127         {
128                 return -1;
129         }
130
131         return 0;
132 }
133
134 int
135 TimeSpan::CompareTo(const TimeSpan& value) const
136 {
137         return TimeSpan::Compare(*this, value);
138 }
139
140 bool
141 TimeSpan::Equals(const Object& obj) const
142 {
143         const TimeSpan* pOther = dynamic_cast< const TimeSpan* >(&obj);
144         if (pOther == null)
145         {
146                 return false;
147         }
148
149         return *this == *pOther;
150 }
151
152 TimeSpan
153 TimeSpan::Duration(void) const
154 {
155         return TimeSpan((__ticks >= 0 ? __ticks : -__ticks));
156 }
157
158 int
159 TimeSpan::GetHashCode(void) const
160 {
161         return(int((__ticks >> 32) ^ ((__ticks >> 32) >> 16)) ^ int((__ticks & 0x0000FFFF)));
162 }
163
164 TimeSpan
165 TimeSpan::Negate(void) const
166 {
167         return TimeSpan(-__ticks);
168 }
169
170 long long
171 TimeSpan::CalcTicks(int days, int hours, int minutes, int seconds, int milliseconds)
172 {
173         if (days < 0)
174         {
175                 days = 0;
176         }
177         if (hours < 0)
178         {
179                 hours = 0;
180         }
181         if (minutes < 0)
182         {
183                 minutes = 0;
184         }
185         if (seconds < 0)
186         {
187                 seconds = 0;
188         }
189         if (milliseconds < 0)
190         {
191                 milliseconds = 0;
192         }
193
194         return (long long) ((NUM_OF_TICKS_IN_DAY * days) + (NUM_OF_TICKS_IN_HOUR * hours) + (NUM_OF_TICKS_IN_MINUTE * minutes) +
195                                                 (NUM_OF_TICKS_IN_SECOND * seconds) + milliseconds);
196 }
197
198 long long
199 TimeSpan::GetDays(void) const
200 {
201         return (long long) (__ticks / NUM_OF_TICKS_IN_DAY);
202 }
203
204 long long
205 TimeSpan::GetHours(void) const
206 {
207         long long r = __ticks % NUM_OF_TICKS_IN_DAY;
208         if (r != 0)
209         {
210                 return (long long) (r / NUM_OF_TICKS_IN_HOUR);
211         }
212         return 0;
213 }
214
215 long long
216 TimeSpan::GetMinutes(void) const
217 {
218         long long r = __ticks % NUM_OF_TICKS_IN_DAY;
219         if (r != 0)
220         {
221                 r = r % NUM_OF_TICKS_IN_HOUR;
222                 if (r != 0)
223                 {
224                         return (long long) (r / NUM_OF_TICKS_IN_MINUTE);
225                 }
226                 return 0;
227         }
228         return 0;
229 }
230
231 long long
232 TimeSpan::GetSeconds(void) const
233 {
234         long long r = __ticks % NUM_OF_TICKS_IN_DAY;
235         if (r != 0)
236         {
237                 r = r % NUM_OF_TICKS_IN_HOUR;
238                 if (r != 0)
239                 {
240                         r = r % NUM_OF_TICKS_IN_MINUTE;
241                         if (r != 0)
242                         {
243                                 return (long long) (r / NUM_OF_TICKS_IN_SECOND);
244                         }
245                         return 0;
246                 }
247                 return 0;
248         }
249         return 0;
250 }
251
252 long long
253 TimeSpan::GetMilliseconds(void) const
254 {
255         long long r = __ticks % NUM_OF_TICKS_IN_DAY;
256         if (r != 0)
257         {
258                 r = r % NUM_OF_TICKS_IN_HOUR;
259                 if (r != 0)
260                 {
261                         r = r % NUM_OF_TICKS_IN_MINUTE;
262                         if (r != 0)
263                         {
264                                 r = r % NUM_OF_TICKS_IN_SECOND;
265                                 return(r);
266                         }
267                         return 0;
268                 }
269                 return 0;
270         }
271         return 0;
272 }
273
274 long long
275 TimeSpan::GetTicks(void) const
276 {
277         return(__ticks);
278 }
279
280 }} //Tizen::Base