]> granicus.if.org Git - icinga2/blob - lib/base/convert.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / convert.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/convert.hpp"
4 #include "base/datetime.hpp"
5 #include <boost/lexical_cast.hpp>
6
7 using namespace icinga;
8
9 String Convert::ToString(const String& val)
10 {
11         return val;
12 }
13
14 String Convert::ToString(const Value& val)
15 {
16         return val;
17 }
18
19 String Convert::ToString(double val)
20 {
21         double integral;
22         double fractional = std::modf(val, &integral);
23
24         if (fractional == 0)
25                 return Convert::ToString(static_cast<long long>(val));
26
27         std::ostringstream msgbuf;
28         msgbuf << std::fixed << val;
29         return msgbuf.str();
30 }
31
32 double Convert::ToDateTimeValue(double val)
33 {
34         return val;
35 }
36
37 double Convert::ToDateTimeValue(const Value& val)
38 {
39         if (val.IsNumber())
40                 return val;
41         else if (val.IsObjectType<DateTime>())
42                 return static_cast<DateTime::Ptr>(val)->GetValue();
43         else
44                 BOOST_THROW_EXCEPTION(std::invalid_argument("Not a DateTime value."));
45 }