Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
to_string.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file ored/utilities/to_string.cpp
20 \brief
21 \ingroup utilities
22*/
23
26
27#include <ql/errors.hpp>
28
29#include <iostream>
30#include <stdio.h>
31
32#ifdef _MSC_VER
33#define snprintf _snprintf
34#endif
35
36using std::string;
37
38namespace ore {
39namespace data {
40
41using namespace QuantLib;
42
43std::string to_string(const Date& date) {
44 if (date == Date())
45 return "1901-01-01";
46
47 char buf[11];
48 int y = date.year();
49 int m = static_cast<int>(date.month());
50 int d = date.dayOfMonth();
51
52 int n = snprintf(buf, sizeof(buf), "%04d-%02d-%02d", y, m, d);
53 QL_REQUIRE(n == 10, "Failed to convert date " << date << " to_string() n:" << n);
54 return std::string(buf);
55}
56
57string to_string(bool aBool) { return aBool ? "true" : "false"; }
58
59std::string to_string(const Period& period) {
60 Integer n = period.length();
61 Integer m = 0;
62 std::ostringstream o;
63 switch (period.units()) {
64 case Days:
65 if (n>=7) {
66 m = n/7;
67 o << m << "W";
68 n = n%7;
69 }
70 if (n != 0 || m == 0) {
71 o << n << "D";
72 return o.str();
73 }
74 else {
75 return o.str();
76 }
77 case Weeks: {
78 o << n << "W";
79 return o.str();
80 }
81 case Months:
82 if (n>=12) {
83 m = n/12;
84 o << n/12 << "Y";
85 n = n%12;
86 }
87 if (n != 0 || m == 0) {
88 o << n << "M";
89 return o.str();
90 }
91 else {
92 return o.str();
93 }
94 case Years: {
95 o << n << "Y";
96 return o.str();
97 }
98 default:
99 ALOG("unknown time unit (" << Integer(period.units()) << ")");
100 o << period;
101 return o.str();
102 }
103}
104
105} // namespace data
106} // namespace ore
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
#define ALOG(text)
Logging Macro (Level = Alert)
Definition: log.hpp:544
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
Serializable Credit Default Swap.
Definition: namespaces.docs:23
string conversion utilities