is there some kind of magic function i can use to set a variable equal to the time of the day?
if so, how would i use this function?
is there some kind of magic function i can use to set a variable equal to the time of the day?
if so, how would i use this function?
"uh uh uh, you didn't say the magic word"
-Jurassic Park
#include <time.h>
time_t now;
time_t *pNow;
now = time(pNow);
the system time is stored in the variable now and in the pointer pNow. You could use now or pnow alone if desired. Unfortunately time_t and time_t * are not convenient formats so you can convert them like this:
char sNow[30] = ctime(pnow);
tm tNow = localtime(pnow);
the char string returned by ctime() is something like:
Mon Dec 31, 2001
and tm is a struct something like this:
struct tm
{
int tm_month;
int tm_day;
int tm_year;
int day_of_week;
//etc.
}
Exact formats, prototypes, definitions, etc. should be available in the help section of your compiler.
could the above code be modified to tell me the time of the day according to the internal clock on the comp?
"uh uh uh, you didn't say the magic word"
-Jurassic Park
I guess what im asking is....
How could i manipulate the variable holding the day of the week, month, day and time that the variable is holding into just time?
Last edited by heat511; 01-03-2002 at 11:23 AM.
"uh uh uh, you didn't say the magic word"
-Jurassic Park
Originally posted by Unregistered
#include <time.h>
time_t now;
time_t *pNow;
now = time(pNow);
Woah, is that an uninatialized pointer there? It looks like its pointing to nowhere, but then you pass it to the time function, which fills it in, so your filling up some random memory. Not cool
http://www.cppreference.com/stddate.html has a reference for all the time functions
The localtime function returns a struct that has a bunch of info
info here from http://www.devx.com/free/tips/tipvie...ontent_id=1810
struct tm
{
int tm_sec; // the number of elapsed seconds in the minute
int tm_min; // the number of elapsed minutes in the hour
int tm_hour; // the number of elapsed hours in the day (0-23)
int tm_mday; / /the number of elapsed days in the month (1-31)
int tm_mon; // the number of elapsed months in the year (0-11)
int tm_year; //the numbers of elapsed years since 1900
int tm_wday; // the number of elapsed days in the week since Sunday (0-6)
int tm_yday; //the number of the elapsed days in the year (0-365)
int tm_isdst; // equals 1 if daylight savings is in effect, zero if not, -1 if unknown
};
You're right, using unitialize pointers can be problematic. But it's not as ominus as you make it sound. The code snippet you refer to is no different than this:
int num = 1;
int * pNum;
pNum = #
Used with respect unitialized pointers are not going to blow up or anything.
To refer to certain members of a struct, but not all, you can use the dot or arrow operators. Using the dot operator it goes something like ths:
//using snippet from first post:
cout << tNow.tm_hour << ':' << tNow.tm_min << ':' << tNow.tm_sec << endl;
leave out the colons if you wish. Manipulate tm structs as you need to to use whatever display format you want.