diff --git a/livesupport/modules/core/include/LiveSupport/Core/TimeConversion.h b/livesupport/modules/core/include/LiveSupport/Core/TimeConversion.h index 588dbee53..138a8b2f7 100644 --- a/livesupport/modules/core/include/LiveSupport/Core/TimeConversion.h +++ b/livesupport/modules/core/include/LiveSupport/Core/TimeConversion.h @@ -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::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::Ref + tmToPtime(const struct tm *time) throw (); + /** * Return the current time, with microsecond precision. * diff --git a/livesupport/modules/core/src/TimeConversion.cxx b/livesupport/modules/core/src/TimeConversion.cxx index e7e40f6bd..9730853fe 100644 --- a/livesupport/modules/core/src/TimeConversion.cxx +++ b/livesupport/modules/core/src/TimeConversion.cxx @@ -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::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::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. *----------------------------------------------------------------------------*/ diff --git a/livesupport/modules/core/src/TimeConversionTest.cxx b/livesupport/modules/core/src/TimeConversionTest.cxx index 4b18ca1c1..8099b9921 100644 --- a/livesupport/modules/core/src/TimeConversionTest.cxx +++ b/livesupport/modules/core/src/TimeConversionTest.cxx @@ -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::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 *----------------------------------------------------------------------------*/ diff --git a/livesupport/modules/core/src/TimeConversionTest.h b/livesupport/modules/core/src/TimeConversionTest.h index 3cbb617ea..aa3fa7a6d 100644 --- a/livesupport/modules/core/src/TimeConversionTest.h +++ b/livesupport/modules/core/src/TimeConversionTest.h @@ -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. *