16

タイマーからミリ秒を取得する必要があります

    // get timer part
    time_t timer = time(NULL);
    struct tm now = *localtime( &timer );
    char timestamp[256];

    // format date time
    strftime(timestamp, sizeof(timestamp), "%Y-%m-%d_%H.%M.%S", &now);

私はC#でこのようになりたい:

DateTime.Now.ToString("yyyy-MM-dd_HH.mm.ss.ff")

%f または %F を使用してみましたが、C++ で動作します。tm からミリ秒単位で %f を取得するにはどうすればよいですか?

4

8 に答える 8

23
#include <chrono>

typedef std::chrono::system_clock Clock;

auto now = Clock::now();
auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
auto fraction = now - seconds;
time_t cnow = Clock::to_time_t(now);

次に、秒の精度で time_t を出力し、分数が表すものを出力できます。ミリ秒、マイクロ秒、またはその他の可能性があります。具体的にミリ秒を取得するには:

auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(fraction);
std::cout << milliseconds.count() << '\n';
于 2012-05-18T14:29:32.503 に答える
7

関数 getimeofday() があります。時間をミリ秒単位で返します。ここで確認してください: http://souptonuts.sourceforge.net/code/gettimeofday.c.html

于 2012-05-18T14:25:59.863 に答える
6

Win32 API SYSTEMTIME 構造を使用する Windows では、ミリ秒が得られます。次に、時間関数を使用して時間を取得する必要があります。このような:

#include <windows.h>

int main()
{
    SYSTEMTIME stime;
    //structure to store system time (in usual time format)
    FILETIME ltime;
    //structure to store local time (local time in 64 bits)
    FILETIME ftTimeStamp;
    char TimeStamp[256];//to store TimeStamp information
    GetSystemTimeAsFileTime(&ftTimeStamp); //Gets the current system time

    FileTimeToLocalFileTime (&ftTimeStamp,&ltime);//convert in local time and store in ltime
    FileTimeToSystemTime(&ltime,&stime);//convert in system time and store in stime

    sprintf(TimeStamp, "%d:%d:%d:%d, %d.%d.%d",stime.wHour,stime.wMinute,stime.wSecond, 
            stime.wMilliseconds, stime.wDay,stime.wMonth,stime.wYear);

    printf(TimeStamp);

    return 0;
} 
于 2012-05-18T15:05:53.773 に答える
6

これが別のc ++ 11の答えです:

#include <iostream>
#include <iomanip>
#include <chrono>
#ifdef WIN32
#define localtime_r(_Time, _Tm) localtime_s(_Tm, _Time)
#endif

int main()
{
    tm localTime;
    std::chrono::system_clock::time_point t = std::chrono::system_clock::now();
    time_t now = std::chrono::system_clock::to_time_t(t);
    localtime_r(&now, &localTime);

    const std::chrono::duration<double> tse = t.time_since_epoch();
    std::chrono::seconds::rep milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(tse).count() % 1000;

    std::cout << (1900 + localTime.tm_year) << '-'
        << std::setfill('0') << std::setw(2) << (localTime.tm_mon + 1) << '-'
        << std::setfill('0') << std::setw(2) << localTime.tm_mday << ' '
        << std::setfill('0') << std::setw(2) << localTime.tm_hour << ':'
        << std::setfill('0') << std::setw(2) << localTime.tm_min << ':'
        << std::setfill('0') << std::setw(2) << localTime.tm_sec << '.'
        << std::setfill('0') << std::setw(3) << milliseconds
        << std::endl;
}
于 2015-06-01T22:03:04.067 に答える
2

boost::posix_time::ptimeクラスを受講できます。その参照があります。

于 2012-05-18T14:25:19.040 に答える
2

私は、個人的に、これを使用します: http://wyw.dcweb.cn/time.htm

于 2012-05-18T16:04:20.333 に答える
1

MS Visual Studio c/c++ では、sys/timeb.h をインクルードし、_ftime (_ftime_s) を使用します。time_t 構造体とミリ秒を含む struct _timeb (_ftime64) を取得します。MSDN http://msdn.microsoft.com/en-/library/z54t9z5f.aspxを参照してください。

#include <sys/timeb.h>

struct _timeb timebuffer;
_ftime(&timebuffer);
timebuffer.millitm; //milli seconds
timebuffer.time; //the same like struct time_t
于 2015-07-28T07:36:06.963 に答える
0

このコードで試してください:

struct tvTime;

gettimeofday(&tvTime, NULL);

int iTotal_seconds = tvTime.tv_sec;
struct tm *ptm = localtime((const time_t *) & iTotal_seconds);

int iHour = ptm->tm_hour;;
int iMinute = ptm->tm_min;
int iSecond = ptm->tm_sec;
int iMilliSec = tvTime.tv_usec / 1000;
int iMicroSec = tvTime.tv_usec;
于 2016-05-27T17:43:35.313 に答える