source/time: Implement
diff --git a/.gitignore b/.gitignore
index 61aa582..5a7271a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -48,3 +48,4 @@
 /test/internal_sdref
 /test/internal_utils
 /test/source_base
+/test/source_time
diff --git a/src/Makefile.am b/src/Makefile.am
index a027d5f..d4e7906 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -24,4 +24,7 @@
 nobase_include_HEADERS += sdeventplus/source/base.hpp
 libsdeventplus_la_SOURCES += sdeventplus/source/base.cpp
 
+nobase_include_HEADERS += sdeventplus/source/time.hpp
+libsdeventplus_la_SOURCES += sdeventplus/source/time.cpp
+
 nobase_include_HEADERS += sdeventplus/test/sdevent.hpp
diff --git a/src/sdeventplus/internal/sdevent.hpp b/src/sdeventplus/internal/sdevent.hpp
index 7713cb8..9a6ec07 100644
--- a/src/sdeventplus/internal/sdevent.hpp
+++ b/src/sdeventplus/internal/sdevent.hpp
@@ -17,6 +17,12 @@
     virtual sd_event* sd_event_ref(sd_event* event) const = 0;
     virtual sd_event* sd_event_unref(sd_event* event) const = 0;
 
+    virtual int sd_event_add_time(sd_event* event, sd_event_source** source,
+                                  clockid_t clock, uint64_t usec,
+                                  uint64_t accuracy,
+                                  sd_event_time_handler_t callback,
+                                  void* userdata) const = 0;
+
     virtual int sd_event_prepare(sd_event* event) const = 0;
     virtual int sd_event_wait(sd_event* event, uint64_t usec) const = 0;
     virtual int sd_event_dispatch(sd_event* event) const = 0;
@@ -57,6 +63,14 @@
                                             int* enabled) const = 0;
     virtual int sd_event_source_set_enabled(sd_event_source* source,
                                             int enabled) const = 0;
+    virtual int sd_event_source_get_time(sd_event_source* source,
+                                         uint64_t* usec) const = 0;
+    virtual int sd_event_source_set_time(sd_event_source* source,
+                                         uint64_t usec) const = 0;
+    virtual int sd_event_source_get_time_accuracy(sd_event_source* source,
+                                                  uint64_t* usec) const = 0;
+    virtual int sd_event_source_set_time_accuracy(sd_event_source* source,
+                                                  uint64_t usec) const = 0;
 };
 
 class SdEventImpl : public SdEvent
@@ -82,6 +96,15 @@
         return ::sd_event_unref(event);
     }
 
+    int sd_event_add_time(sd_event* event, sd_event_source** source,
+                          clockid_t clock, uint64_t usec, uint64_t accuracy,
+                          sd_event_time_handler_t callback,
+                          void* userdata) const override
+    {
+        return ::sd_event_add_time(event, source, clock, usec, accuracy,
+                                   callback, userdata);
+    }
+
     int sd_event_prepare(sd_event* event) const override
     {
         return ::sd_event_prepare(event);
@@ -196,6 +219,30 @@
     {
         return ::sd_event_source_set_enabled(source, enabled);
     }
+
+    int sd_event_source_get_time(sd_event_source* source,
+                                 uint64_t* usec) const override
+    {
+        return ::sd_event_source_get_time(source, usec);
+    }
+
+    int sd_event_source_set_time(sd_event_source* source,
+                                 uint64_t usec) const override
+    {
+        return ::sd_event_source_set_time(source, usec);
+    }
+
+    int sd_event_source_get_time_accuracy(sd_event_source* source,
+                                          uint64_t* usec) const override
+    {
+        return ::sd_event_source_get_time_accuracy(source, usec);
+    }
+
+    int sd_event_source_set_time_accuracy(sd_event_source* source,
+                                          uint64_t usec) const override
+    {
+        return ::sd_event_source_set_time_accuracy(source, usec);
+    }
 };
 
 extern SdEventImpl sdevent_impl;
diff --git a/src/sdeventplus/source/time.cpp b/src/sdeventplus/source/time.cpp
new file mode 100644
index 0000000..1a24604
--- /dev/null
+++ b/src/sdeventplus/source/time.cpp
@@ -0,0 +1,106 @@
+#include <cstdio>
+#include <sdeventplus/clock.hpp>
+#include <sdeventplus/exception.hpp>
+#include <sdeventplus/internal/utils.hpp>
+#include <sdeventplus/source/time.hpp>
+#include <type_traits>
+#include <utility>
+
+namespace sdeventplus
+{
+namespace source
+{
+
+template <ClockId Id>
+Time<Id>::Time(const Event& event, TimePoint time, Accuracy accuracy,
+               Callback&& callback) :
+    Base(event, create_source(event, time, accuracy), std::false_type()),
+    callback(std::move(callback))
+{
+}
+
+template <ClockId Id>
+typename Time<Id>::TimePoint Time<Id>::get_time() const
+{
+    uint64_t usec;
+    int r = event.getSdEvent()->sd_event_source_get_time(source.get(), &usec);
+    if (r < 0)
+    {
+        throw SdEventError(-r, "sd_event_source_get_time");
+    }
+    return Time<Id>::TimePoint(SdEventDuration(usec));
+}
+
+template <ClockId Id>
+void Time<Id>::set_time(TimePoint time) const
+{
+    int r = event.getSdEvent()->sd_event_source_set_time(
+        source.get(), SdEventDuration(time.time_since_epoch()).count());
+    if (r < 0)
+    {
+        throw SdEventError(-r, "sd_event_source_set_time");
+    }
+}
+
+template <ClockId Id>
+typename Time<Id>::Accuracy Time<Id>::get_accuracy() const
+{
+    uint64_t usec;
+    int r = event.getSdEvent()->sd_event_source_get_time_accuracy(source.get(),
+                                                                  &usec);
+    if (r < 0)
+    {
+        throw SdEventError(-r, "sd_event_source_get_time_accuracy");
+    }
+    return SdEventDuration(usec);
+}
+
+template <ClockId Id>
+void Time<Id>::set_accuracy(Accuracy accuracy) const
+{
+    int r = event.getSdEvent()->sd_event_source_set_time_accuracy(
+        source.get(), SdEventDuration(accuracy).count());
+    if (r < 0)
+    {
+        throw SdEventError(-r, "sd_event_source_set_time_accuracy");
+    }
+}
+
+template <ClockId Id>
+const typename Time<Id>::Callback& Time<Id>::get_callback() const
+{
+    return callback;
+}
+
+template <ClockId Id>
+sd_event_source* Time<Id>::create_source(const Event& event, TimePoint time,
+                                         Accuracy accuracy)
+{
+    sd_event_source* source;
+    int r = event.getSdEvent()->sd_event_add_time(
+        event.get(), &source, static_cast<clockid_t>(Id),
+        SdEventDuration(time.time_since_epoch()).count(),
+        SdEventDuration(accuracy).count(), timeCallback, nullptr);
+    if (r < 0)
+    {
+        throw SdEventError(-r, "sd_event_add_time");
+    }
+    return source;
+}
+
+template <ClockId Id>
+int Time<Id>::timeCallback(sd_event_source* source, uint64_t usec,
+                           void* userdata)
+{
+    return sourceCallback<Callback, Time, &Time::get_callback>(
+        "timeCallback", source, userdata, TimePoint(SdEventDuration(usec)));
+}
+
+template class Time<ClockId::RealTime>;
+template class Time<ClockId::Monotonic>;
+template class Time<ClockId::BootTime>;
+template class Time<ClockId::RealTimeAlarm>;
+template class Time<ClockId::BootTimeAlarm>;
+
+} // namespace source
+} // namespace sdeventplus
diff --git a/src/sdeventplus/source/time.hpp b/src/sdeventplus/source/time.hpp
new file mode 100644
index 0000000..28a42fb
--- /dev/null
+++ b/src/sdeventplus/source/time.hpp
@@ -0,0 +1,43 @@
+#pragma once
+
+#include <cstdint>
+#include <functional>
+#include <sdeventplus/clock.hpp>
+#include <sdeventplus/internal/utils.hpp>
+#include <sdeventplus/source/base.hpp>
+#include <systemd/sd-event.h>
+
+namespace sdeventplus
+{
+namespace source
+{
+
+template <ClockId Id>
+class Time : public Base
+{
+  public:
+    using TimePoint = typename Clock<Id>::time_point;
+    using Accuracy = SdEventDuration;
+    using Callback = std::function<void(Time& source, TimePoint time)>;
+
+    Time(const Event& event, TimePoint time, Accuracy accuracy,
+         Callback&& callback);
+
+    TimePoint get_time() const;
+    void set_time(TimePoint time) const;
+    Accuracy get_accuracy() const;
+    void set_accuracy(Accuracy accuracy) const;
+
+  private:
+    Callback callback;
+
+    const Callback& get_callback() const;
+
+    static sd_event_source* create_source(const Event& event, TimePoint time,
+                                          Accuracy accuracy);
+    static int timeCallback(sd_event_source* source, uint64_t usec,
+                            void* userdata);
+};
+
+} // namespace source
+} // namespace sdeventplus
diff --git a/src/sdeventplus/test/sdevent.hpp b/src/sdeventplus/test/sdevent.hpp
index 9b39d50..1e90bc7 100644
--- a/src/sdeventplus/test/sdevent.hpp
+++ b/src/sdeventplus/test/sdevent.hpp
@@ -17,6 +17,10 @@
     MOCK_CONST_METHOD1(sd_event_ref, sd_event*(sd_event*));
     MOCK_CONST_METHOD1(sd_event_unref, sd_event*(sd_event*));
 
+    MOCK_CONST_METHOD7(sd_event_add_time,
+                       int(sd_event*, sd_event_source**, clockid_t, uint64_t,
+                           uint64_t, sd_event_time_handler_t, void*));
+
     MOCK_CONST_METHOD1(sd_event_prepare, int(sd_event*));
     MOCK_CONST_METHOD2(sd_event_wait, int(sd_event*, uint64_t));
     MOCK_CONST_METHOD1(sd_event_dispatch, int(sd_event*));
@@ -51,6 +55,14 @@
     MOCK_CONST_METHOD2(sd_event_source_get_enabled,
                        int(sd_event_source*, int*));
     MOCK_CONST_METHOD2(sd_event_source_set_enabled, int(sd_event_source*, int));
+    MOCK_CONST_METHOD2(sd_event_source_get_time,
+                       int(sd_event_source*, uint64_t*));
+    MOCK_CONST_METHOD2(sd_event_source_set_time,
+                       int(sd_event_source*, uint64_t));
+    MOCK_CONST_METHOD2(sd_event_source_get_time_accuracy,
+                       int(sd_event_source*, uint64_t*));
+    MOCK_CONST_METHOD2(sd_event_source_set_time_accuracy,
+                       int(sd_event_source*, uint64_t));
 };
 
 } // namespace test
diff --git a/test/Makefile.am b/test/Makefile.am
index 4b94144..8fc2beb 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -36,3 +36,8 @@
 source_base_SOURCES = source/base.cpp
 source_base_CPPFLAGS = $(gtest_cppflags)
 source_base_LDADD = $(gtest_ldadd)
+
+check_PROGRAMS += source_time
+source_time_SOURCES = source/time.cpp
+source_time_CPPFLAGS = $(gtest_cppflags)
+source_time_LDADD = $(gtest_ldadd)
diff --git a/test/source/time.cpp b/test/source/time.cpp
new file mode 100644
index 0000000..0ee612f
--- /dev/null
+++ b/test/source/time.cpp
@@ -0,0 +1,204 @@
+#include <cerrno>
+#include <chrono>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <memory>
+#include <sdeventplus/clock.hpp>
+#include <sdeventplus/exception.hpp>
+#include <sdeventplus/source/time.hpp>
+#include <sdeventplus/test/sdevent.hpp>
+#include <systemd/sd-event.h>
+#include <time.h>
+#include <utility>
+
+namespace sdeventplus
+{
+namespace source
+{
+namespace
+{
+
+using testing::DoAll;
+using testing::Return;
+using testing::SaveArg;
+using testing::SetArgPointee;
+
+using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>;
+
+class TimeTest : public testing::Test
+{
+  protected:
+    testing::StrictMock<test::SdEventMock> mock;
+    sd_event_source* const expected_source =
+        reinterpret_cast<sd_event_source*>(1234);
+    sd_event* const expected_event = reinterpret_cast<sd_event*>(2345);
+    UniqueEvent event = make_event(expected_event);
+
+    UniqueEvent make_event(sd_event* event)
+    {
+        auto deleter = [this, event](Event* e) {
+            EXPECT_CALL(this->mock, sd_event_unref(event))
+                .WillOnce(Return(nullptr));
+            delete e;
+        };
+        return UniqueEvent(new Event(event, std::false_type(), &mock), deleter);
+    }
+
+    void expect_time_destroy(sd_event* event, sd_event_source* source)
+    {
+        {
+            testing::InSequence sequence;
+            EXPECT_CALL(mock, sd_event_source_set_enabled(source, SD_EVENT_OFF))
+                .WillOnce(Return(0));
+            EXPECT_CALL(mock, sd_event_source_unref(source))
+                .WillOnce(Return(nullptr));
+        }
+        EXPECT_CALL(mock, sd_event_unref(event)).WillOnce(Return(nullptr));
+    }
+};
+
+TEST_F(TimeTest, ConstructSuccess)
+{
+    constexpr ClockId id = ClockId::RealTime;
+    const Time<id>::TimePoint expected_time(std::chrono::seconds{2});
+    const Time<id>::Accuracy expected_accuracy(std::chrono::milliseconds{50});
+    Time<id>::TimePoint saved_time;
+    Time<id>::Callback callback = [&saved_time](Time<id>&,
+                                                Time<id>::TimePoint time) {
+        saved_time = time;
+    };
+
+    EXPECT_CALL(mock, sd_event_ref(expected_event))
+        .WillOnce(Return(expected_event));
+    sd_event_time_handler_t handler;
+    EXPECT_CALL(mock,
+                sd_event_add_time(expected_event, testing::_, CLOCK_REALTIME,
+                                  2000000, 50000, testing::_, nullptr))
+        .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<5>(&handler),
+                        Return(0)));
+    void* userdata;
+    EXPECT_CALL(mock, sd_event_source_set_userdata(expected_source, testing::_))
+        .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
+    Time<id> time(*event, expected_time, expected_accuracy,
+                  std::move(callback));
+    EXPECT_EQ(expected_event, time.get_event().get());
+    EXPECT_EQ(expected_source, time.get());
+
+    EXPECT_EQ(0, handler(nullptr, 2000100, userdata));
+    EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds(2000100)),
+              saved_time);
+
+    expect_time_destroy(expected_event, expected_source);
+}
+
+TEST_F(TimeTest, ConstructError)
+{
+    constexpr ClockId id = ClockId::Monotonic;
+    const Time<id>::TimePoint expected_time(std::chrono::seconds{2});
+    const Time<id>::Accuracy expected_accuracy(std::chrono::milliseconds{50});
+    Time<id>::Callback callback = [](Time<id>&, Time<id>::TimePoint) {};
+
+    EXPECT_CALL(mock,
+                sd_event_add_time(expected_event, testing::_, CLOCK_MONOTONIC,
+                                  2000000, 50000, testing::_, nullptr))
+        .WillOnce(Return(-ENOSYS));
+    EXPECT_THROW(
+        Time<id>(*event, expected_time, expected_accuracy, std::move(callback)),
+        SdEventError);
+}
+
+class TimeMethodTest : public TimeTest
+{
+  protected:
+    static constexpr ClockId id = ClockId::BootTime;
+    std::unique_ptr<Time<id>> time;
+
+    void SetUp()
+    {
+        EXPECT_CALL(mock, sd_event_ref(expected_event))
+            .WillOnce(Return(expected_event));
+        EXPECT_CALL(mock, sd_event_add_time(expected_event, testing::_,
+                                            CLOCK_BOOTTIME, 2000000, 50000,
+                                            testing::_, nullptr))
+            .WillOnce(DoAll(SetArgPointee<1>(expected_source), Return(0)));
+        EXPECT_CALL(mock,
+                    sd_event_source_set_userdata(expected_source, testing::_))
+            .WillOnce(Return(nullptr));
+        time = std::make_unique<Time<id>>(
+            *event, Time<id>::TimePoint(std::chrono::seconds{2}),
+            std::chrono::milliseconds{50},
+            [](Time<id>&, Time<id>::TimePoint) {});
+    }
+
+    void TearDown()
+    {
+        expect_time_destroy(expected_event, expected_source);
+        time.reset();
+    }
+};
+
+TEST_F(TimeMethodTest, SetTimeSuccess)
+{
+    EXPECT_CALL(mock, sd_event_source_set_time(expected_source, 1000000))
+        .WillOnce(Return(0));
+    time->set_time(Time<id>::TimePoint(std::chrono::seconds{1}));
+}
+
+TEST_F(TimeMethodTest, SetTimeError)
+{
+    EXPECT_CALL(mock, sd_event_source_set_time(expected_source, 1000000))
+        .WillOnce(Return(-EINVAL));
+    EXPECT_THROW(time->set_time(Time<id>::TimePoint(std::chrono::seconds{1})),
+                 SdEventError);
+}
+
+TEST_F(TimeMethodTest, GetTimeSuccess)
+{
+    EXPECT_CALL(mock, sd_event_source_get_time(expected_source, testing::_))
+        .WillOnce(DoAll(SetArgPointee<1>(10), Return(0)));
+    EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds{10}),
+              time->get_time());
+}
+
+TEST_F(TimeMethodTest, GetTimeError)
+{
+    EXPECT_CALL(mock, sd_event_source_get_time(expected_source, testing::_))
+        .WillOnce(Return(-ENOSYS));
+    EXPECT_THROW(time->get_time(), SdEventError);
+}
+
+TEST_F(TimeMethodTest, SetAccuracySuccess)
+{
+    EXPECT_CALL(mock,
+                sd_event_source_set_time_accuracy(expected_source, 5000000))
+        .WillOnce(Return(0));
+    time->set_accuracy(std::chrono::seconds{5});
+}
+
+TEST_F(TimeMethodTest, SetAccuracyError)
+{
+    EXPECT_CALL(mock,
+                sd_event_source_set_time_accuracy(expected_source, 5000000))
+        .WillOnce(Return(-EINVAL));
+    EXPECT_THROW(time->set_accuracy(std::chrono::seconds{5}), SdEventError);
+}
+
+TEST_F(TimeMethodTest, GetAccuracySuccess)
+{
+    EXPECT_CALL(mock,
+                sd_event_source_get_time_accuracy(expected_source, testing::_))
+        .WillOnce(DoAll(SetArgPointee<1>(1000), Return(0)));
+    EXPECT_EQ(std::chrono::milliseconds{1}, time->get_accuracy());
+}
+
+TEST_F(TimeMethodTest, GetAccuracyError)
+{
+    EXPECT_CALL(mock,
+                sd_event_source_get_time_accuracy(expected_source, testing::_))
+        .WillOnce(Return(-ENOSYS));
+    EXPECT_THROW(time->get_accuracy(), SdEventError);
+}
+
+} // namespace
+} // namespace source
+} // namespace sdeventplus