Quantlib Date to and from xml file ( or to and from std::string)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Quantlib Date to and from xml file ( or to and from std::string)

P Nelnik
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
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib Date to and from xml file ( or to and from std::string)

P Nelnik
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
Reply | Threaded
Open this post in threaded view
|

Re: Quantlib Date to and from xml file ( or to and from std::string)

Fabio Ramponi
Hi all,
 
the string-Date conversion feature is already implemented in QuantLib :
 
- string -> QuantLib::Date : see QuantLib::DateParser class
- QuantLib::Date -> string : see methods defined in ql/time/date.hpp, QuantLib::io namespace.
             for example, you can convert a QuantLib::Date dt in a ISO formated string date in this way:
 
std::ostringstream out;
std::string outStr;
out << QuantLib::io::iso_date(dt);
outStr = out.str();
 
 
 
 
 


 
On Thu, May 13, 2010 at 9:29 AM, P Nelnik <[hidden email]> wrote:
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



------------------------------------------------------------------------------


_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users