challenge ミリ秒まで含んだ時刻文字列

YYYY年mm月dd日HH時MM分SS.xxx秒なら、「YYYYmmddHHMMSS.xxx」のようにミリ秒まで含んだ文字列を返すプログラムを書いてください。

Posted feedbacks - C++

Boost使ってみました。 ちゃんと理解していないので、出力のあたりがちょっと汚らしい。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdlib>
#include <iostream>
#include <boost/format.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

using namespace std;
using namespace boost;
using namespace boost::gregorian;
using namespace boost::posix_time;

int main(void)
{
        ptime now = microsec_clock::local_time();

        cout    << format("%s%02d%02d%02d.%03d")
                % to_iso_string(now.date()).c_str()
                % now.time_of_day().hours()
                % now.time_of_day().minutes()
                % now.time_of_day().seconds()
                % (now.time_of_day().fractional_seconds() / 1000)
                << endl;

        return EXIT_SUCCESS;
}

ついでにQtで書いてみました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <QtCore>

#include <cstdlib>
#include <iostream>

using namespace std;

int main(void)
{
        QDateTime now = QDateTime::currentDateTime();

        cout << now.toString("yyyyMMddhhmmss.zzz").toStdString() << endl;

        return EXIT_SUCCESS;
}

Index

Feed

Other

Link

Pathtraq

loading...