Getting time from system clock in nanoseconds

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 3
Reputation: Flawless is an unknown quantity at this point 
Solved Threads: 0
Flawless Flawless is offline Offline
Newbie Poster

Getting time from system clock in nanoseconds

 
0
  #1
Oct 27th, 2004
How can i get the time from the system clock in nanoseconds??
im using c++ here..
thanx..
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Getting time from system clock in nanoseconds

 
0
  #2
Oct 27th, 2004
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. :-)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 1
Reputation: Rita agrawal is an unknown quantity at this point 
Solved Threads: 0
Rita agrawal Rita agrawal is offline Offline
Newbie Poster

Re: Getting time from system clock in nanoseconds

 
0
  #3
Aug 5th, 2008
How can i get tha system boot time.plz provide me solution with code.
i want time in usec from system booted
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Getting time from system clock in nanoseconds

 
0
  #4
Aug 5th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Getting time from system clock in nanoseconds

 
0
  #5
Aug 5th, 2008
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <ctime>
  4. #include <windows.h>
  5. using namespace std;
  6.  
  7.  
  8. int main()
  9. {
  10. DWORD ticks = GetTickCount() / 1000;
  11. cout << ticks << "\n";
  12. time_t now = time(0);
  13. struct tm* tm = localtime(&now);
  14. tm->tm_sec -= ticks;
  15. now = mktime(tm);
  16. cout << setw(2) << setfill('0') << tm->tm_mon << ":" << setw(2) << setfill('0') <<
  17. tm->tm_mday << ":" << setw(4) <<tm->tm_year + 1900 << " ";
  18. cout << setw(2) << setfill('0') << tm->tm_hour << "/" <<
  19. setw(2) << setfill('0') << tm->tm_min << "/" << setw(2) << setfill('0') << tm->tm_sec << "\n";
  20.  
  21. }
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Getting time from system clock in nanoseconds

 
0
  #6
Aug 5th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 9
Reputation: brain is an unknown quantity at this point 
Solved Threads: 0
brain brain is offline Offline
Newbie Poster

Re: Getting time from system clock in nanoseconds

 
-1
  #7
Sep 2nd, 2008
Originally Posted by Ancient Dragon View Post
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <ctime>
  4. #include <windows.h>
  5. using namespace std;
  6.  
  7.  
  8. int main()
  9. {
  10. DWORD ticks = GetTickCount() / 1000;
  11. cout << ticks << "\n";
  12. time_t now = time(0);
  13. struct tm* tm = localtime(&now);
  14. tm->tm_sec -= ticks;
  15. now = mktime(tm);
  16. cout << setw(2) << setfill('0') << tm->tm_mon << ":" << setw(2) << setfill('0') <<
  17. tm->tm_mday << ":" << setw(4) <<tm->tm_year + 1900 << " ";
  18. cout << setw(2) << setfill('0') << tm->tm_hour << "/" <<
  19. setw(2) << setfill('0') << tm->tm_min << "/" << setw(2) << setfill('0') << tm->tm_sec << "\n";
  20.  
  21. }
How can we get time on Linux in nanoseconds?

Thanks.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC