AVPKit
TimeValue.cpp
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 
20 /*
21  * TimeValue.cpp
22  *
23  * Created on: Sep 19, 2008
24  * Author: aclarke
25  */
26 
27 #include <climits>
28 #include "TimeValue.h"
29 
30 namespace com { namespace avpkit { namespace core {
31 
32 static int64_t sConversionTable[7][7] =
33 { /* DY, HR, MI, SE, MS, US, NS */
34  /* DY */ { 1, 24, 1440, 86400, 86400000, 86400000000LL, 86400000000000LL },
35  /* HR */ { 0, 1, 60, 3600, 3600000, 3600000000LL, 3600000000000LL },
36  /* MI */ { 0, 0, 1, 60, 60000, 60000000LL, 60000000000LL },
37  /* SE */ { 0, 0, 0, 1, 1000, 1000000LL, 1000000000LL },
38  /* MS */ { 0, 0, 0, 0, 1, 1000LL, 1000000LL },
39  /* US */ { 0, 0, 0, 0, 0, 1LL, 1000LL },
40  /* NS */ { 0, 0, 0, 0, 0, 0LL, 1LL },
41 };
42 
43 TimeValue::TimeValue()
44 {
45  mValue = 0;
46  mUnit = NANOSECONDS;
47 }
48 
49 TimeValue::~TimeValue()
50 {
51 }
52 
53 void
54 TimeValue::set(int64_t value, Unit unit)
55 {
56  mValue = value;
57  mUnit = unit;
58 }
59 
60 TimeValue*
61 TimeValue::make(int64_t value, Unit unit)
62 {
63  TimeValue *retval = make();
64  if (retval)
65  retval->set(value, unit);
66  return retval;
67 }
68 
69 TimeValue*
70 TimeValue::make(TimeValue * src)
71 {
72  if (!src)
73  return 0;
74  return make(src->mValue, src->mUnit);
75 }
76 
77 ITimeValue::Unit
78 TimeValue::getNativeUnit()
79 {
80  return mUnit;
81 }
82 
83 int64_t
84 TimeValue::get(Unit aUnit)
85 {
86  if (mUnit == aUnit)
87  return mValue;
88 
89  int64_t retval = 0;
90  bool multiply = false;
91  int64_t factor = sConversionTable[aUnit][mUnit];
92  if (!factor)
93  {
94  factor = sConversionTable[mUnit][aUnit];
95  multiply = true;
96  }
97  if (multiply)
98  retval = mValue * factor;
99  else
100  retval = mValue / factor;
101 
102  return retval;
103 }
104 
105 int32_t
107 {
108  int32_t retval = -1;
109  TimeValue* that = dynamic_cast<TimeValue*>(aThat);
110  if (that)
111  {
112  // convert both to NANOSECONDS
113  Unit thisUnit = this->getNativeUnit();
114  Unit thatUnit = that->getNativeUnit();
115 
116  // NOTE: Order of the entires in the Enum matters here.
117  Unit minUnit = thisUnit > thatUnit ? thisUnit : thatUnit;
118 
119  int64_t thisValue = this->get(minUnit);
120  int64_t thatValue = that->get(minUnit);
121  retval = TimeValue::compare(thisValue, thatValue);
122  }
123  return retval;
124 }
125 
126 }}}
Time (duration) values with units.
Definition: ITimeValue.h:43
virtual int32_t compareTo(ITimeValue *other)
Compare this timeValue to another.
Definition: TimeValue.cpp:106
virtual int64_t get(Unit unit)
Get the value of this ITimeValue, in the specified Unit.
Definition: TimeValue.cpp:84
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...