How would I get the local time (just time, not date) and compare it. I want to say something like if it is 12:00 run this code.
How would I get the local time (just time, not date) and compare it. I want to say something like if it is 12:00 run this code.
Look into the ctime header file and the tm struct. More specifically, look at localtime() within ctime header or look it up at cppreference.com.
Last edited by elad; 07-25-2005 at 07:49 AM.
You're only born perfect.
This is what my code looks like:
but that returns the date as well. Is there a way to parse what it returns. And how would I compare it?Code:#include <iostream> #include <time.h> #include <stdio.h> using namespace std; int main() { time_t timedata; time(&timedata); cout << ctime(&timedata); return 0; }
EDIT: strftime() looks promising
Last edited by zach0616; 07-25-2005 at 08:08 AM.
I'd use localtime(). It returns a pointer to a tm struct. I would then display the members of the struct I wanted, rather than being forced to return an entire string. Alternatively, as you suggested, you could save the string returned by ctime() to a temp string, and parse out the values you want, but that seems harder to me. I'm not familiar with strftime(), I'll have to look it up.
Here's a link to an example of using strftime(). Looks like it should work, if you want.
http://tutorials.programmingsite.co.uk/cstrftime.php;
Last edited by elad; 07-25-2005 at 09:04 AM.
You're only born perfect.