c++ clock?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 11
Reputation: spuddy1515 is an unknown quantity at this point 
Solved Threads: 0
spuddy1515 spuddy1515 is offline Offline
Newbie Poster

c++ clock?

 
0
  #1
Apr 22nd, 2008
I need help with manipulating my computers clock through a c++ program and i have no idea on how to make the program. Does anyone have any idea on how to do this?
spuddy1515 was here!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 724
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: c++ clock?

 
1
  #2
Apr 22nd, 2008
What kind of manipulation are you looking for? Standard C++ pretty much only gives you query capabilities for the clock, though you have comprehensive time and date control.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: spuddy1515 is an unknown quantity at this point 
Solved Threads: 0
spuddy1515 spuddy1515 is offline Offline
Newbie Poster

Re: c++ clock?

 
0
  #3
Apr 22nd, 2008
well i want it so that i can take it and subtract a chosen amount of time from the clock.
spuddy1515 was here!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 724
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: c++ clock?

 
1
  #4
Apr 22nd, 2008
To what end? You can easily call std::clock() to get the current value and perform math on it. But the result may not be meaningful.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: spuddy1515 is an unknown quantity at this point 
Solved Threads: 0
spuddy1515 spuddy1515 is offline Offline
Newbie Poster

Re: c++ clock?

 
0
  #5
Apr 22nd, 2008
like take the current time and subtract say 6 hours. This is all for a timezone project im doing and i have everything but the clock
spuddy1515 was here!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 724
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: c++ clock?

 
1
  #6
Apr 22nd, 2008
That's slightly harder, but you have more control and it's actually portable:
  1. #include <ctime>
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6. std::time_t now = std::time ( 0 );
  7. std::tm *local = std::localtime ( &now );
  8.  
  9. local->tm_hour -= 6;
  10.  
  11. std::time_t before = std::mktime ( local );
  12.  
  13. std::cout<<"Now: "<< ctime ( &now ) <<'\n';
  14. std::cout<<"6 hours ago: "<< ctime ( &before ) <<'\n';
  15. }
C++ supports something called a clock, and means system clock ticks, not wallclock time. Keep that in mind when asking questions about the "clock", because what you actually want to manipulate is date/time.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: spuddy1515 is an unknown quantity at this point 
Solved Threads: 0
spuddy1515 spuddy1515 is offline Offline
Newbie Poster

Re: c++ clock?

 
0
  #7
Apr 22nd, 2008
thanks ill try it out
spuddy1515 was here!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1
Reputation: Merkwurdigliebe is an unknown quantity at this point 
Solved Threads: 1
Merkwurdigliebe Merkwurdigliebe is offline Offline
Newbie Poster

Re: c++ clock?

 
0
  #8
Apr 23rd, 2008
While I'm no expert at C++ I think I can help you with your dilema.

I use the library time.h so you'd want to add

#include <time.h>

to your header files to use this. I saw an earlier post uses ctime but anyway

if you use time.h you'll be able to use things like this:
this is all assuming that you have using namespace std; in there as well, although I'm not sure that time.h requires it.

Anyway the main two things you want to use are clock() and clock_t

clock_t is a type. it is a variable type like int to store clock variables.
clock() returns the given system clock at a time. Now this number is some strange number that won't really make any sense to you, so you just have to work with it in the way that I explain.


Let's say you want to have something happen 5 seconds in the future. Here's what you do.

clock_t FiveSecondsLater; // This declares your time variable
FiveSecondsLater = clock() + 5 * CLOCKS_PER_SEC;

Now at this point, you'll have a variable "FiveSecondsLater" that equals the value that the system clock will be in five seconds. How does this work?

clock() will return a value something like 39492. This number goes up at a certain rate.
CLOCKS_PER_SEC is a constant. It is exactly how much clock() increases in a second.
So if you want to set a time for 5 seconds in the future, you multiply it by however many seconds you want to do.

So if you later want to do something after five seconds you'd use an if statement combined with the previous code.

  1. if (clock() > FiveSecondsLater)
  2. {
  3. // Do whatever it is you want to do.
  4. }



Another function you could do here would be a generic "wait" function.


  1. void wait (float seconds)
  2. {
  3. clock_t EndTime = seconds * CLOCKS_PER_SEC;
  4. while (EndTime > clock())
  5. {
  6. // this will loop until clock() equals EndTime
  7. }
  8.  
  9. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: c++ clock?

 
0
  #9
Apr 23rd, 2008
Well all C-based compilers have to add in the headers with '.h' following them. Later after the formation of c++ the programmers have ruled out the usage of '.h' and added c in the begininning

So
#include <ctime>
or
#include <time.h>
Refers to a header file with same content in it.

time.h is available so that even old c programs can be compiled using a c++ compiler.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 724
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: c++ clock?

 
1
  #10
Apr 23rd, 2008
>While I'm no expert at C++ I think I can help you with your dilema.
I don't think you can, sorry.

>I use the library time.h so you'd want to add
time.h is deprecated in standard C++. New code should be written using the ctime header instead.

>this is all assuming that you have using namespace std; in
>there as well, although I'm not sure that time.h requires it.
time.h does not place any names in the std namespace, but ctime does.

>Anyway the main two things you want to use are clock() and clock_t
Did you even bother reading the thread before replying? We've already established that the OP wants to work with date/time, not clock ticks.

>clock_t FiveSecondsLater; // This declares your time variable
>FiveSecondsLater = clock() + 5 * CLOCKS_PER_SEC;
That's nice. Now explain how that can be used to given the current wall clock time for different timezones.

>Another function you could do here would be a generic "wait" function.
Which is an extremely bad idea. A busy wait is both inefficient and anti-social.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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