• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18
19// -*- c++ -*-
20// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
21
22//               O S C L _ T I M E   ( T I M E   F U N C T I O N S )
23
24// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
25
26#include "oscl_int64_utils.h"
27
28//------ NTPTime ------------
29OSCL_COND_EXPORT_REF OSCL_INLINE NTPTime::NTPTime()
30{
31    set_to_current_time();
32}
33
34OSCL_COND_EXPORT_REF OSCL_INLINE NTPTime::NTPTime(const NTPTime& src)
35{
36    timevalue = src.timevalue;
37}
38
39OSCL_COND_EXPORT_REF OSCL_INLINE NTPTime::NTPTime(const uint32 seconds)
40{
41    Oscl_Int64_Utils::set_uint64(timevalue, seconds, 0);
42}
43
44OSCL_COND_EXPORT_REF OSCL_INLINE NTPTime::NTPTime(const int32 seconds)
45{
46    if (seconds < 0) return;
47    timevalue = uint64(seconds) << 32;
48}
49
50OSCL_COND_EXPORT_REF OSCL_INLINE NTPTime::NTPTime(const uint64 value)
51{
52    timevalue = value;
53}
54
55OSCL_COND_EXPORT_REF OSCL_INLINE NTPTime& NTPTime::operator=(uint32 newval)
56{
57    Oscl_Int64_Utils::set_uint64(timevalue, newval, 0);
58    return *this;
59}
60
61OSCL_COND_EXPORT_REF OSCL_INLINE NTPTime& NTPTime::operator=(uint64 newval)
62{
63    timevalue = newval;
64    return *this;
65}
66
67OSCL_COND_EXPORT_REF OSCL_INLINE NTPTime& NTPTime::operator+=(uint64 val)
68{
69    timevalue += val;
70    return *this;
71}
72
73OSCL_COND_EXPORT_REF OSCL_INLINE NTPTime NTPTime::operator-(const NTPTime &ntpt) const
74{
75    NTPTime temp;
76    temp.timevalue = timevalue - ntpt.timevalue;
77    return temp;
78}
79
80OSCL_COND_EXPORT_REF OSCL_INLINE void NTPTime::set_from_system_time(const uint32 systemtime)
81{
82    timevalue = int64(systemtime + 2208988800U) << 32;
83}
84
85OSCL_COND_EXPORT_REF OSCL_INLINE uint32 NTPTime::get_middle32() const
86{
87    return Oscl_Int64_Utils::get_uint64_middle32(timevalue);
88}
89
90OSCL_COND_EXPORT_REF OSCL_INLINE uint32 NTPTime::get_upper32() const
91{
92    return Oscl_Int64_Utils::get_uint64_upper32(timevalue);
93}
94
95OSCL_COND_EXPORT_REF OSCL_INLINE uint32 NTPTime::get_lower32() const
96{
97    return Oscl_Int64_Utils::get_uint64_lower32(timevalue);
98}
99
100OSCL_COND_EXPORT_REF OSCL_INLINE int32 NTPTime::to_system_time() const
101{
102    return (uint32)((timevalue >> 32) - 2208988800U);
103}
104
105OSCL_COND_EXPORT_REF OSCL_INLINE uint64 NTPTime::get_value() const
106{
107    return timevalue;
108}
109
110//------ TimeValue ----------
111OSCL_COND_EXPORT_REF OSCL_INLINE bool TimeValue::is_zero()
112{
113    return ((ts.tv_usec == 0) && (ts.tv_sec == 0));
114}
115
116OSCL_COND_EXPORT_REF OSCL_INLINE uint32 TimeValue::get_sec() const
117{
118    return ts.tv_sec;
119};
120
121
122OSCL_COND_EXPORT_REF OSCL_INLINE int32 TimeValue::to_msec() const
123{
124    return ((ts.tv_usec / 1000) + (ts.tv_sec * 1000));
125}
126
127OSCL_COND_EXPORT_REF OSCL_INLINE uint32 TimeValue::get_usec() const
128{
129    return ts.tv_usec;
130}
131
132OSCL_COND_EXPORT_REF OSCL_INLINE TimeValue operator -(const TimeValue& a, const TimeValue& b)
133{
134    TimeValue c;
135    c = a;
136    c -= b;
137    return c;
138}
139
140// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141
142OSCL_COND_EXPORT_REF OSCL_INLINE TimeValue& TimeValue::operator =(const TimeValue & a)
143{
144    this->ts.tv_usec = a.ts.tv_usec;
145    this->ts.tv_sec = a.ts.tv_sec;
146    return *this;
147}
148
149// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
150
151OSCL_COND_EXPORT_REF OSCL_INLINE TimeValue& TimeValue::operator +=(const TimeValue & a)
152{
153    this->ts.tv_usec += a.ts.tv_usec;
154    this->ts.tv_sec += a.ts.tv_sec;
155
156    if (this->ts.tv_usec >= USEC_PER_SEC)
157    {
158        this->ts.tv_usec -= USEC_PER_SEC;
159        ++this->ts.tv_sec;
160    }
161    return *this;
162}
163
164// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
165
166
167OSCL_COND_EXPORT_REF OSCL_INLINE TimeValue& TimeValue::operator -=(const TimeValue & a)
168{
169    this->ts.tv_usec -= a.ts.tv_usec;
170    this->ts.tv_sec -= a.ts.tv_sec;
171
172    if ((this->ts.tv_sec > 0) && (this->ts.tv_usec < 0))
173    {
174        this->ts.tv_usec += USEC_PER_SEC;
175        --this->ts.tv_sec;
176    }
177    return *this;
178}
179
180// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
181
182
183OSCL_COND_EXPORT_REF OSCL_INLINE TimeValue& TimeValue::operator *=(const int scale)
184{
185    this->ts.tv_usec *= scale;
186    this->ts.tv_sec *= scale;
187    unsigned long secs = this->ts.tv_usec / USEC_PER_SEC;
188    this->ts.tv_sec += secs;
189    this->ts.tv_usec -= secs * USEC_PER_SEC;
190    return *this;
191}
192
193// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
194
195
196OSCL_COND_EXPORT_REF OSCL_INLINE bool operator ==(const TimeValue& a, const TimeValue& b)
197{
198    return timercmp(&a.ts, &b.ts, ==);
199}
200
201// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
202
203OSCL_COND_EXPORT_REF OSCL_INLINE bool operator !=(const TimeValue& a, const TimeValue& b)
204{
205    return timercmp(&a.ts, &b.ts, !=);
206}
207
208// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
209
210OSCL_COND_EXPORT_REF OSCL_INLINE bool operator <=(const TimeValue& a, const TimeValue& b)
211{
212
213    return !timercmp(&a.ts, &b.ts, >);
214}
215
216// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
217
218OSCL_COND_EXPORT_REF OSCL_INLINE bool operator >=(const TimeValue& a, const TimeValue& b)
219{
220    return !timercmp(&a.ts, &b.ts, <);
221}
222
223// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
224
225
226OSCL_COND_EXPORT_REF OSCL_INLINE bool operator >(const TimeValue& a, const TimeValue& b)
227{
228    return timercmp(&a.ts, &b.ts, >);
229}
230
231// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
232
233OSCL_COND_EXPORT_REF OSCL_INLINE bool operator <(const TimeValue& a, const TimeValue& b)
234{
235
236    return timercmp(&a.ts, &b.ts, <);
237}
238
239// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
240OSCL_COND_EXPORT_REF OSCL_INLINE void TimeValue::set_to_current_time()
241{
242    gettimeofday(&ts, NULL);
243}
244
245// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
246#include "oscl_mem_basic_functions.h"
247
248OSCL_COND_EXPORT_REF OSCL_INLINE void TimeValue::set_to_zero()
249{
250    oscl_memset(&ts, 0, sizeof(ts));
251}
252
253// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
254
255OSCL_COND_EXPORT_REF OSCL_INLINE TimeValue::TimeValue()
256{
257    gettimeofday(&ts, NULL);
258}
259
260
261// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
262
263OSCL_COND_EXPORT_REF OSCL_INLINE TimeValue::TimeValue(const TimeValue& Tv)
264{
265    ts.tv_usec = Tv.ts.tv_usec;
266    ts.tv_sec  = Tv.ts.tv_sec;
267}
268
269
270// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
271OSCL_COND_EXPORT_REF OSCL_INLINE TimeValue::TimeValue(const OsclBasicTimeStruct& in_tv)
272{
273    ts.tv_usec = in_tv.tv_usec;
274    ts.tv_sec = in_tv.tv_sec;
275}
276
277// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
278OSCL_COND_EXPORT_REF OSCL_INLINE TimeValue::TimeValue(long num_units, TimeUnits units)
279{
280    ts.tv_sec = num_units / MapToSeconds[units];
281    long diff = num_units - MapToSeconds[units] * ts.tv_sec;
282    ts.tv_usec = diff * MapToUSeconds[units];
283}
284
285// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
286OSCL_COND_EXPORT_REF OSCL_INLINE void TimeValue::set_from_ntp_time(const uint32 ntp_offset_seconds)
287{
288    ts.tv_sec = ntp_offset_seconds - unix_ntp_offset;
289    ts.tv_usec = 0;
290}
291
292// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
293OSCL_COND_EXPORT_REF OSCL_INLINE TimeValue::TimeValue(OsclBasicDateTimeStruct ints)
294{
295    ts.tv_sec = mktime(&ints);
296    ts.tv_usec = 0;
297}
298
299// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
300
301OSCL_COND_EXPORT_REF OSCL_INLINE int32 TimeValue::get_local_time()
302{
303    // Daylight saving time offset of 1 hour in the summer
304    uint dst_offset = 60 * 60;
305    int32 GMTime = ts.tv_sec;
306
307    struct tm *timeptr;
308    struct tm buffer;
309    int dstFlag;
310    timeptr = localtime_r(&ts.tv_sec, &buffer);
311
312    GMTime -= timeptr->tm_gmtoff;
313
314    dstFlag = timeptr->tm_isdst;
315
316    if (dstFlag == 1)
317    {
318        // Daylight saving time. Add an hour's offset to get GMT
319        GMTime += dst_offset;
320    }
321
322    return GMTime;
323}
324
325OSCL_COND_EXPORT_REF OSCL_INLINE OsclBasicTimeStruct * TimeValue::get_timeval_ptr()
326{
327    return &ts;
328}
329
330// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
331
332// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
333
334