2

次の形式を使用して、日付と時刻を文字列としてフォーマットします。

20130630-03:11:45.862

これのほとんどはstrftimeを使用して実行できますが、最後に小数秒を達成する明確な方法はありません。

私の現在のコードは次のとおりです。

time_t rawtime;
time(&rawtime);
tm* timeinfo = localtime(&rawtime);
char buffer[80];
strftime(buffer, 80, "%G%m%d-%I:%M:%S", timeinfo);

これにより、小数秒部分のない値が生成されます。

ただし、最終的には、この形式の日付の文字列バージョンが必要であり、どの API が必要かは気にしません。

関連する場合に備えて、Linuxでg ++を使用しています。

4

1 に答える 1

2

API を気にしない場合は、boost::date_timeを使用できます。これはtime_facetです。

これまでの短い例:

// setup facet and zone
// this facet should result like your desired format
std::string facet="%Y%m%d-%H:%M:%s";
std::string zone="UTC+00";

// create a facet
boost::local_time::local_time_facet *time_facet;
time_facet = new boost::local_time::local_time_facet;

// create a stream and imbue the facet
std::stringstream stream(std::stringstream::in | std::stringstream::out);
stream.imbue(std::locale(stream.getloc(), time_facet));

// create zone
boost::local_time::time_zone_ptr time_zone;
time_zone.reset(new boost::local_time::posix_time_zone(zone));

// write local from calculated zone in the given facet to stream
stream << boost::local_time::local_microsec_clock::local_time(time_zone);

// now you can get the string from stream
std::string my_time = stream.str();

私のコードをいくつかコピーしたため、この例は不完全かもしれませんが、要点を理解していただければ幸いです。

ファセットを使用すると、フォーマットを設定できます。(%s小さい s あり、フラクタルなしの大きな S) フラクシャルありのセットアップ秒。これはドキュメントファセット形式で読むことができます。

タイムゾーンは、ローカル マシンの時刻を正しいゾーンに合わせて計算するためのものです。

于 2013-06-30T01:44:17.253 に答える