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

Dani AI

Generated

and are both hinting at the key idea: you can format a value in nanoseconds, but the real resolution depends on the OS and hardware. For measuring short intervals, use a monotonic clock (not the wall clock). In modern C++, std::chrono::steady_clock is portable and exposes nanosecond precision, even if the underlying resolution is coarser.

#include <chrono>
#include <cstdint>

using namespace std::chrono;

uint64_t do_work_and_measure_ns() {
    auto start = steady_clock::now();
    // ... work you want to time ...
    auto ns = duration_cast<nanoseconds>(steady_clock::now() - start).count();
    return static_cast<uint64_t>(ns);
}

For asking about Linux nanoseconds: clock_gettime() gives you a timespec with tv_nsec. Use CLOCK_MONOTONIC for elapsed time (won’t jump if the user changes the system clock). On very old glibc you might need to link with -lrt; on current systems it’s in libc.

#include <time.h>
#include <stdint.h>

uint64_t now_ns_monotonic() {
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);           // or CLOCK_BOOTTIME if you need sleep time included
    return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
}

Windows notes to complement : GetTickCount() is millisecond precision and wraps at ~49.7 days; prefer GetTickCount64() on Vista+ for uptime. For high‑resolution intervals, use QueryPerformanceCounter()/QueryPerformanceFrequency() and convert to nanoseconds. Even then, expect accuracy bounded by the platform’s timer resolution. In short: use a monotonic clock for intervals, accept that true nanosecond accuracy is not guaranteed, and only use the wall clock when you actually need calendar time.

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.