2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "Temperature.h"
11 #include "utils/Archive.h"
12 #include "utils/StringUtils.h"
16 CTemperature::CTemperature()
22 CTemperature::CTemperature(const CTemperature
& temperature
)
24 m_value
=temperature
.m_value
;
25 m_valid
=temperature
.m_valid
;
28 CTemperature::CTemperature(double value
)
34 bool CTemperature::operator >(const CTemperature
& right
) const
37 assert(right
.IsValid());
39 if (!IsValid() || !right
.IsValid())
45 return (m_value
>right
.m_value
);
48 bool CTemperature::operator >=(const CTemperature
& right
) const
50 return operator >(right
) || operator ==(right
);
53 bool CTemperature::operator <(const CTemperature
& right
) const
56 assert(right
.IsValid());
58 if (!IsValid() || !right
.IsValid())
64 return (m_value
<right
.m_value
);
67 bool CTemperature::operator <=(const CTemperature
& right
) const
69 return operator <(right
) || operator ==(right
);
72 bool CTemperature::operator ==(const CTemperature
& right
) const
75 assert(right
.IsValid());
77 if (!IsValid() || !right
.IsValid())
83 return (m_value
==right
.m_value
);
86 bool CTemperature::operator !=(const CTemperature
& right
) const
88 return !operator ==(right
.m_value
);
91 CTemperature
& CTemperature::operator =(const CTemperature
& right
)
93 m_valid
=right
.m_valid
;
94 m_value
=right
.m_value
;
98 const CTemperature
& CTemperature::operator +=(const CTemperature
& right
)
101 assert(right
.IsValid());
103 m_value
+=right
.m_value
;
107 const CTemperature
& CTemperature::operator -=(const CTemperature
& right
)
110 assert(right
.IsValid());
112 m_value
-=right
.m_value
;
116 const CTemperature
& CTemperature::operator *=(const CTemperature
& right
)
119 assert(right
.IsValid());
121 m_value
*=right
.m_value
;
125 const CTemperature
& CTemperature::operator /=(const CTemperature
& right
)
128 assert(right
.IsValid());
130 m_value
/=right
.m_value
;
134 CTemperature
CTemperature::operator +(const CTemperature
& right
) const
137 assert(right
.IsValid());
139 CTemperature
temp(*this);
141 if (!IsValid() || !right
.IsValid())
142 temp
.SetValid(false);
144 temp
.m_value
+=right
.m_value
;
149 CTemperature
CTemperature::operator -(const CTemperature
& right
) const
152 assert(right
.IsValid());
154 CTemperature
temp(*this);
155 if (!IsValid() || !right
.IsValid())
156 temp
.SetValid(false);
158 temp
.m_value
-=right
.m_value
;
163 CTemperature
CTemperature::operator *(const CTemperature
& right
) const
166 assert(right
.IsValid());
168 CTemperature
temp(*this);
169 if (!IsValid() || !right
.IsValid())
170 temp
.SetValid(false);
172 temp
.m_value
*=right
.m_value
;
176 CTemperature
CTemperature::operator /(const CTemperature
& right
) const
179 assert(right
.IsValid());
181 CTemperature
temp(*this);
182 if (!IsValid() || !right
.IsValid())
183 temp
.SetValid(false);
185 temp
.m_value
/=right
.m_value
;
189 CTemperature
& CTemperature::operator ++()
197 CTemperature
& CTemperature::operator --()
205 CTemperature
CTemperature::operator ++(int)
209 CTemperature
temp(*this);
214 CTemperature
CTemperature::operator --(int)
218 CTemperature
temp(*this);
223 bool CTemperature::operator >(double right
) const
230 return (m_value
>right
);
233 bool CTemperature::operator >=(double right
) const
235 return operator >(right
) || operator ==(right
);
238 bool CTemperature::operator <(double right
) const
245 return (m_value
<right
);
248 bool CTemperature::operator <=(double right
) const
250 return operator <(right
) || operator ==(right
);
253 bool CTemperature::operator ==(double right
) const
258 return (m_value
==right
);
261 bool CTemperature::operator !=(double right
) const
263 return !operator ==(right
);
266 const CTemperature
& CTemperature::operator +=(double right
)
274 const CTemperature
& CTemperature::operator -=(double right
)
282 const CTemperature
& CTemperature::operator *=(double right
)
290 const CTemperature
& CTemperature::operator /=(double right
)
298 CTemperature
CTemperature::operator +(double right
) const
302 CTemperature
temp(*this);
307 CTemperature
CTemperature::operator -(double right
) const
311 CTemperature
temp(*this);
316 CTemperature
CTemperature::operator *(double right
) const
320 CTemperature
temp(*this);
325 CTemperature
CTemperature::operator /(double right
) const
329 CTemperature
temp(*this);
334 CTemperature
CTemperature::CreateFromFahrenheit(double value
)
336 return CTemperature(value
);
339 CTemperature
CTemperature::CreateFromReaumur(double value
)
341 return CTemperature(value
* 2.25 + 32.0);
344 CTemperature
CTemperature::CreateFromRankine(double value
)
346 return CTemperature(value
- 459.67);
349 CTemperature
CTemperature::CreateFromRomer(double value
)
351 return CTemperature((value
- 7.5) * 24.0 / 7.0 + 32.0);
354 CTemperature
CTemperature::CreateFromDelisle(double value
)
356 CTemperature
temp(212.0 - value
* 1.2);
360 CTemperature
CTemperature::CreateFromNewton(double value
)
362 return CTemperature(value
* 60.0 / 11.0 + 32.0);
365 CTemperature
CTemperature::CreateFromCelsius(double value
)
367 return CTemperature(value
* 1.8 + 32.0);
370 CTemperature
CTemperature::CreateFromKelvin(double value
)
372 return CTemperature((value
- 273.15) * 1.8 + 32.0);
375 void CTemperature::Archive(CArchive
& ar
)
389 bool CTemperature::IsValid() const
394 double CTemperature::ToFahrenheit() const
399 double CTemperature::ToKelvin() const
401 return (m_value
+ 459.67) / 1.8;
404 double CTemperature::ToCelsius() const
406 return (m_value
- 32.0) / 1.8;
409 double CTemperature::ToReaumur() const
411 return (m_value
- 32.0) / 2.25;
414 double CTemperature::ToRankine() const
416 return m_value
+ 459.67;
419 double CTemperature::ToRomer() const
421 return (m_value
- 32.0) * 7.0 / 24.0 + 7.5;
424 double CTemperature::ToDelisle() const
426 return (212.0 - m_value
) * 5.0 / 6.0;
429 double CTemperature::ToNewton() const
431 return (m_value
- 32.0) * 11.0 / 60.0;
434 double CTemperature::To(Unit temperatureUnit
) const
441 switch (temperatureUnit
)
444 value
=ToFahrenheit();
474 // Returns temperature as localized string
475 std::string
CTemperature::ToString(Unit temperatureUnit
) const
480 return StringUtils::Format("{:2.0f}", To(temperatureUnit
));