summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Schwerin <schwerin@mongodb.com>2015-05-14 17:22:29 -0400
committerAndy Schwerin <schwerin@mongodb.com>2015-05-15 11:10:57 -0400
commit2518e60c495a700cbb44237425ecf064db970dbd (patch)
tree60806c821a19a1f97d7e766771cc6297440205df
parent5b1bb5cf93b3e15393ea9118602d3d72e9703fff (diff)
downloadmongo-2518e60c495a700cbb44237425ecf064db970dbd.tar.gz
SERVER-13874 Support converting Date_t to system_clock::time_point and back.
-rw-r--r--src/mongo/util/time_support.cpp7
-rw-r--r--src/mongo/util/time_support.h22
-rw-r--r--src/mongo/util/time_support_test.cpp9
3 files changed, 35 insertions, 3 deletions
diff --git a/src/mongo/util/time_support.cpp b/src/mongo/util/time_support.cpp
index 4c588d39071..57eefa80775 100644
--- a/src/mongo/util/time_support.cpp
+++ b/src/mongo/util/time_support.cpp
@@ -126,6 +126,13 @@ namespace {
return fromMillisSinceEpoch(curTimeMillis64());
}
+ Date_t::Date_t(stdx::chrono::system_clock::time_point tp) :
+ millis(durationCount<Milliseconds>(tp - stdx::chrono::system_clock::from_time_t(0))) {}
+
+ stdx::chrono::system_clock::time_point Date_t::toSystemTimePoint() const {
+ return stdx::chrono::system_clock::from_time_t(0) + toDurationSinceEpoch();
+ }
+
bool Date_t::isFormattable() const {
if (millis < 0) {
return false;
diff --git a/src/mongo/util/time_support.h b/src/mongo/util/time_support.h
index 596812e4b65..3d5128f43e9 100644
--- a/src/mongo/util/time_support.h
+++ b/src/mongo/util/time_support.h
@@ -119,7 +119,12 @@ namespace mongo {
/**
* Constructs a Date_t representing the epoch.
*/
- Date_t(): millis(0) {}
+ Date_t() = default;
+
+ /**
+ * Constructs a Date_t from a system clock time point.
+ */
+ explicit Date_t(stdx::chrono::system_clock::time_point tp);
/**
* Returns a string representation of the date.
@@ -165,6 +170,11 @@ namespace mongo {
*/
long long toMillisSinceEpoch() const { return static_cast<long long>(millis); }
+ /*
+ * Returns a system clock time_point representing the same point in time as this Date_t.
+ */
+ stdx::chrono::system_clock::time_point toSystemTimePoint() const;
+
/**
* Returns true if this Date_t is in the range of Date_ts that can be formatted as calendar
* dates. This property is guaranteed to be true for all dates from the epoch,
@@ -173,6 +183,12 @@ namespace mongo {
*/
bool isFormattable() const;
+ /**
+ * Implicit conversion operator to system clock time point. Enables use of Date_t with
+ * condition_variable::wait_until.
+ */
+ operator stdx::chrono::system_clock::time_point () const { return toSystemTimePoint(); }
+
template <typename Duration>
Date_t& operator+=(Duration d) {
millis += duration_cast<Milliseconds>(d).count();
@@ -223,9 +239,9 @@ namespace mongo {
}
private:
- Date_t(long long m): millis(m) {}
+ explicit Date_t(long long m): millis(m) {}
- long long millis;
+ long long millis = 0;
};
// uses ISO 8601 dates without trailing Z
diff --git a/src/mongo/util/time_support_test.cpp b/src/mongo/util/time_support_test.cpp
index 8c8f1a74f37..22a97acaf13 100644
--- a/src/mongo/util/time_support_test.cpp
+++ b/src/mongo/util/time_support_test.cpp
@@ -825,5 +825,14 @@ namespace {
ASSERT_EQUALS("52ms52\xce\xbcs52s", os.str());
}
+ TEST(SystemTime, ConvertDateToSystemTime) {
+ const std::string isoTimeString = "2015-05-14T17:28:33.123Z";
+ const Date_t aDate = unittest::assertGet(dateFromISOString(isoTimeString));
+ const auto aTimePoint = aDate.toSystemTimePoint();
+ ASSERT_EQUALS(aDate.toDurationSinceEpoch(),
+ aTimePoint - stdx::chrono::system_clock::from_time_t(0));
+ ASSERT_EQUALS(aDate, Date_t(aTimePoint));
+ }
+
} // namespace
} // namespace mongo