added new timeDurationToShortString() function

This commit is contained in:
fgerlits 2005-07-27 11:12:43 +00:00
parent 7adf52fe92
commit 3548340451
4 changed files with 94 additions and 18 deletions

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $
Version : $Revision: 1.11 $
Version : $Revision: 1.12 $
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.11 $
* @version $Revision: 1.12 $
*/
class TimeConversion
{
@ -194,7 +194,9 @@ class TimeConversion
/**
* 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".
*
@ -205,6 +207,22 @@ class TimeConversion
timeDurationToHhMmSsString(Ptr<time_duration>::Ref duration)
throw ();
/**
* Convert a time_duration to a format used for fade info.
*
* This means a hh:mm:ss.ffffff format, with hours, minutes and
* fractions left off when zero.
*
* For example: 01:02:03.004, 1:02 (meaning 1m 2s), 3 (meaning 3s),
* 0.002 (meaning 2ms). Zero is represented as 0.
*
* @param duration the time duration to convert.
* @return the time duration in string format
*/
static Ptr<std::string>::Ref
timeDurationToShortString(Ptr<time_duration>::Ref duration)
throw ();
/**
* Parse a string to a time_duration.
* Similar to boost::posix_time::duration_from_string(), only

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $
Version : $Revision: 1.11 $
Version : $Revision: 1.12 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversion.cxx,v $
------------------------------------------------------------------------------*/
@ -214,6 +214,51 @@ TimeConversion :: timeDurationToHhMmSsString(
}
/*------------------------------------------------------------------------------
* Convert a time_duration to a format used for fade info.
*----------------------------------------------------------------------------*/
Ptr<std::string>::Ref
TimeConversion :: timeDurationToShortString(
Ptr<time_duration>::Ref duration)
throw ()
{
std::stringstream stringStream;
if (duration->hours()) {
stringStream << duration->hours()
<< ":"
<< std::setw(2)
<< std::setfill('0')
<< duration->minutes()
<< ":"
<< std::setw(2)
<< std::setfill('0');
} else if (duration->minutes()) {
stringStream << duration->minutes()
<< ":"
<< std::setw(2)
<< std::setfill('0');
}
stringStream << duration->seconds();
std::stringstream fractionsStream;
fractionsStream << std::setw(getNumberOfDigitsPrecision())
<< std::setfill('0')
<< duration->fractional_seconds();
std::string fractionsString(fractionsStream.str());
unsigned int lastNonZero = fractionsString.find_last_not_of('0');
if (lastNonZero != std::string::npos) {
stringStream << "."
<< fractionsString.substr(0, lastNonZero+1);
}
Ptr<std::string>::Ref result(new std::string(stringStream.str()));
return result;
}
/*------------------------------------------------------------------------------
* Parse a string to a time_duration.
*----------------------------------------------------------------------------*/

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $
Version : $Revision: 1.9 $
Version : $Revision: 1.10 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversionTest.cxx,v $
------------------------------------------------------------------------------*/
@ -215,28 +215,41 @@ TimeConversionTest :: sleepTest(void)
/*------------------------------------------------------------------------------
* Test the timeDurationToSmilString() and timeDurationToHhMmSs() functions
* Test the time_duration to string conversions
*----------------------------------------------------------------------------*/
void
TimeConversionTest :: durationToStringTest(void)
throw (CPPUNIT_NS::Exception)
{
Ptr<time_duration>::Ref duration(new time_duration(duration_from_string(
"01:02:03.503700" )));
Ptr<time_duration>::Ref duration;
Ptr<std::string>::Ref smilString;
Ptr<std::string>::Ref hhMmSsString;
Ptr<std::string>::Ref shortString;
Ptr<std::string>::Ref smilString
= TimeConversion::timeDurationToSmilString(
duration);
duration.reset(new time_duration(duration_from_string("01:02:03.503700")));
smilString = TimeConversion::timeDurationToSmilString(duration);
CPPUNIT_ASSERT_EQUAL(std::string("3723.504s"), *smilString);
Ptr<std::string>::Ref hhMmSsString
= TimeConversion::timeDurationToHhMmSsString(
duration);
hhMmSsString = TimeConversion::timeDurationToHhMmSsString(duration);
CPPUNIT_ASSERT_EQUAL(std::string("01:02:04"), *hhMmSsString);
shortString = TimeConversion::timeDurationToShortString(duration);
CPPUNIT_ASSERT_EQUAL(std::string("1:02:03.5037"), *shortString);
duration.reset(new time_duration(duration_from_string("111:22:33")));
hhMmSsString = TimeConversion::timeDurationToHhMmSsString(duration);
CPPUNIT_ASSERT_EQUAL(std::string("111:22:33"), *hhMmSsString);
shortString = TimeConversion::timeDurationToShortString(duration);
CPPUNIT_ASSERT_EQUAL(std::string("111:22:33"), *shortString);
duration.reset(new time_duration(duration_from_string("00:01:02.500000")));
shortString = TimeConversion::timeDurationToShortString(duration);
CPPUNIT_ASSERT_EQUAL(std::string("1:02.5"), *shortString);
duration.reset(new time_duration(duration_from_string("00:00:02.001000")));
shortString = TimeConversion::timeDurationToShortString(duration);
CPPUNIT_ASSERT_EQUAL(std::string("2.001"), *shortString);
}

View file

@ -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/TimeConversionTest.h,v $
------------------------------------------------------------------------------*/
@ -58,7 +58,7 @@ namespace Core {
* Unit test for the TimeConversion class.
*
* @author $Author: fgerlits $
* @version $Revision: 1.8 $
* @version $Revision: 1.9 $
* @see TimeConversion
*/
class TimeConversionTest : public CPPUNIT_NS::TestFixture
@ -116,7 +116,7 @@ class TimeConversionTest : public CPPUNIT_NS::TestFixture
sleepTest(void) throw (CPPUNIT_NS::Exception);
/**
* Test the timeDurationToSmilString() and timeDurationToHhMmSs()
* Test the time_duration to string conversions
* functions.
*
* @exception CPPUNIT_NS::Exception on test failures.