954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Getting time from system clock in nanoseconds

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

Flawless
Newbie Poster
3 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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. :-)

Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

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

Rita agrawal
Newbie Poster
1 post since May 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
#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";

}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
#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.

brain
Newbie Poster
9 posts since Jan 2008
Reputation Points: 6
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You