Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#2576) std::chrono support in Timespan #2623

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Foundation/include/Poco/Timespan.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef Foundation_Timespan_INCLUDED
#define Foundation_Timespan_INCLUDED

#include <chrono>

#include "Poco/Foundation.h"
#include "Poco/Timestamp.h"
Expand Down Expand Up @@ -47,6 +48,11 @@ class Foundation_API Timespan
Timespan(const Timespan& timespan);
/// 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();
/// Destroys the Timespan.

Expand All @@ -63,6 +69,13 @@ class Foundation_API Timespan
/// Assigns a new span. Useful for assigning
/// from a struct timeval.

template <class T, class Period>
Timespan& assign(const std::chrono::duration<T, Period> & dtime)
{
_span = std::chrono::duration_cast<std::chrono::microseconds>(dtime).count();
return *this;
}

void swap(Timespan& timespan);
/// Swaps the Timespan with another one.

Expand Down
4 changes: 4 additions & 0 deletions Foundation/testsuite/src/TimespanTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ void TimespanTest::testConversions()
assertTrue (ts.minutes() == 30);
assertTrue (ts.hours() == 12);
assertTrue (ts.days() == 1);

ts.assign(std::chrono::hours(1));
assertTrue(ts.hours() == 1);
assertTrue(ts.minutes() == 0);
}


Expand Down