Use date utilities from libyang-cpp
Unfortunately, our CI still does not have std::chrono implementation
we need in order to not require the date lib.
Also change date timezones from -00:00 to local timezone.
Depends-on: https://gerrit.cesnet.cz/c/CzechLight/dependencies/+/7234
Change-Id: If0ca8fc049a9a3b41853144f5c3b81532b0e349d
diff --git a/src/ietf-hardware/IETFHardware.cpp b/src/ietf-hardware/IETFHardware.cpp
index 103b45d..e6d37a0 100644
--- a/src/ietf-hardware/IETFHardware.cpp
+++ b/src/ietf-hardware/IETFHardware.cpp
@@ -6,10 +6,10 @@
*/
#include <chrono>
+#include <libyang-cpp/Time.hpp>
#include <utility>
#include "IETFHardware.h"
#include "utils/log.h"
-#include "utils/time.h"
using namespace std::literals;
@@ -130,7 +130,7 @@
}
}
- pollData.data[ietfHardwareStatePrefix + "/last-change"] = velia::utils::yangTimeFormat(std::chrono::system_clock::now());
+ pollData.data[ietfHardwareStatePrefix + "/last-change"] = libyang::yangTimeFormat(std::chrono::system_clock::now(), libyang::TimezoneInterpretation::Unspecified);
return {pollData.data, alarms, activeSensors, pollData.sideLoadedAlarms};
}
@@ -320,7 +320,7 @@
std::chrono::year(std::stoi(mfgDate.substr(3, 4))),
std::chrono::month(std::stoi(mfgDate.substr(0, 2))),
std::chrono::day(1));
- mfgDate = velia::utils::yangTimeFormat(std::chrono::sys_days{calendarDate});
+ mfgDate = libyang::yangTimeFormat(std::chrono::sys_days{calendarDate}, libyang::TimezoneInterpretation::Unspecified);
addComponent(m_staticData,
m_componentName,
diff --git a/src/system/Authentication.cpp b/src/system/Authentication.cpp
index 544a5d2..460e53c 100644
--- a/src/system/Authentication.cpp
+++ b/src/system/Authentication.cpp
@@ -7,6 +7,7 @@
#include <fmt/core.h>
#include <fstream>
+#include <libyang-cpp/Time.hpp>
#include <pwd.h>
#include <shadow.h>
#include <spdlog/spdlog.h>
@@ -17,7 +18,6 @@
#include "utils/io.h"
#include "utils/libyang.h"
#include "utils/sysrepo.h"
-#include "utils/time.h"
using namespace std::string_literals;
namespace {
@@ -131,8 +131,8 @@
assert(entry);
using namespace std::chrono_literals;
- using TimeType = std::chrono::time_point<std::chrono::system_clock>;
- res.emplace(entry->sp_namp, velia::utils::yangTimeFormat(TimeType(24h * entry->sp_lstchg)));
+ using TimeType = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
+ res.emplace(entry->sp_namp, libyang::yangTimeFormat(TimeType(24h * entry->sp_lstchg), libyang::TimezoneInterpretation::Local));
}
return res;
diff --git a/src/system/IETFSystem.cpp b/src/system/IETFSystem.cpp
index 5439d09..3175b50 100644
--- a/src/system/IETFSystem.cpp
+++ b/src/system/IETFSystem.cpp
@@ -8,6 +8,7 @@
#include <arpa/inet.h>
#include <boost/algorithm/string/predicate.hpp>
#include <fstream>
+#include <libyang-cpp/Time.hpp>
#include <optional>
#include "IETFSystem.h"
#include "system_vars.h"
@@ -15,7 +16,6 @@
#include "utils/io.h"
#include "utils/log.h"
#include "utils/sysrepo.h"
-#include "utils/time.h"
using namespace std::literals;
@@ -203,7 +203,7 @@
void IETFSystem::initClock()
{
sysrepo::OperGetCb cb = [] (auto, auto, auto, auto, auto, auto, auto& parent) {
- parent->newPath(IETF_SYSTEM_STATE_CLOCK_PATH + "/current-datetime"s, utils::yangTimeFormat(std::chrono::system_clock::now()));
+ parent->newPath(IETF_SYSTEM_STATE_CLOCK_PATH + "/current-datetime"s, libyang::yangTimeFormat(std::chrono::system_clock::now(), libyang::TimezoneInterpretation::Local));
return sysrepo::ErrorCode::Ok;
};
diff --git a/src/utils/time.cpp b/src/utils/time.cpp
deleted file mode 100644
index 4a92dcb..0000000
--- a/src/utils/time.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
- *
- * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
- *
- */
-
-#include <iomanip>
-#include <sstream>
-#include <utils/time.h>
-
-/** @short Utilitary functions for various needs */
-namespace velia::utils {
-
-/** @brief Converts a time_point to a UTC timezone textual representation required by yang:date-and-time. */
-std::string yangTimeFormat(const std::chrono::time_point<std::chrono::system_clock>& timePoint)
-{
- auto time = std::chrono::system_clock::to_time_t(timePoint);
-
- std::ostringstream oss;
- oss << std::put_time(std::gmtime(&time), "%Y-%m-%dT%H:%M:%S-00:00");
- return oss.str();
-}
-}
diff --git a/src/utils/time.h b/src/utils/time.h
deleted file mode 100644
index 686f6ff..0000000
--- a/src/utils/time.h
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
- *
- * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
- *
- */
-
-#pragma once
-
-#include <chrono>
-#include <string>
-
-namespace velia::utils {
-
-std::string yangTimeFormat(const std::chrono::time_point<std::chrono::system_clock>& timePoint);
-}