summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2015-01-17 01:30:10 +0000
committerCarl Hetherington <cth@carlh.net>2015-01-20 11:20:49 +0000
commite70f560b63aa5c17d2f7f102f232cf4d5cd4c807 (patch)
treea501629c7e34dc7db53a34fd7f05e3c71e029235
parent0d9495a6e6b18d1d713f3dcfb89f87d9c791f11f (diff)
Add Time::all_as_seconds().
-rw-r--r--src/sub_time.cc10
-rw-r--r--src/sub_time.h2
-rw-r--r--test/time_test.cc10
3 files changed, 21 insertions, 1 deletions
diff --git a/src/sub_time.cc b/src/sub_time.cc
index e304310..3f4d1e9 100644
--- a/src/sub_time.cc
+++ b/src/sub_time.cc
@@ -152,3 +152,13 @@ Time::from_hms (int h, int m, int s, int ms)
{
return Time (h * 3600 + m * 60 + s, ms, Rational (1000, 1));
}
+
+double
+Time::all_as_seconds () const
+{
+ if (!_rate) {
+ throw UnknownFrameRateError ();
+ }
+
+ return _seconds + double (_frames) / _rate.get().fraction ();
+}
diff --git a/src/sub_time.h b/src/sub_time.h
index 7e1d193..22178e9 100644
--- a/src/sub_time.h
+++ b/src/sub_time.h
@@ -55,6 +55,8 @@ public:
int frames_at (Rational rate) const;
int milliseconds () const;
+ double all_as_seconds () const;
+
static Time from_hmsf (int h, int m, int s, int f, boost::optional<Rational> rate = boost::optional<Rational> ());
static Time from_hms (int h, int m, int s, int ms);
diff --git a/test/time_test.cc b/test/time_test.cc
index f57bb2c..76f9a7b 100644
--- a/test/time_test.cc
+++ b/test/time_test.cc
@@ -1,5 +1,5 @@
/*
- Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -18,6 +18,7 @@
*/
#include "sub_time.h"
+#include "exceptions.h"
#include <boost/test/unit_test.hpp>
/* Check time construction */
@@ -64,3 +65,10 @@ BOOST_AUTO_TEST_CASE (time_operator_test)
BOOST_CHECK_EQUAL (sub::Time::from_hms (0, 0, 5, 198 * 4), sub::Time::from_hms (0, 0, 5, 198 * 4));
BOOST_CHECK (sub::Time::from_hms (0, 0, 55, 332) != sub::Time::from_hms (0, 0, 58, 332));
}
+
+/* Check some other bits of Time */
+BOOST_AUTO_TEST_CASE (time_other_test)
+{
+ BOOST_CHECK_THROW (sub::Time::from_hmsf (2, 1, 58, 4).all_as_seconds(), sub::UnknownFrameRateError);
+ BOOST_CHECK_CLOSE (sub::Time::from_hmsf (2, 1, 58, 4, sub::Rational (24, 1)).all_as_seconds(), 7318.1667, 0.001);
+}