Move time conversion logic from lumitime.cpp to time.c

This commit is contained in:
Stefan Kangas 2010-12-06 16:18:54 +01:00
parent fd6492d745
commit 41e6750163
4 changed files with 64 additions and 16 deletions

View file

@ -47,41 +47,45 @@ namespace lumiera {
) )
: t_(lumiera_build_time (millis,secs,mins,hours)) : t_(lumiera_build_time (millis,secs,mins,hours))
{ } { }
int int
Time::getMillis() const Time::getMillis() const
{ {
return (t_ / (GAVL_TIME_SCALE / 1000)) % 1000; return lumiera_time_millis(t_);
} }
int int
Time::getSecs() const Time::getSecs() const
{ {
return (t_ / (GAVL_TIME_SCALE / 1 )) % 60; return lumiera_time_seconds(t_);
} }
int int
Time::getMins() const Time::getMins() const
{ {
return (t_ / (60 * GAVL_TIME_SCALE)) % 60; return lumiera_time_minutes(t_);
} }
int int
Time::getHours() const Time::getHours() const
{ {
return (t_ / (gavl_time_t(60) * 60 * GAVL_TIME_SCALE)); return lumiera_time_hours(t_);
} }
int
Time::getFrames() const
{
// TODO
return 0;
}
Time::operator string() const Time::operator string() const
{ {
return string (lumiera_tmpbuf_print_time (t_)); return string (lumiera_tmpbuf_print_time (t_));
} }
} // namespace lumiera } // namespace lumiera

View file

@ -85,8 +85,8 @@ namespace lumiera {
int getSecs () const; int getSecs () const;
int getMins () const; int getMins () const;
int getHours () const; int getHours () const;
int getFrames () const;
operator std::string () const; operator std::string () const;
operator gavl_time_t () const { return t_; } operator gavl_time_t () const { return t_; }

View file

@ -23,12 +23,16 @@
#include "lib/time.h" #include "lib/time.h"
#include "lib/tmpbuf.h" #include "lib/tmpbuf.h"
/* GAVL_TIME_SCALE is the correct factor or dividend when using gavl_time_t for
* units of whole seconds from gavl_time_t. Since we want to use milliseconds,
* we need to multiply or divide by 1000 to get correct results. */
#define GAVL_TIME_SCALE_MS (GAVL_TIME_SCALE / 1000)
char* char*
lumiera_tmpbuf_print_time (gavl_time_t time) lumiera_tmpbuf_print_time (gavl_time_t time)
{ {
int milliseconds, seconds, minutes, hours; int milliseconds, seconds, minutes, hours;
int negative; int negative;
if(time < 0) if(time < 0)
{ {
@ -37,7 +41,7 @@ lumiera_tmpbuf_print_time (gavl_time_t time)
} }
else negative = 0; else negative = 0;
time /= GAVL_TIME_SCALE / 1000; time /= GAVL_TIME_SCALE_MS;
milliseconds = time % 1000; milliseconds = time % 1000;
time /= 1000; time /= 1000;
seconds = time % 60; seconds = time % 60;
@ -53,16 +57,37 @@ lumiera_tmpbuf_print_time (gavl_time_t time)
return buffer; return buffer;
} }
gavl_time_t gavl_time_t
lumiera_build_time (long millis, uint secs, uint mins, uint hours) lumiera_build_time(long millis, uint secs, uint mins, uint hours)
{ {
gavl_time_t time = millis gavl_time_t time = millis
+ 1000 * secs + 1000 * secs
+ 1000 * 60 * mins + 1000 * 60 * mins
+ 1000 * 60 * 60 * hours; + 1000 * 60 * 60 * hours;
time *= GAVL_TIME_SCALE / 1000; time *= GAVL_TIME_SCALE_MS;
return time; return time;
} }
int
lumiera_time_hours(gavl_time_t time)
{
return time / GAVL_TIME_SCALE_MS / 1000 / 60 / 60;
}
int
lumiera_time_minutes(gavl_time_t time)
{
return (time / GAVL_TIME_SCALE_MS / 1000 / 60) % 60;
}
int
lumiera_time_seconds(gavl_time_t time)
{
return (time / GAVL_TIME_SCALE_MS / 1000) % 60;
}
int
lumiera_time_millis(gavl_time_t time)
{
return (time / GAVL_TIME_SCALE_MS) % 1000;
}

View file

@ -34,12 +34,31 @@
char* char*
lumiera_tmpbuf_print_time (gavl_time_t time); lumiera_tmpbuf_print_time (gavl_time_t time);
/** /**
* Builds a time value by summing up the given components. * Builds a time value by summing up the given components.
*/ */
gavl_time_t gavl_time_t
lumiera_build_time (long millis, uint secs, uint mins, uint hours); lumiera_build_time (long millis, uint secs, uint mins, uint hours);
/**
* Get the hour part of given time.
*/
int lumiera_time_hours(gavl_time_t time);
/**
* Get the minute part of given time.
*/
int lumiera_time_minutes(gavl_time_t time);
/**
* Get the seconds part of given time.
*/
int lumiera_time_seconds(gavl_time_t time);
/**
* Get the milliseconds part of given time.
*/
int lumiera_time_millis(gavl_time_t time);
#endif #endif