diff --git a/livesupport/modules/core/include/LiveSupport/Core/TimeConversion.h b/livesupport/modules/core/include/LiveSupport/Core/TimeConversion.h index 495495291..e5f12fe85 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: fgerlits $ - Version : $Revision: 1.8 $ + Version : $Revision: 1.9 $ 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: fgerlits $ - * @version $Revision: 1.8 $ + * @version $Revision: 1.9 $ */ class TimeConversion { @@ -141,11 +141,25 @@ class TimeConversion /** * Convert a time_duration to a format used in SMILs. + * This means number of seconds, rounded to the nearest millisecond. + * For example: "1234.567s", "0.890s", or "3.000s". * - * @param duration sleep for this duration. + * @param duration the time duration to convert. */ static Ptr::Ref - timeDurationToSmilString(Ptr::Ref duration) + timeDurationToSmilString(Ptr::Ref duration) + throw (); + + /** + * Convert a time_duration to a rounded format used on the screen. + * This means a hh:mm:ss format, rounded to the nearest second. + * For example: "01:02:03" or "00:10:00". The hours field can be + * more than two characters wide, e.g.: "8765:48:45". + * + * @param duration the time duration to convert. + */ + static Ptr::Ref + timeDurationToHhMmSsString(Ptr::Ref duration) throw (); }; diff --git a/livesupport/modules/core/src/TimeConversion.cxx b/livesupport/modules/core/src/TimeConversion.cxx index 7a36d581f..ac5cecac7 100644 --- a/livesupport/modules/core/src/TimeConversion.cxx +++ b/livesupport/modules/core/src/TimeConversion.cxx @@ -22,7 +22,7 @@ Author : $Author: fgerlits $ - Version : $Revision: 1.8 $ + Version : $Revision: 1.9 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversion.cxx,v $ ------------------------------------------------------------------------------*/ @@ -162,13 +162,48 @@ TimeConversion :: timeDurationToSmilString( throw () { std::stringstream stringStream; - stringStream << duration->total_seconds(); - int microseconds = duration->fractional_seconds(); + stringStream << std::dec + << duration->total_seconds(); + + int microseconds = duration->fractional_seconds(); stringStream << "." - << std::setw(3) << std::setfill('0') << std::dec + << std::setw(3) + << std::setfill('0') << (microseconds + 500) / 1000 << 's'; Ptr::Ref result(new std::string(stringStream.str())); return result; } + +/*------------------------------------------------------------------------------ + * Convert a time_duration to a rounded format used on the screen. + *----------------------------------------------------------------------------*/ +Ptr::Ref +TimeConversion :: timeDurationToHhMmSsString( + Ptr::Ref duration) + throw () +{ + std::stringstream stringStream; + stringStream << std::dec + << std::setw(2) + << std::setfill('0') + << duration->hours() + << ":" + << std::setw(2) + << std::setfill('0') + << duration->minutes(); + + int seconds = duration->seconds(); + if (duration->fractional_seconds() >= 500000) { + ++seconds; + } + stringStream << ":" + << std::setw(2) + << std::setfill('0') + << seconds; + + Ptr::Ref result(new std::string(stringStream.str())); + return result; +} + diff --git a/livesupport/modules/core/src/TimeConversionTest.cxx b/livesupport/modules/core/src/TimeConversionTest.cxx index 7fdab1bff..a97803f52 100644 --- a/livesupport/modules/core/src/TimeConversionTest.cxx +++ b/livesupport/modules/core/src/TimeConversionTest.cxx @@ -22,7 +22,7 @@ Author : $Author: fgerlits $ - Version : $Revision: 1.7 $ + Version : $Revision: 1.8 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversionTest.cxx,v $ ------------------------------------------------------------------------------*/ @@ -215,18 +215,27 @@ TimeConversionTest :: sleepTest(void) /*------------------------------------------------------------------------------ - * Test the timeDurationToSmilString() function + * Test the timeDurationToSmilString() and timeDurationToHhMmSs() functions *----------------------------------------------------------------------------*/ void TimeConversionTest :: durationToStringTest(void) throw (CPPUNIT_NS::Exception) { Ptr::Ref duration(new time_duration(duration_from_string( - "01:02:03.003700" ))); + "01:02:03.503700" ))); - Ptr::Ref durationString + Ptr::Ref smilString = TimeConversion::timeDurationToSmilString( duration); - CPPUNIT_ASSERT_EQUAL(std::string("3723.004s"), *durationString); + CPPUNIT_ASSERT_EQUAL(std::string("3723.504s"), *smilString); + + Ptr::Ref hhMmSsString + = TimeConversion::timeDurationToHhMmSsString( + duration); + CPPUNIT_ASSERT_EQUAL(std::string("01:02:04"), *hhMmSsString); + + duration.reset(new time_duration(duration_from_string("111:22:33"))); + hhMmSsString = TimeConversion::timeDurationToHhMmSsString(duration); + CPPUNIT_ASSERT_EQUAL(std::string("111:22:33"), *hhMmSsString); } diff --git a/livesupport/modules/core/src/TimeConversionTest.h b/livesupport/modules/core/src/TimeConversionTest.h index 0e207d2d0..3d6a7f922 100644 --- a/livesupport/modules/core/src/TimeConversionTest.h +++ b/livesupport/modules/core/src/TimeConversionTest.h @@ -22,7 +22,7 @@ Author : $Author: fgerlits $ - Version : $Revision: 1.6 $ + Version : $Revision: 1.7 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversionTest.h,v $ ------------------------------------------------------------------------------*/ @@ -58,7 +58,7 @@ namespace Core { * Unit test for the TimeConversion class. * * @author $Author: fgerlits $ - * @version $Revision: 1.6 $ + * @version $Revision: 1.7 $ * @see TimeConversion */ class TimeConversionTest : public CPPUNIT_NS::TestFixture @@ -115,7 +115,8 @@ class TimeConversionTest : public CPPUNIT_NS::TestFixture sleepTest(void) throw (CPPUNIT_NS::Exception); /** - * Test the timeDurationToSmilString() function. + * Test the timeDurationToSmilString() and timeDurationToHhMmSs() + * functions. * * @exception CPPUNIT_NS::Exception on test failures. */