私は多くの例を見てきましたが、この単純なケースに対処しているようです。解析したい文字列は次のとおりです。
「2012-06-0116:45:34EDT」
私は次のように作成しようとしましたlocal_time_input_facet
:
"%Y-%m-%d %H:%M:%S %Z"
local_date_time
オブジェクトのゾーンポインタは常に設定されていません。ドキュメントを読むことは混乱を招きます:
%Z *!フルタイムゾーン名(出力のみ)。ptimeでtime_facetを使用する場合、このフラグは無視されます。
"EDT"//東部夏時間
誰かがこれを以前にやったことがありますか?
更新:問題をもう少しよく説明するためにコードを更新しました:
using namespace std;
using namespace boost::local_time;
int main()
{
stringstream ss;
// Set up the input datetime format.
local_time_input_facet *input_facet
= new local_time_input_facet("%Y-%m-%d %H:%M:%S %ZP");
ss.imbue(std::locale(ss.getloc(), input_facet));
local_date_time ldt(not_a_date_time),ldt1(not_a_date_time);
// Read a time into ldt
ss.str("2012-06-01 17:45:34 EDT");
ss >> ldt;
ss.str("2012-06-01 17:45:34 CDT");
ss >> ldt1;
std::cerr << (ldt - ldt1).total_seconds() << std::endl;
// Write the time to stdout.
cout << "Full Time:\t" << ldt.to_string() << endl;
cout << "Local time:\t" << ldt.local_time() << endl;
cout << "Time zone:\t" << ldt.zone_as_posix_string() << endl;
cout << "Zone abbrev:\t" << ldt.zone_abbrev() << endl;
cout << "Zone offset:\t" << ldt.zone_abbrev(true) << endl;
cout << "Full Time:\t" << ldt1.to_string() << endl;
cout << "Local time:\t" << ldt1.local_time() << endl;
cout << "Time zone:\t" << ldt1.zone_as_posix_string() << endl;
cout << "Zone abbrev:\t" << ldt1.zone_abbrev() << endl;
cout << "Zone offset:\t" << ldt1.zone_abbrev(true) << endl;
return 0;
}
出力:
0
Full Time: 2012-Jun-01 17:45:34 EDT
Local time: 2012-Jun-01 17:45:34
Time zone: EDT+00
Zone abbrev: EDT
Zone offset: +0000
Full Time: 2012-Jun-01 17:45:34 CDT
Local time: 2012-Jun-01 17:45:34
Time zone: CDT+00
Zone abbrev: CDT
Zone offset: +0000