user1155299
user1155299

Reputation: 927

converting string to boost::date and around

I have the following date :

std::string sdt("2011-01-03");

I wrote 2 functions as below and called them using:

 date test;
  string_to_date(sdt,test);
  date_to_string(test,sdt);

the string_to_date() works and it returns 2011-Jan-03 as it should whereas the date_to_string() returns

not-a-date-time

these are the functions:

void string_to_date(const std::string& st, date out)
{
  std::string in=st.substr(0,10);
  date d1(from_simple_string(std::string (in.begin(), in.end())));
  std::cout<<d1;
  out=d1;
}


void date_to_string(date in, const std::string& out)
{

  date_facet* facet(new date_facet("%Y-%m-%d"));
  std::cout.imbue(std::locale(std::cout.getloc(), facet));
  std::cout<<in<<std::endl;

    out=in;//this doesn't work

}

Upvotes: 1

Views: 208

Answers (1)

Florian Sowade
Florian Sowade

Reputation: 1747

void date_to_string(date in, std::string& out)
{
    std::ostringstream str;
    date_facet* facet(new date_facet("%Y-%m-%d"));
    str.imbue(std::locale(str.getloc(), facet));
    str << in;

    out = str.str();
}

should work. Notice the removed const from the out parameter. Is there a reason not to simply return the produced string?

Upvotes: 2

Related Questions