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

有函数gettimeofday()。以毫秒为单位返回时间检查这里:http ://souptonuts.sourceforge.net/code/gettimeofday.c.html

于 2012-05-18T14:25:59.863 回答
6

在 Windows 上使用 Win32 API SYSTEMTIME 结构会给你毫秒。然后,您应该使用时间函数来获取时间。像这样:

#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 结构和毫秒的结构 _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 回答