Re: Quantlib Date to and from xml file ( or to and from std::string)
Posted by
P Nelnik on
URL: http://quantlib.414.s1.nabble.com/Quantlib-Date-to-and-from-xml-file-or-to-and-from-std-string-tp8232p8233.html
Answering one of the questions below myself:
To convert a (quantlib) Date to a string, I've written this function, which seems to do the trick:
std::string dateToString(const Date d, const std::string format)
{ std::stringstream stream;
if( format == "d-mmm-yyyy") // for example: 7-May-2015
{ stream << d.dayOfMonth() << "-" << d.month() << "-" << d.year();
} else // could extend this function to deal with date formats other than d-mmm-yyyy
{ QL_FAIL("Unsupported format: " << format << ", could try d-mmm-yyyy.");
} return stream.str();
}I think it would make sense to include it as a method in the Date class.
If you have some suggestions as to how the function can be improved, I'm interested in hearing from you.
The follow-up question has anyone written code to do the reverse, i.e. convert a string to a Date?
Thanks
Philip
On Wed, May 12, 2010 at 6:38 PM, P Nelnik
<[hidden email]> wrote:
Greetings
I've been trying to read and write (QuantLib) Dates to and from xml files.
Has this been done before? (I'm guessing it has.)
One way to do it is to use the boost property_tree.
That's able to read and write string.
But then I need to convert a QuantLib Date to and from a string?
Has that been done? (I'm guessing it has!)
Any help would be much appreciated.
Thanks
Philip
P.S for the record, to read and write xml, in C++, I used:
using boost::property_tree::ptree;
ptree pt;
read_xml("trade_data.xml", pt);
m_tradeID = pt.get<std::string>("trade.id");
where trade_data.xml contains:
<trade>
<id>GT2341251</id>
</trade>
------------------------------------------------------------------------------
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users