| | |
c++ clock?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
That's slightly harder, but you have more control and it's actually portable:
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.
C++ Syntax (Toggle Plain Text)
#include <ctime> #include <iostream> int main() { std::time_t now = std::time ( 0 ); std::tm *local = std::localtime ( &now ); local->tm_hour -= 6; std::time_t before = std::mktime ( local ); std::cout<<"Now: "<< ctime ( &now ) <<'\n'; std::cout<<"6 hours ago: "<< ctime ( &before ) <<'\n'; }
I'm here to prove you wrong.
•
•
Join Date: Apr 2008
Posts: 1
Reputation:
Solved Threads: 1
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.
Another function you could do here would be a generic "wait" function.
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.
C++ Syntax (Toggle Plain Text)
if (clock() > FiveSecondsLater) { // Do whatever it is you want to do. }
Another function you could do here would be a generic "wait" function.
C++ Syntax (Toggle Plain Text)
void wait (float seconds) { clock_t EndTime = seconds * CLOCKS_PER_SEC; while (EndTime > clock()) { // this will loop until clock() equals EndTime } }
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.
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.
>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 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.
![]() |
Similar Threads
- Synchronize Your Computer Clock with an Internet Time Server (Windows tips 'n' tweaks)
- System clock lose about 4mins per hour!!! (Windows NT / 2000 / XP)
- Update Computer Clock Through a Firewall (Windows tips 'n' tweaks)
- Mandrake guest in VMware - System clock frozen... (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: Double to string conversion with sprintf
- Next Thread: Error when evaluate some Postfix
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux 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 test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






