将字符串转换为boost :: date和around

时间:2012-05-28 00:44:02

标签: c++ boost

我有以下日期:

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

我写了两个函数如下,并使用:

调用它们
 date test;
  string_to_date(sdt,test);
  date_to_string(test,sdt);

string_to_date()有效并返回 应该2011-Jan-03date_to_string()返回

not-a-date-time

这些是功能:

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

}

1 个答案:

答案 0 :(得分:2)

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();
}

应该有效。请注意const参数中已删除的out。有没有理由不简单地返回生成的字符串?