added TimeCoverstion::tmToPtime()

This commit is contained in:
maroy 2004-12-13 12:33:41 +00:00
parent ce64c6ead3
commit b937849015
4 changed files with 75 additions and 6 deletions

View file

@ -22,7 +22,7 @@
Author : $Author: maroy $
Version : $Revision: 1.2 $
Version : $Revision: 1.3 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/TimeConversion.h,v $
------------------------------------------------------------------------------*/
@ -71,7 +71,7 @@ using namespace LiveSupport;
* A helper object holding static time conversion functions.
*
* @author $Author: maroy $
* @version $Revision: 1.2 $
* @version $Revision: 1.3 $
*/
class TimeConversion
{
@ -95,6 +95,16 @@ class TimeConversion
static Ptr<ptime>::Ref
timevalToPtime(const struct timeval *timeval) throw ();
/**
* Convert a struct tm to a boost::posix_time::ptime,
* with second precision.
*
* @param time the struct tm to convert.
* @return a boost::posix_time::ptime, holding the same time.
*/
static Ptr<ptime>::Ref
tmToPtime(const struct tm *time) throw ();
/**
* Return the current time, with microsecond precision.
*

View file

@ -22,7 +22,7 @@
Author : $Author: maroy $
Version : $Revision: 1.3 $
Version : $Revision: 1.4 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversion.cxx,v $
------------------------------------------------------------------------------*/
@ -75,6 +75,26 @@ TimeConversion :: timevalToPtime(const struct timeval *timeval)
}
/*------------------------------------------------------------------------------
* Convert a struct tm to a boost::ptime
*----------------------------------------------------------------------------*/
Ptr<ptime>::Ref
TimeConversion :: tmToPtime(const struct tm *time)
throw ()
{
// don't convert through the boost::posix_time::from_time_t() function
// as probably because of timezone settings it ruins the actual value
Ptr<ptime>::Ref pTime(new ptime(date(1900 + time->tm_year,
1 + time->tm_mon,
time->tm_mday),
time_duration(time->tm_hour,
time->tm_min,
time->tm_sec)));
return pTime;
}
/*------------------------------------------------------------------------------
* Return the current time.
*----------------------------------------------------------------------------*/

View file

@ -22,7 +22,7 @@
Author : $Author: maroy $
Version : $Revision: 1.2 $
Version : $Revision: 1.3 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversionTest.cxx,v $
------------------------------------------------------------------------------*/
@ -113,6 +113,36 @@ TimeConversionTest :: timevalToPtimeTest(void)
}
/*------------------------------------------------------------------------------
* Test the tmToPtime function
*----------------------------------------------------------------------------*/
void
TimeConversionTest :: tmToPtimeTest(void)
throw (CPPUNIT_NS::Exception)
{
struct tm tm;
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;
// and now convert, and see if it is correct
ptime = TimeConversion::tmToPtime(&tm);
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);
}
/*------------------------------------------------------------------------------
* Test the now function
*----------------------------------------------------------------------------*/

View file

@ -22,7 +22,7 @@
Author : $Author: maroy $
Version : $Revision: 1.2 $
Version : $Revision: 1.3 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversionTest.h,v $
------------------------------------------------------------------------------*/
@ -58,13 +58,14 @@ namespace Core {
* Unit test for the TimeConversion class.
*
* @author $Author: maroy $
* @version $Revision: 1.2 $
* @version $Revision: 1.3 $
* @see TimeConversion
*/
class TimeConversionTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(TimeConversionTest);
CPPUNIT_TEST(timevalToPtimeTest);
CPPUNIT_TEST(tmToPtimeTest);
CPPUNIT_TEST(nowTest);
CPPUNIT_TEST(sleepTest);
CPPUNIT_TEST_SUITE_END();
@ -79,6 +80,14 @@ class TimeConversionTest : public CPPUNIT_NS::TestFixture
void
timevalToPtimeTest(void) throw (CPPUNIT_NS::Exception);
/**
* Test conversion from struct tm to ptime
*
* @exception CPPUNIT_NS::Exception on test failures.
*/
void
tmToPtimeTest(void) throw (CPPUNIT_NS::Exception);
/**
* Test the now function.
*