diff options
Diffstat (limited to 'project2/common/functions/dates.cpp')
-rw-r--r-- | project2/common/functions/dates.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/project2/common/functions/dates.cpp b/project2/common/functions/dates.cpp new file mode 100644 index 0000000..da169cf --- /dev/null +++ b/project2/common/functions/dates.cpp @@ -0,0 +1,95 @@ +#include <pch.hpp> +#include "../variables.h" +#include "../xmlObjectLoader.h" +#include "../xmlStorage.h" +#include "../logger.h" +#include <locale> +#include <iostream> +#include <boost/date_time.hpp> + +class ParseError { }; +/// Variable implementation to access platform configuration values +class ParseDate : public VariableImplDyn { + public: + ParseDate(const xmlpp::Element * e) : + VariableImplDyn(e), + string(e, "string"), + format(e, "format") + { + } + VariableType value() const + { + const char * s = string(); + const char * f = format(); + boost::posix_time::ptime t; + struct tm tm; + memset(&tm, 0, sizeof(struct tm)); + mktime(&tm); + const char * e = strptime(s, f, &tm); + if (!e) { + Logger()->messagef(LOG_ERR, "%s: check failed (parse) for '%s' against '%s' (remaining chars='%s')", + __PRETTY_FUNCTION__, s, f, e); + throw ParseError(); + } + return boost::posix_time::ptime(boost::posix_time::ptime_from_tm(tm)); + } + private: + Variable string; + Variable format; +}; +DECLARE_COMPONENT_LOADER("parsedate", ParseDate, VariableLoader); + +class FormatDate : public VariableImplDyn { + public: + FormatDate(const xmlpp::Element * e) : + VariableImplDyn(e), + date(e, "date"), + format(e, "format") + { + } + VariableType value() const + { + std::stringstream ss; + boost::date_time::time_facet<boost::posix_time::ptime, char> * ft = new boost::date_time::time_facet<boost::posix_time::ptime, char>(); + ss.imbue(std::locale(ss.getloc(), ft)); + ft->format(format()); + ss << boost::get<boost::posix_time::ptime>(date()); + return ss.str(); + } + private: + Variable date; + Variable format; +}; +DECLARE_COMPONENT_LOADER("formatdate", FormatDate, VariableLoader); + +class AdjustDate : public VariableImplDyn { + public: + AdjustDate(const xmlpp::Element * e) : + VariableImplDyn(e), + date(e, "date"), + offset(e, "offset") + { + } + VariableType value() const + { + return boost::get<boost::posix_time::ptime>(date()) + boost::posix_time::duration_from_string(offset()); + } + private: + Variable date; + Variable offset; +}; +DECLARE_COMPONENT_LOADER("adjustdate", AdjustDate, VariableLoader); + +class CurrentDate : public VariableImplDyn { + public: + CurrentDate(const xmlpp::Element * e) : + VariableImplDyn(e) + { + } + VariableType value() const + { + return boost::posix_time::microsec_clock::universal_time(); + } +}; +DECLARE_COMPONENT_LOADER("currentdate", CurrentDate, VariableLoader); + |