From 5f03d18970bca9e66cf38784427d586157f68005 Mon Sep 17 00:00:00 2001 From: maroy Date: Thu, 4 Nov 2004 12:39:54 +0000 Subject: [PATCH] added TimeConversion class --- livesupport/modules/core/etc/Makefile.in | 11 +- livesupport/modules/core/etc/configure.ac | 6 +- .../include/LiveSupport/Core/TimeConversion.h | 118 +++++++++++++++ .../modules/core/src/TimeConversion.cxx | 91 +++++++++++ .../modules/core/src/TimeConversionTest.cxx | 142 ++++++++++++++++++ .../modules/core/src/TimeConversionTest.h | 116 ++++++++++++++ 6 files changed, 477 insertions(+), 7 deletions(-) create mode 100644 livesupport/modules/core/include/LiveSupport/Core/TimeConversion.h create mode 100644 livesupport/modules/core/src/TimeConversion.cxx create mode 100644 livesupport/modules/core/src/TimeConversionTest.cxx create mode 100644 livesupport/modules/core/src/TimeConversionTest.h diff --git a/livesupport/modules/core/etc/Makefile.in b/livesupport/modules/core/etc/Makefile.in index 0367b5c5e..43ee1c255 100644 --- a/livesupport/modules/core/etc/Makefile.in +++ b/livesupport/modules/core/etc/Makefile.in @@ -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 #------------------------------------------------------------------------------- diff --git a/livesupport/modules/core/etc/configure.ac b/livesupport/modules/core/etc/configure.ac index 22bf45115..a07ed298e 100644 --- a/livesupport/modules/core/etc/configure.ac +++ b/livesupport/modules/core/etc/configure.ac @@ -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----------------------------------------------------------------------------- diff --git a/livesupport/modules/core/include/LiveSupport/Core/TimeConversion.h b/livesupport/modules/core/include/LiveSupport/Core/TimeConversion.h new file mode 100644 index 000000000..b146d6a59 --- /dev/null +++ b/livesupport/modules/core/include/LiveSupport/Core/TimeConversion.h @@ -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 +#else +#error need sys/time.h +#endif + +#include + +#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::Ref + timevalToPtime(const struct timeval *timeval) throw (); + + /** + * Return the current time, with microsecond precision. + * + * @return the current time, with microsecond precision. + */ + static Ptr::Ref + now(void) throw (); +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + +} // namespace Core +} // namespace LiveSupport + +#endif // LiveSupport_Core_TimeConversion_h + diff --git a/livesupport/modules/core/src/TimeConversion.cxx b/livesupport/modules/core/src/TimeConversion.cxx new file mode 100644 index 000000000..dfe81cf27 --- /dev/null +++ b/livesupport/modules/core/src/TimeConversion.cxx @@ -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::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::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::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); +} + diff --git a/livesupport/modules/core/src/TimeConversionTest.cxx b/livesupport/modules/core/src/TimeConversionTest.cxx new file mode 100644 index 000000000..555e949e6 --- /dev/null +++ b/livesupport/modules/core/src/TimeConversionTest.cxx @@ -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 +#include + +#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::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::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); +} + diff --git a/livesupport/modules/core/src/TimeConversionTest.h b/livesupport/modules/core/src/TimeConversionTest.h new file mode 100644 index 000000000..aa193c1b5 --- /dev/null +++ b/livesupport/modules/core/src/TimeConversionTest.h @@ -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 + + +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 +