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