2007-08-08 04:50:02 +02:00
|
|
|
/*
|
2008-10-16 20:31:02 +02:00
|
|
|
Time - convenience wrapper for working with gavl_time in C++
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2008-03-10 04:25:03 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2007-08-08 04:50:02 +02:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
2007-08-08 04:50:02 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2007-08-08 04:50:02 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2007-08-08 04:50:02 +02:00
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
2010-12-21 02:05:13 +01:00
|
|
|
#include "lib/time/timevalue.hpp"
|
2009-07-11 20:06:35 +02:00
|
|
|
#include "lib/time.h"
|
2012-03-17 00:49:58 +01:00
|
|
|
#include "lib/util-quant.hpp"
|
2009-07-11 20:06:35 +02:00
|
|
|
|
2007-11-12 02:05:39 +01:00
|
|
|
#include <limits>
|
2009-07-11 20:06:35 +02:00
|
|
|
#include <string>
|
2010-12-31 05:59:53 +01:00
|
|
|
#include <boost/rational.hpp>
|
2007-11-12 02:05:39 +01:00
|
|
|
|
2009-07-11 20:06:35 +02:00
|
|
|
using std::string;
|
2012-03-17 00:49:58 +01:00
|
|
|
using util::floordiv;
|
2007-08-08 04:50:02 +02:00
|
|
|
|
|
|
|
|
|
2010-12-27 05:55:15 +01:00
|
|
|
namespace lib {
|
|
|
|
|
namespace time {
|
|
|
|
|
|
2010-12-31 05:59:53 +01:00
|
|
|
|
|
|
|
|
|
2011-01-08 20:01:26 +01:00
|
|
|
/** @note the allowed time range is explicitly limited to help overflow protection */
|
|
|
|
|
const Time Time::MAX ( TimeValue::buildRaw_(+std::numeric_limits<gavl_time_t>::max() / 30) );
|
|
|
|
|
const Time Time::MIN ( TimeValue::buildRaw_(-_raw(Time::MAX) ) );
|
2011-05-16 04:37:03 +02:00
|
|
|
const Time Time::ZERO;
|
2010-12-27 05:55:15 +01:00
|
|
|
|
2011-12-09 01:00:50 +01:00
|
|
|
const Time Time::ANYTIME(Time::MAX);
|
|
|
|
|
const Time Time::NEVER (Time::MIN);
|
|
|
|
|
|
2011-06-04 17:16:29 +02:00
|
|
|
const Offset Offset::ZERO (Time::ZERO);
|
|
|
|
|
|
|
|
|
|
|
2010-12-27 05:55:15 +01:00
|
|
|
|
2010-12-28 07:24:54 +01:00
|
|
|
/** convenience constructor to build an
|
|
|
|
|
* internal Lumiera Time value from the usual parts
|
2011-01-02 01:21:39 +01:00
|
|
|
* of an sexagesimal time specification. Arbitrary integral
|
2010-12-28 07:24:54 +01:00
|
|
|
* values are acceptable and will be summed up accordingly.
|
|
|
|
|
* The minute and hour part can be omitted.
|
|
|
|
|
* @warning internal Lumiera time values refer to an
|
|
|
|
|
* implementation dependent time origin/scale.
|
|
|
|
|
* The given value will be used as-is, without
|
|
|
|
|
* any further adjustments.
|
|
|
|
|
*/
|
|
|
|
|
Time::Time ( long millis
|
|
|
|
|
, uint secs
|
|
|
|
|
, uint mins
|
|
|
|
|
, uint hours
|
|
|
|
|
)
|
|
|
|
|
: TimeValue(lumiera_build_time (millis,secs,mins,hours))
|
2011-05-13 07:46:21 +02:00
|
|
|
{ }
|
2010-12-28 07:24:54 +01:00
|
|
|
|
|
|
|
|
|
2010-12-31 05:59:53 +01:00
|
|
|
/** convenience constructor to build an Time value
|
|
|
|
|
* from a fraction of seconds, given as rational number.
|
|
|
|
|
* An example would be to the time unit of a framerate.
|
|
|
|
|
*/
|
2011-01-02 18:56:44 +01:00
|
|
|
Time::Time (FSecs const& fractionalSeconds)
|
2011-01-09 04:56:46 +01:00
|
|
|
: TimeValue(lumiera_rational_to_time (fractionalSeconds))
|
2010-12-31 05:59:53 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
2010-12-28 07:24:54 +01:00
|
|
|
/** displaying an internal Lumiera Time value
|
|
|
|
|
* for diagnostic purposes or internal reporting.
|
|
|
|
|
* @warning internal Lumiera time values refer to an
|
|
|
|
|
* implementation dependent time origin/scale.
|
|
|
|
|
* @return string rendering of the actual, underlying
|
|
|
|
|
* implementation value, as \c h:m:s:ms
|
|
|
|
|
*/
|
|
|
|
|
Time::operator string() const
|
|
|
|
|
{
|
|
|
|
|
return string (lumiera_tmpbuf_print_time (t_));
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-05 18:15:44 +01:00
|
|
|
TimeVar::operator string() const
|
|
|
|
|
{
|
|
|
|
|
return string (lumiera_tmpbuf_print_time (t_));
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-28 07:24:54 +01:00
|
|
|
|
2011-01-08 20:01:26 +01:00
|
|
|
/** @internal backdoor to sneak in a raw time value
|
|
|
|
|
* bypassing any normalisation and limiting */
|
|
|
|
|
TimeValue
|
|
|
|
|
TimeValue::buildRaw_ (gavl_time_t raw)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<TimeValue const&> (raw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-12-28 07:24:54 +01:00
|
|
|
|
2011-01-09 04:56:46 +01:00
|
|
|
/** predefined constant for PAL framerate */
|
|
|
|
|
const FrameRate FrameRate::PAL (25);
|
2011-01-15 03:49:35 +01:00
|
|
|
const FrameRate FrameRate::NTSC (30000,1001);
|
2011-01-09 04:56:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @return time span of one frame of this rate,
|
|
|
|
|
* cast into internal Lumiera time scale */
|
|
|
|
|
Duration
|
|
|
|
|
FrameRate::duration() const
|
|
|
|
|
{
|
|
|
|
|
if (0 == *this)
|
|
|
|
|
throw error::Logic ("Impossible to quantise to an zero spaced frame grid"
|
|
|
|
|
, error::LUMIERA_ERROR_BOTTOM_VALUE);
|
|
|
|
|
|
|
|
|
|
return Duration (1, *this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 00:49:58 +01:00
|
|
|
Offset
|
|
|
|
|
operator* (boost::rational<int64_t> factor, Offset const& o)
|
|
|
|
|
{
|
|
|
|
|
boost::rational<int64_t> distance (_raw(o));
|
|
|
|
|
distance *= factor;
|
|
|
|
|
gavl_time_t microTicks = floordiv (distance.numerator(), distance.denominator());
|
|
|
|
|
return Offset(TimeValue(microTicks));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-06-11 20:20:20 +02:00
|
|
|
/** offset by the given number of frames. */
|
|
|
|
|
Offset::Offset (int64_t count, FrameRate const& fps)
|
|
|
|
|
: TimeValue (count? (count<0? -1:+1) * lumiera_framecount_to_time (::abs(count), fps)
|
|
|
|
|
: _raw(Duration::NIL))
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
/** duration of the given number of frames.
|
|
|
|
|
* @note always positive; count used absolute */
|
|
|
|
|
Duration::Duration (int64_t count, FrameRate const& fps)
|
|
|
|
|
: TimeValue (count? lumiera_framecount_to_time (abs(count), fps) : _raw(Duration::NIL))
|
2011-01-09 04:56:46 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** constant to indicate "no duration" */
|
2011-06-08 02:12:38 +02:00
|
|
|
const Duration Duration::NIL (Time::ZERO);
|
2011-01-09 04:56:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-27 05:55:15 +01:00
|
|
|
}} // namespace lib::Time
|