diff --git a/lib/base/i2-base.h b/lib/base/i2-base.h index a0c33dcbe..56a75e156 100644 --- a/lib/base/i2-base.h +++ b/lib/base/i2-base.h @@ -143,6 +143,7 @@ using std::type_info; #include #include #include +#include using boost::shared_ptr; using boost::weak_ptr; diff --git a/lib/base/streamlogger.cpp b/lib/base/streamlogger.cpp index 74afa94d7..27cf7744f 100644 --- a/lib/base/streamlogger.cpp +++ b/lib/base/streamlogger.cpp @@ -76,24 +76,7 @@ void StreamLogger::OpenFile(const String& filename) */ void StreamLogger::ProcessLogEntry(ostream& stream, bool tty, const LogEntry& entry) { - char timestamp[100]; - - time_t ts = entry.Timestamp; - tm tmnow; - -#ifdef _WIN32 - tm *temp = localtime(&ts); - - if (temp == NULL) - BOOST_THROW_EXCEPTION(PosixException("localtime() failed", errno)); - - tmnow = *temp; -#else /* _WIN32 */ - if (localtime_r(&ts, &tmnow) == NULL) - BOOST_THROW_EXCEPTION(PosixException("localtime_r() failed.", errno)); -#endif /* _WIN32 */ - - strftime(timestamp, sizeof(timestamp), "%Y/%m/%d %H:%M:%S %z", &tmnow); + String timestamp = Utility::FormatDateTime("%Y/%m/%d %H:%M:%S %z", entry.Timestamp); boost::mutex::scoped_lock lock(m_Mutex); diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index f98ce187c..ad7726a5b 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -553,3 +553,27 @@ void Utility::QueueAsyncCallback(const boost::function& callback) { Application::GetEQ().Post(callback); } + +String Utility::FormatDateTime(const char *format, double ts) +{ + char timestamp[128]; + time_t tempts = (time_t)ts; /* We don't handle sub-second timestamp here just yet. */ + tm tmthen; + +#ifdef _MSC_VER + tm *temp = localtime(&tempts); + + if (temp == NULL) + BOOST_THROW_EXCEPTION(PosixException("localtime() failed", errno)); + + tmthen = *temp; +#else /* _MSC_VER */ + if (localtime_r(&tempts, &tmthen) == NULL) + BOOST_THROW_EXCEPTION(PosixException("localtime_r() failed.", errno)); +#endif /* _MSC_VER */ + + strftime(timestamp, sizeof(timestamp), format, &tmthen); + + return timestamp; +} + diff --git a/lib/base/utility.h b/lib/base/utility.h index 3b99f5446..f90176592 100644 --- a/lib/base/utility.h +++ b/lib/base/utility.h @@ -60,6 +60,8 @@ public: static void QueueAsyncCallback(const boost::function& callback); + static String FormatDateTime(const char *format, double ts); + static #ifdef _WIN32 HMODULE diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp index e1b92a663..66b6e0952 100644 --- a/lib/icinga/icingaapplication.cpp +++ b/lib/icinga/icingaapplication.cpp @@ -157,7 +157,13 @@ Dictionary::Ptr IcingaApplication::CalculateDynamicMacros(const IcingaApplicatio { Dictionary::Ptr macros = boost::make_shared(); - macros->Set("TIMET", (long)Utility::GetTime()); + double now = Utility::GetTime(); + + macros->Set("TIMET", (long)now); + macros->Set("LONGDATETIME", Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", now)); + macros->Set("SHORTDATETIME", Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", now)); + macros->Set("DATE", Utility::FormatDateTime("%Y-%m-%d", now)); + macros->Set("TIME", Utility::FormatDateTime("%H:%M:%S %z", now)); return macros; }