From 41e675016349be528236281f344d25708365c39e Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Mon, 6 Dec 2010 16:18:54 +0100 Subject: [PATCH] Move time conversion logic from lumitime.cpp to time.c --- src/lib/lumitime.cpp | 20 ++++++++++++-------- src/lib/lumitime.hpp | 4 ++-- src/lib/time.c | 35 ++++++++++++++++++++++++++++++----- src/lib/time.h | 21 ++++++++++++++++++++- 4 files changed, 64 insertions(+), 16 deletions(-) diff --git a/src/lib/lumitime.cpp b/src/lib/lumitime.cpp index fb798d476..bc9812aed 100644 --- a/src/lib/lumitime.cpp +++ b/src/lib/lumitime.cpp @@ -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 diff --git a/src/lib/lumitime.hpp b/src/lib/lumitime.hpp index 9f13d48f8..4f2ab8834 100644 --- a/src/lib/lumitime.hpp +++ b/src/lib/lumitime.hpp @@ -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_; } diff --git a/src/lib/time.c b/src/lib/time.c index 9fcc15b4f..c5f497f9d 100644 --- a/src/lib/time.c +++ b/src/lib/time.c @@ -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; +} diff --git a/src/lib/time.h b/src/lib/time.h index 5a6034a20..688eb91ad 100644 --- a/src/lib/time.h +++ b/src/lib/time.h @@ -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