How can i get the time from the system clock in nanoseconds??
im using c++ here..
thanx..

Recommended Answers

All 6 Replies

On Windows, you can get the number of milliseconds since the processor started (GetTickCount()), and there are 'high resolution timers' (see "multimedia timer" or "high resolution timer" in VC help) that are higher resolution than that, but you won't get an accurate number of nanoseconds.

Besides, by the time the light traveled from your screen to your eyeball, the time in nanoseconds would be out-of-date. :-)

How can i get tha system boot time.plz provide me solution with code.
i want time in usec from system booted

system boot time is the value returned by GetTickCount(). If you want to know the actual date/time, call time() function in time.h, then localtime() to get struct tm pointer. From that subtract the number of seconds returned by GetTickCount(), pass that result to mktime() (from time.h) which will do all the math needed to normalize the date/time in struct tm. Volla -- you have the actual date/time the system was booted.

#include <iostream>
#include <iomanip>
#include <ctime>
#include <windows.h>
using namespace std;


int main()
{
    DWORD ticks = GetTickCount() / 1000;
    cout << ticks << "\n";
    time_t now = time(0);
    struct tm* tm = localtime(&now);
    tm->tm_sec -= ticks;
    now = mktime(tm);
    cout << setw(2) << setfill('0') << tm->tm_mon << ":" << setw(2) << setfill('0') << 
        tm->tm_mday << ":" << setw(4) <<tm->tm_year + 1900 << "  ";
    cout << setw(2) << setfill('0') << tm->tm_hour << "/" << 
        setw(2) << setfill('0') << tm->tm_min << "/" << setw(2) << setfill('0') << tm->tm_sec << "\n";

}

Wow, a 4 year bump, and the answer is still the same?
Did the bumper bother to read anything, or just attach the same question based on the result of a feeble search?

#include <iostream>
#include <iomanip>
#include <ctime>
#include <windows.h>
using namespace std;


int main()
{
    DWORD ticks = GetTickCount() / 1000;
    cout << ticks << "\n";
    time_t now = time(0);
    struct tm* tm = localtime(&now);
    tm->tm_sec -= ticks;
    now = mktime(tm);
    cout << setw(2) << setfill('0') << tm->tm_mon << ":" << setw(2) << setfill('0') << 
        tm->tm_mday << ":" << setw(4) <<tm->tm_year + 1900 << "  ";
    cout << setw(2) << setfill('0') << tm->tm_hour << "/" << 
        setw(2) << setfill('0') << tm->tm_min << "/" << setw(2) << setfill('0') << tm->tm_sec << "\n";

}

How can we get time on Linux in nanoseconds?

Thanks.

commented: Clues for the clueless, or at least unable to read. -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.