2

4 桁の年とその年の週番号で構成される文字列を解析したいと考えています。ブースト日付/時刻 IO チュートリアルに従い、次のようなテスト例を作成しました。

std::string week_format = "%Y-W%W";
boost::date_time::date_input_facet<boost::gregorian::date, char> week_facet =     boost::date_time::date_input_facet<boost::gregorian::date, char>(week_format);

std::stringstream input_ss;
input_ss.imbue(locale(input_ss.getloc(), &week_facet));

std::string input_week = "2004-W34";
input_ss.str(input_week);

boost::gregorian::date input_date;
input_ss >> input_date;

残念ながら、input_date単に「2004-01-01」と出力され、年を解析しただけであることを暗示しています。私は何を間違っていますか?%W入力で使用できませんか? (ドキュメントではそのようにマークされていません。)

4

1 に答える 1

1

ドキュメントの「フォーマットフラグ」セクションでそのようにマークされていないのは正しいです(横に「!」はありません...)

http://www.boost.org/doc/libs/1_35_0/doc/html/date_time/date_time_io.html#date_time.format_flags

しかし、それは見落としのようです。Boostのformat_date_parser.hpp場合、このケースのカバレッジがないためparse_date... switchステートメントを見て、次のことを確認できます。

http://svn.boost.org/svn/boost/trunk/boost/date_time/format_date_parser.hpp

それを行うためのコードがないにもかかわらず、ソースのコメントでさえ、解析入力で%Wと%Uを処理すると言っています。どうしたの?:-/

別の注意点として、あなたの例では、week_facetを動的に割り当てる必要があると思います。

std::string week_format = "%Y-W%W";
boost::date_time::date_input_facet<boost::gregorian::date, char>* week_facet =
    new boost::date_time::date_input_facet<boost::gregorian::date, char>(
        week_format
    );

std::stringstream input_ss;
input_ss.imbue(std::locale(input_ss.getloc(), week_facet));

(または、少なくとも、例がクラッシュしないようにするために、そのようにする必要がありました。)

于 2011-11-01T05:48:43.203 に答える