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))
{ }
int
Time::getMillis() const
{
return (t_ / (GAVL_TIME_SCALE / 1000)) % 1000;
return lumiera_time_millis(t_);
}
int
Time::getSecs() const
{
return (t_ / (GAVL_TIME_SCALE / 1 )) % 60;
return lumiera_time_seconds(t_);
}
int
Time::getMins() const
{
return (t_ / (60 * GAVL_TIME_SCALE)) % 60;
return lumiera_time_minutes(t_);
}
int
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
{
return string (lumiera_tmpbuf_print_time (t_));
}
} // namespace lumiera

View file

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

View file

@ -23,12 +23,16 @@
#include "lib/time.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*
lumiera_tmpbuf_print_time (gavl_time_t time)
{
int milliseconds, seconds, minutes, hours;
int negative;
if(time < 0)
{
@ -37,7 +41,7 @@ lumiera_tmpbuf_print_time (gavl_time_t time)
}
else negative = 0;
time /= GAVL_TIME_SCALE / 1000;
time /= GAVL_TIME_SCALE_MS;
milliseconds = time % 1000;
time /= 1000;
seconds = time % 60;
@ -53,16 +57,37 @@ lumiera_tmpbuf_print_time (gavl_time_t time)
return buffer;
}
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
+ 1000 * secs
+ 1000 * 60 * mins
+ 1000 * 60 * 60 * hours;
time *= GAVL_TIME_SCALE / 1000;
time *= GAVL_TIME_SCALE_MS;
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*
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);
/**
* 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