feat(Timespan): Add std::chrono support #2576 #2623

This commit is contained in:
Alex Fabijanic 2022-06-22 14:25:07 +02:00
parent cafd56a947
commit 6a97657df8
2 changed files with 18 additions and 0 deletions

View File

@ -20,6 +20,7 @@
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include "Poco/Timestamp.h" #include "Poco/Timestamp.h"
#include <chrono>
namespace Poco { namespace Poco {
@ -47,6 +48,11 @@ public:
Timespan(const Timespan& timespan); Timespan(const Timespan& timespan);
/// Creates a Timespan from another one. /// Creates a Timespan from another one.
template <class T, class Period>
Timespan(const std::chrono::duration<T, Period>& dtime) :
_span(std::chrono::duration_cast<std::chrono::microseconds>(dtime).count()) {}
/// Creates a Timespan from std::chrono::duration
~Timespan(); ~Timespan();
/// Destroys the Timespan. /// Destroys the Timespan.
@ -63,6 +69,14 @@ public:
/// Assigns a new span. Useful for assigning /// Assigns a new span. Useful for assigning
/// from a struct timeval. /// from a struct timeval.
template <class T, class Period>
Timespan& assign(const std::chrono::duration<T, Period>& dtime)
/// Assigns a new span from std::chrono::duration.
{
_span = std::chrono::duration_cast<std::chrono::microseconds>(dtime).count();
return *this;
}
void swap(Timespan& timespan); void swap(Timespan& timespan);
/// Swaps the Timespan with another one. /// Swaps the Timespan with another one.

View File

@ -70,6 +70,10 @@ void TimespanTest::testConversions()
assertTrue (ts.minutes() == 30); assertTrue (ts.minutes() == 30);
assertTrue (ts.hours() == 12); assertTrue (ts.hours() == 12);
assertTrue (ts.days() == 1); assertTrue (ts.days() == 1);
ts.assign(std::chrono::minutes(62));
assertTrue(ts.hours() == 1);
assertTrue(ts.minutes() == 2);
} }