]> granicus.if.org Git - icinga2/blob - lib/base/datetime.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / datetime.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/datetime.hpp"
4 #include "base/datetime-ti.cpp"
5 #include "base/utility.hpp"
6 #include "base/primitivetype.hpp"
7
8 using namespace icinga;
9
10 REGISTER_TYPE_WITH_PROTOTYPE(DateTime, DateTime::GetPrototype());
11
12 DateTime::DateTime(double value)
13         : m_Value(value)
14 { }
15
16 DateTime::DateTime(const std::vector<Value>& args)
17 {
18         if (args.empty())
19                 m_Value = Utility::GetTime();
20         else if (args.size() == 3 || args.size() == 6) {
21                 struct tm tms;
22                 tms.tm_year = args[0] - 1900;
23                 tms.tm_mon = args[1] - 1;
24                 tms.tm_mday = args[2];
25
26                 if (args.size() == 6) {
27                         tms.tm_hour = args[3];
28                         tms.tm_min = args[4];
29                         tms.tm_sec = args[5];
30                 } else {
31                         tms.tm_hour = 0;
32                         tms.tm_min = 0;
33                         tms.tm_sec = 0;
34                 }
35
36                 tms.tm_isdst = -1;
37
38                 m_Value = mktime(&tms);
39         } else if (args.size() == 1)
40                 m_Value = args[0];
41         else
42                 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid number of arguments for the DateTime constructor."));
43 }
44
45 double DateTime::GetValue() const
46 {
47         return m_Value;
48 }
49
50 String DateTime::Format(const String& format) const
51 {
52         return Utility::FormatDateTime(format.CStr(), m_Value);
53 }
54
55 String DateTime::ToString() const
56 {
57         return Format("%Y-%m-%d %H:%M:%S %z");
58 }