diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:34:54 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:34:54 +0000 |
commit | 0915b3ef56dfac3113cce55a59a5765dc94976be (patch) | |
tree | a8fea11d50b4f083e1bf0f90025ece7f0824784a /lib/base/datetime.cpp | |
parent | Initial commit. (diff) | |
download | icinga2-0915b3ef56dfac3113cce55a59a5765dc94976be.tar.xz icinga2-0915b3ef56dfac3113cce55a59a5765dc94976be.zip |
Adding upstream version 2.13.6.upstream/2.13.6upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | lib/base/datetime.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/base/datetime.cpp b/lib/base/datetime.cpp new file mode 100644 index 0000000..aa7b5e5 --- /dev/null +++ b/lib/base/datetime.cpp @@ -0,0 +1,58 @@ +/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ + +#include "base/datetime.hpp" +#include "base/datetime-ti.cpp" +#include "base/utility.hpp" +#include "base/primitivetype.hpp" + +using namespace icinga; + +REGISTER_TYPE_WITH_PROTOTYPE(DateTime, DateTime::GetPrototype()); + +DateTime::DateTime(double value) + : m_Value(value) +{ } + +DateTime::DateTime(const std::vector<Value>& args) +{ + if (args.empty()) + m_Value = Utility::GetTime(); + else if (args.size() == 3 || args.size() == 6) { + struct tm tms; + tms.tm_year = args[0] - 1900; + tms.tm_mon = args[1] - 1; + tms.tm_mday = args[2]; + + if (args.size() == 6) { + tms.tm_hour = args[3]; + tms.tm_min = args[4]; + tms.tm_sec = args[5]; + } else { + tms.tm_hour = 0; + tms.tm_min = 0; + tms.tm_sec = 0; + } + + tms.tm_isdst = -1; + + m_Value = mktime(&tms); + } else if (args.size() == 1) + m_Value = args[0]; + else + BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid number of arguments for the DateTime constructor.")); +} + +double DateTime::GetValue() const +{ + return m_Value; +} + +String DateTime::Format(const String& format) const +{ + return Utility::FormatDateTime(format.CStr(), m_Value); +} + +String DateTime::ToString() const +{ + return Format("%Y-%m-%d %H:%M:%S %z"); +} |