| | |
Getting time from system clock in nanoseconds
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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. :-)
Besides, by the time the light traveled from your screen to your eyeball, the time in nanoseconds would be out-of-date. :-)
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
C++ Syntax (Toggle Plain Text)
#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"; }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2008
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
C++ Syntax (Toggle Plain Text)
#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"; }
Thanks.
![]() |
Similar Threads
- Java Code for getting Clock to show time (Java)
- calling system clock? (C++)
- Mandrake guest in VMware - System clock frozen... (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: Dynamic memmory Allocation , Using Pointers .Help me .
- Next Thread: Need help
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






