add component access to the time wrapper (quick'n dirty)

This commit is contained in:
Fischlurch 2009-07-12 22:51:04 +02:00
parent 66f7b1be17
commit 30db042b16
7 changed files with 125 additions and 4 deletions

View file

@ -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);
}

View file

@ -39,6 +39,44 @@ namespace lumiera {
const Time Time::MIN ( -std::numeric_limits<int64_t>::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_));

View file

@ -25,6 +25,7 @@
#define LUMIERA_LUMITIME_H
#include <boost/operators.hpp>
#include <string>
extern "C" {
#include <gavl/gavltime.h>
@ -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; }

View file

@ -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;
}

View file

@ -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

View file

@ -423,6 +423,7 @@ END
TEST "Time Wrapper" TimeWrapper_test <<END
out: ..:..:..\....
return: 0
END

View file

@ -25,12 +25,18 @@
#include "lib/util.hpp"
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <cstdlib>
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());
}
};