From 0a967cebf1526c0beba12deef8756e12eaeb9472 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 5 May 2019 23:59:31 +0100 Subject: Add generic duration based time_pointer to time_t converter --- project2/common/chronoHelpers.h | 12 ++++++++++++ project2/files/optionsSource.cpp | 3 ++- project2/xml/xmlScriptParser.cpp | 7 ++++--- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 project2/common/chronoHelpers.h diff --git a/project2/common/chronoHelpers.h b/project2/common/chronoHelpers.h new file mode 100644 index 0000000..7bf1650 --- /dev/null +++ b/project2/common/chronoHelpers.h @@ -0,0 +1,12 @@ +#ifndef COMMON_CHRONOHELPERS_H +#define COMMON_CHRONOHELPERS_H + +template +std::time_t +to_time_t(const time_point & t) +{ + return std::chrono::duration_cast(t.time_since_epoch()).count(); +} + +#endif + diff --git a/project2/files/optionsSource.cpp b/project2/files/optionsSource.cpp index 9172834..7d81e50 100644 --- a/project2/files/optionsSource.cpp +++ b/project2/files/optionsSource.cpp @@ -6,6 +6,7 @@ #include "configFlexLexer.h" #include #include +#include FileOptions::FileOptions(const std::filesystem::path & f) : file(std::filesystem::absolute(f)) @@ -29,7 +30,7 @@ boost::posix_time::ptime FileOptions::modifiedTime() const { return boost::posix_time::from_time_t( - std::chrono::system_clock::to_time_t( + to_time_t( std::filesystem::exists(file) ? std::max( std::filesystem::last_write_time(file), diff --git a/project2/xml/xmlScriptParser.cpp b/project2/xml/xmlScriptParser.cpp index 7e4565b..354bb55 100644 --- a/project2/xml/xmlScriptParser.cpp +++ b/project2/xml/xmlScriptParser.cpp @@ -4,6 +4,7 @@ #include "commonObjects.h" #include "variables/literal.h" #include +#include static const std::string XIncludeNS("http://www.w3.org/2001/XInclude"); static const std::string XIncludeInclude("include"); @@ -17,7 +18,7 @@ XmlScriptParser::XmlScriptParser(const std::filesystem::path & file) throw NotReadable(file.string()); } doIncludes(get_document()->get_root_node(), file); - files.insert(Files::value_type(file, std::chrono::system_clock::to_time_t( + files.insert(Files::value_type(file, to_time_t( std::filesystem::last_write_time(file)))); } @@ -38,7 +39,7 @@ XmlScriptParser::doIncludes(xmlpp::Element * e, const std::filesystem::path & f) xmlpp::DomParser(inc.string()).get_document()->get_root_node()))) { doIncludes(c, inc); } - files.insert(Files::value_type(inc, std::chrono::system_clock::to_time_t( + files.insert(Files::value_type(inc, to_time_t( std::filesystem::last_write_time(inc)))); } else { @@ -79,7 +80,7 @@ XmlScriptParser::isCurrent() const { for (const Files::value_type & f : files) { try { - if (std::chrono::system_clock::to_time_t(std::filesystem::last_write_time(f.first)) != f.second) { + if (to_time_t(std::filesystem::last_write_time(f.first)) != f.second) { _root.reset(); return false; } -- cgit v1.2.3