olidev
olidev

Reputation: 20644

Get millisecond part of time

I need to get milliseconds from the timer

    // 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);

I want to get like this in C#:

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

I tried using %f or %F but it does work with C++. how can I get %f for milliseconds from tm?

Upvotes: 19

Views: 83387

Answers (8)

jeremy
jeremy

Reputation: 21

Try with this code:

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;

Upvotes: 1

bames53
bames53

Reputation: 88155

#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);

Then you can print out the time_t with seconds precision and then print whatever the fraction represents. Could be milliseconds, microseconds, or something else. To specifically get milliseconds:

auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(fraction);
std::cout << milliseconds.count() << '\n';

Upvotes: 29

steven j
steven j

Reputation: 11

under MS Visual Studio c/c++ include sys/timeb.h and use _ftime (_ftime_s). Retrieves a struct _timeb (_ftime64) containing the time_t struct and also milli seconds, see 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

Upvotes: 1

Chris Desjardins
Chris Desjardins

Reputation: 2720

Here is another c++11 answer:

#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;
}

Upvotes: 7

John Kaul
John Kaul

Reputation: 340

I, personally, use this one: http://wyw.dcweb.cn/time.htm

Upvotes: 2

SChepurin
SChepurin

Reputation: 1854

On Windows using Win32 API SYSTEMTIME structure will give you milliseconds. Then, you should use Time Functions to get time. Like this:

#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;
} 

Upvotes: 7

amanda
amanda

Reputation: 384

there is the function getimeofday(). returns time in ms check here: http://souptonuts.sourceforge.net/code/gettimeofday.c.html

Upvotes: 7

inkooboo
inkooboo

Reputation: 2944

You can youse boost::posix_time::ptime class. Its reference there.

Upvotes: 2

Related Questions