added TimeConversion class

This commit is contained in:
maroy 2004-11-04 12:39:54 +00:00
parent 28f3dbc318
commit 5f03d18970
6 changed files with 477 additions and 7 deletions

View File

@ -20,8 +20,8 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# Author : $Author: fgerlits $
# Version : $Revision: 1.9 $
# Author : $Author: maroy $
# Version : $Revision: 1.10 $
# Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/etc/Makefile.in,v $
#
# @configure_input@
@ -88,15 +88,18 @@ CORE_LIB_OBJS = ${TMP_DIR}/UniqueId.o \
${TMP_DIR}/AudioClip.o \
${TMP_DIR}/FadeInfo.o \
${TMP_DIR}/PlaylistElement.o \
${TMP_DIR}/Playlist.o
${TMP_DIR}/Playlist.o \
${TMP_DIR}/TimeConversion.o
TEST_RUNNER_OBJS = ${TMP_DIR}/AudioClipTest.o \
${TMP_DIR}/FadeInfoTest.o \
${TMP_DIR}/PlaylistElementTest.o \
${TMP_DIR}/PlaylistTest.o \
${TMP_DIR}/TimeConversionTest.o \
${TMP_DIR}/TestRunner.o
TEST_RUNNER_LIBS = -l${CORE_LIB} -lxml++-1.0 -lcppunit -ldl
TEST_RUNNER_LIBS = -l${CORE_LIB} -lxml++-1.0 -lboost_date_time-gcc \
-lcppunit -ldl
#-------------------------------------------------------------------------------

View File

@ -21,7 +21,7 @@ dnl Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
dnl
dnl
dnl Author : $Author: maroy $
dnl Version : $Revision: 1.3 $
dnl Version : $Revision: 1.4 $
dnl Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/etc/configure.ac,v $
dnl-----------------------------------------------------------------------------
@ -35,14 +35,14 @@ dnl-----------------------------------------------------------------------------
AC_INIT(Core, 1.0, bugs@campware.org)
AC_PREREQ(2.59)
AC_COPYRIGHT([Copyright (c) 2004 Media Development Loan Fund under the GNU GPL])
AC_REVISION($Revision: 1.3 $)
AC_REVISION($Revision: 1.4 $)
AC_CONFIG_SRCDIR(../src/UniqueId.cxx)
AC_CONFIG_HEADERS(configure.h)
AC_PROG_CXX()
AC_CHECK_HEADERS(getopt.h)
AC_CHECK_HEADERS(getopt.h sys/time.h)
dnl-----------------------------------------------------------------------------

View File

@ -0,0 +1,118 @@
/*------------------------------------------------------------------------------
Copyright (c) 2004 Media Development Loan Fund
This file is part of the LiveSupport project.
http://livesupport.campware.org/
To report bugs, send an e-mail to bugs@campware.org
LiveSupport is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
LiveSupport is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LiveSupport; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author: maroy $
Version : $Revision: 1.1 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/TimeConversion.h,v $
------------------------------------------------------------------------------*/
#ifndef LiveSupport_Core_TimeConversion_h
#define LiveSupport_Core_TimeConversion_h
#ifndef __cplusplus
#error This is a C++ include file
#endif
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "configure.h"
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#error need sys/time.h
#endif
#include <boost/date_time/posix_time/posix_time.hpp>
#include "LiveSupport/Core/Ptr.h"
namespace LiveSupport {
namespace Core {
using namespace boost::posix_time;
using namespace LiveSupport;
/* ================================================================ constants */
/* =================================================================== macros */
/* =============================================================== data types */
/**
* A helper object holding static time conversion functions.
*
* @author $Author: maroy $
* @version $Revision: 1.1 $
*/
class TimeConversion
{
private:
/**
* The default constructor.
*/
TimeConversion(void) throw ()
{
}
public:
/**
* Convert a struct timeval to a boost::posix_time::ptime,
* with microsecond precision.
*
* @param timeval the struct timeval to convert.
* @return a boost::posix_time::ptime, holding the same time.
*/
static Ptr<ptime>::Ref
timevalToPtime(const struct timeval *timeval) throw ();
/**
* Return the current time, with microsecond precision.
*
* @return the current time, with microsecond precision.
*/
static Ptr<ptime>::Ref
now(void) throw ();
};
/* ================================================= external data structures */
/* ====================================================== function prototypes */
} // namespace Core
} // namespace LiveSupport
#endif // LiveSupport_Core_TimeConversion_h

View File

@ -0,0 +1,91 @@
/*------------------------------------------------------------------------------
Copyright (c) 2004 Media Development Loan Fund
This file is part of the LiveSupport project.
http://livesupport.campware.org/
To report bugs, send an e-mail to bugs@campware.org
LiveSupport is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
LiveSupport is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LiveSupport; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author: maroy $
Version : $Revision: 1.1 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversion.cxx,v $
------------------------------------------------------------------------------*/
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "configure.h"
#endif
#include "LiveSupport/Core/TimeConversion.h"
using namespace boost::posix_time;
using namespace boost::gregorian;
using namespace LiveSupport::Core;
/* =================================================== local data structures */
/* ================================================ local constants & macros */
/* =============================================== local function prototypes */
/* ============================================================= module code */
/*------------------------------------------------------------------------------
* Convert a struct timeval to a boost::ptime
*----------------------------------------------------------------------------*/
Ptr<ptime>::Ref
TimeConversion :: timevalToPtime(const struct timeval *timeval)
throw ()
{
// don't convert through the boost::posix_time::from_time_t() function
// as probably because of timezone settings it ruins the actual value
struct tm tm;
localtime_r(&timeval->tv_sec, &tm);
Ptr<ptime>::Ref time(new ptime(date(1900 + tm.tm_year,
1 + tm.tm_mon,
tm.tm_mday),
time_duration(tm.tm_hour,
tm.tm_min,
tm.tm_sec)
+ microseconds(timeval->tv_usec)));
return time;
}
/*------------------------------------------------------------------------------
* Return the current time.
*----------------------------------------------------------------------------*/
Ptr<ptime>::Ref
TimeConversion :: now(void)
throw ()
{
struct timeval timeval;
// TODO: check for -1 return value, to see if there are errors
gettimeofday(&timeval, 0);
return timevalToPtime(&timeval);
}

View File

@ -0,0 +1,142 @@
/*------------------------------------------------------------------------------
Copyright (c) 2004 Media Development Loan Fund
This file is part of the LiveSupport project.
http://livesupport.campware.org/
To report bugs, send an e-mail to bugs@campware.org
LiveSupport is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
LiveSupport is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LiveSupport; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author: maroy $
Version : $Revision: 1.1 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversionTest.cxx,v $
------------------------------------------------------------------------------*/
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "configure.h"
#endif
#include <string>
#include <iostream>
#include "LiveSupport/Core/TimeConversion.h"
#include "TimeConversionTest.h"
using namespace LiveSupport::Core;
/* =================================================== local data structures */
/* ================================================ local constants & macros */
CPPUNIT_TEST_SUITE_REGISTRATION(TimeConversionTest);
/* =============================================== local function prototypes */
/* ============================================================= module code */
/*------------------------------------------------------------------------------
* Set up the test environment
*----------------------------------------------------------------------------*/
void
TimeConversionTest :: setUp(void) throw ()
{
}
/*------------------------------------------------------------------------------
* Clean up the test environment
*----------------------------------------------------------------------------*/
void
TimeConversionTest :: tearDown(void) throw ()
{
}
/*------------------------------------------------------------------------------
* Test the timevalToPtime function
*----------------------------------------------------------------------------*/
void
TimeConversionTest :: timevalToPtimeTest(void)
throw (CPPUNIT_NS::Exception)
{
struct tm tm;
time_t time;
struct timeval timeval;
Ptr<ptime>::Ref ptime;
// first create a time_t with the time for 2004-11-04 12:58:30
tm.tm_year = 104; // number of years since 1900, 104 means 2004
tm.tm_mon = 10; // number of months since January, 10 means November
tm.tm_mday = 4;
tm.tm_hour = 12;
tm.tm_min = 58;
tm.tm_sec = 30;
tm.tm_isdst = 0;
time = mktime(&tm);
// now fill the timeval with timet, and 1234 useconds
timeval.tv_sec = time;
timeval.tv_usec = 1234;
// and now convert, and see if it is correct
ptime = TimeConversion::timevalToPtime(&timeval);
CPPUNIT_ASSERT(ptime->date().year() == 2004);
CPPUNIT_ASSERT(ptime->date().month() == 11);
CPPUNIT_ASSERT(ptime->date().day() == 4);
CPPUNIT_ASSERT(ptime->time_of_day().hours() == 12);
CPPUNIT_ASSERT(ptime->time_of_day().minutes() == 58);
CPPUNIT_ASSERT(ptime->time_of_day().seconds() == 30);
CPPUNIT_ASSERT((ptime->time_of_day().total_microseconds()
- ((uint64_t) (ptime->time_of_day().total_seconds()) * 1000000UL))
== 1234);
}
/*------------------------------------------------------------------------------
* Test the now function
*----------------------------------------------------------------------------*/
void
TimeConversionTest :: nowTest(void)
throw (CPPUNIT_NS::Exception)
{
struct tm tm;
time_t tTime;
Ptr<ptime>::Ref ptime;
tTime = time(0);
ptime = TimeConversion::now();
localtime_r(&tTime, &tm);
// the below checking is a bit phone, what if the two times actually
// spill over the second barrier (or, for that instance, the year
// barrier?)
CPPUNIT_ASSERT(ptime->date().year() == (1900 + tm.tm_year));
CPPUNIT_ASSERT(ptime->date().month() == (1 + tm.tm_mon));
CPPUNIT_ASSERT(ptime->date().day() == tm.tm_mday);
CPPUNIT_ASSERT(ptime->time_of_day().hours() == tm.tm_hour);
CPPUNIT_ASSERT(ptime->time_of_day().minutes() == tm.tm_min);
CPPUNIT_ASSERT(ptime->time_of_day().seconds() == tm.tm_sec);
}

View File

@ -0,0 +1,116 @@
/*------------------------------------------------------------------------------
Copyright (c) 2004 Media Development Loan Fund
This file is part of the LiveSupport project.
http://livesupport.campware.org/
To report bugs, send an e-mail to bugs@campware.org
LiveSupport is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
LiveSupport is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LiveSupport; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author: maroy $
Version : $Revision: 1.1 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversionTest.h,v $
------------------------------------------------------------------------------*/
#ifndef TimeConversionTest_h
#define TimeConversionTest_h
#ifndef __cplusplus
#error This is a C++ include file
#endif
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "configure.h"
#endif
#include <cppunit/extensions/HelperMacros.h>
namespace LiveSupport {
namespace Core {
/* ================================================================ constants */
/* =================================================================== macros */
/* =============================================================== data types */
/**
* Unit test for the TimeConversion class.
*
* @author $Author: maroy $
* @version $Revision: 1.1 $
* @see TimeConversion
*/
class TimeConversionTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(TimeConversionTest);
CPPUNIT_TEST(timevalToPtimeTest);
CPPUNIT_TEST(nowTest);
CPPUNIT_TEST_SUITE_END();
protected:
/**
* Test conversion from struct timeval to ptime
*
* @exception CPPUNIT_NS::Exception on test failures.
*/
void
timevalToPtimeTest(void) throw (CPPUNIT_NS::Exception);
/**
* Test the now function.
*
* @exception CPPUNIT_NS::Exception on test failures.
*/
void
nowTest(void) throw (CPPUNIT_NS::Exception);
public:
/**
* Set up the environment for the test case.
*/
void
setUp(void) throw ();
/**
* Clean up the environment after the test case.
*/
void
tearDown(void) throw ();
};
/* ================================================= external data structures */
/* ====================================================== function prototypes */
} // namespace Core
} // namespace LiveSupport
#endif // TimeConversionTest_h