From 30db042b161280eb6959e4f09c36aff01282d931 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sun, 12 Jul 2009 22:51:04 +0200 Subject: [PATCH] add component access to the time wrapper (quick'n dirty) --- src/lib/lumitime-fmt.hpp | 2 +- src/lib/lumitime.cpp | 38 +++++++++++++++++++++++++++++ src/lib/lumitime.hpp | 22 ++++++++++++++--- src/lib/time.c | 13 ++++++++++ src/lib/time.h | 7 ++++++ tests/40components.tests | 1 + tests/lib/timewrappertest.cpp | 46 +++++++++++++++++++++++++++++++++++ 7 files changed, 125 insertions(+), 4 deletions(-) diff --git a/src/lib/lumitime-fmt.hpp b/src/lib/lumitime-fmt.hpp index c7bab597b..bb8066ebe 100644 --- a/src/lib/lumitime-fmt.hpp +++ b/src/lib/lumitime-fmt.hpp @@ -58,7 +58,7 @@ namespace lumiera { /** writes time value, formatted as HH:MM:SS:mmm * @see lumiera_tmpbuf_print_time */ inline std::ostream& - operator<< (ostream& os, Time const& t) + operator<< (std::ostream& os, Time const& t) { return os << lumiera_tmpbuf_print_time(t); } diff --git a/src/lib/lumitime.cpp b/src/lib/lumitime.cpp index 0f9d993d5..29facef22 100644 --- a/src/lib/lumitime.cpp +++ b/src/lib/lumitime.cpp @@ -39,6 +39,44 @@ namespace lumiera { const Time Time::MIN ( -std::numeric_limits::max() ); + + Time::Time ( long millis + , uint secs + , uint mins + , uint hours + ) + : t_(lumiera_build_time (millis,secs,mins,hours)) + { } + + + int + Time::getMillis() const + { + return (t_ / (GAVL_TIME_SCALE / 1000)) % 1000; + } + + + int + Time::getSecs() const + { + return (t_ / (GAVL_TIME_SCALE / 1 )) % 60; + } + + + int + Time::getMins() const + { + return (t_ / (60 * GAVL_TIME_SCALE)) % 60; + } + + + int + Time::getHours() const + { + return (t_ / (60L * 60 * GAVL_TIME_SCALE)); + } + + Time::operator string() const { return string (lumiera_tmpbuf_print_time (t_)); diff --git a/src/lib/lumitime.hpp b/src/lib/lumitime.hpp index f1503f848..6891ce064 100644 --- a/src/lib/lumitime.hpp +++ b/src/lib/lumitime.hpp @@ -25,6 +25,7 @@ #define LUMIERA_LUMITIME_H #include +#include extern "C" { #include @@ -67,11 +68,26 @@ namespace lumiera { static const Time MAX ; static const Time MIN ; - explicit Time (gavl_time_t val=0) : t_(val) {} + explicit + Time (gavl_time_t val=0) + : t_(val) + { } + Time ( long millis + , uint secs + , uint mins =0 + , uint hours=0 + ); + + // component access + int getMillis () const; + int getSecs () const; + int getMins () const; + int getHours () const; + + + operator std::string () const; operator gavl_time_t () const { return t_; } - operator string() const; - // Supporting additive Time& operator+= (Time const& tx) { t_ += tx.t_; return *this; } diff --git a/src/lib/time.c b/src/lib/time.c index 22340ee77..18f093fa2 100644 --- a/src/lib/time.c +++ b/src/lib/time.c @@ -53,3 +53,16 @@ lumiera_tmpbuf_print_time (gavl_time_t time) return buffer; } + +gavl_time_t +lumiera_build_time (long millis, uint secs, uint mins, uint hours) +{ + gavl_time_t time = millis + + 1000 * secs + + 1000 * 60 * mins + + 1000 * 60 * 60 * hours; + time *= GAVL_TIME_SCALE / 1000; + return time; +} + + diff --git a/src/lib/time.h b/src/lib/time.h index 167571dd5..5a6034a20 100644 --- a/src/lib/time.h +++ b/src/lib/time.h @@ -34,5 +34,12 @@ char* lumiera_tmpbuf_print_time (gavl_time_t time); + +/** + * Builds a time value by summing up the given components. + */ +gavl_time_t +lumiera_build_time (long millis, uint secs, uint mins, uint hours); + #endif diff --git a/tests/40components.tests b/tests/40components.tests index daca5bee5..379df32c1 100644 --- a/tests/40components.tests +++ b/tests/40components.tests @@ -423,6 +423,7 @@ END TEST "Time Wrapper" TimeWrapper_test < +#include +#include using boost::lexical_cast; using util::isnil; +using std::rand; +using std::cout; +using std::endl; #include "lib/lumitime.hpp" +#include "lib/lumitime-fmt.hpp" @@ -52,6 +58,7 @@ namespace lumiera { checkBasics (ref); checkComparisons (ref); + checkComponentAccess(); } @@ -116,6 +123,45 @@ namespace lumiera { ASSERT (!(val < gat) ); ASSERT (!(val > gat) ); } + + + void + checkComponentAccess() + { + int millis = rand() % 1000; + int secs = rand() % 60; + int mins = rand() % 60; + int hours = rand() % 100; + + Time time(millis,secs,mins,hours); + ASSERT (millis == time.getMillis()); + ASSERT (secs == time.getSecs()); + ASSERT (mins == time.getMins()); + ASSERT (hours == time.getHours()); + cout << time << endl; + + Time t2(2008,0); + cout << t2 << endl; + ASSERT ( 8 == t2.getMillis()); + ASSERT ( 2 == t2.getSecs()); + ASSERT ( 0 == t2.getMins()); + ASSERT ( 0 == t2.getHours()); + + Time t3(2008,88); + cout << t3 << endl; + ASSERT ( 8 == t3.getMillis()); + ASSERT (30 == t3.getSecs()); + ASSERT ( 1 == t3.getMins()); + ASSERT ( 0 == t3.getHours()); + + Time t4(2008,118,58); + cout << t4 << endl; + ASSERT ( 8 == t4.getMillis()); + ASSERT ( 0 == t4.getSecs()); + ASSERT ( 0 == t4.getMins()); + ASSERT ( 1 == t4.getHours()); + } + };