how to cast current system time in array??

i want to get time and date singly and cast in to array i have peroblem to cast in array ,unsigned int new_date= date(d,m,y) is true??
cout<<new_date<<endl; isnt display current date...memcpy(write,&new_date,sizeof(int)) is true?;

using namespace std;
int date(int &Day, int &Month, int&Year) {
time_t currentTime;
struct tm *localTime;

time(&currentTime);
localTime = localtime(&currentTime);

Day = localTime->tm_mday; // use the reference argument
Month = localTime->tm_mon + 1;
Year = localTime->tm_year + 1900;

return (0);
int time(int &Hour, int &Min, int&Sec) {
time_t currentTime;
struct tm *localTime;

time(&currentTime);
localTime = localtime(&currentTime);

int Hour = localTime->tm_hour;
int Min = localTime->tm_min;
int Sec = localTime->tm_sec;
return (0);
}
int main() {
int d,m,y;
unsigned int new_date=date(d,m,y);
char write[4];
memcpy(write,&new_date,sizeof(int));
int h,m,s;
unsigned int new_time=time(h,m,s);
char wrt[4];
memcpy(wrt,&new_time,sizeof(int));



}

Last edited on
What are you trying to do? time_t is already a numerical value.

Both of your function date(...) and time(...) return 0 no matter what.

http://www.cplusplus.com/reference/ctime/time/?kw=time
1. Please use code tags! (inc. going back an editing you opening post, so anyone who reads this thread in the future can see what's going on better!) And ensure your code is well formatted (indent, spacing, etc.)

"How to use code tags"
http://www.cplusplus.com/articles/jEywvCM9/

2. The code you posted doesn't compile. You say cout<<new_date<<endl; isn't displaying the current date, but as it stands the code won't even compile!

3. Then...

i want to get time and date singly and cast in to array

Cast in to an array for what purpose?

i have peroblem to cast in array ,unsigned int new_date= date(d,m,y) is true??

If you're asking if this the correct way to do it, the answer is no.

cout<<new_date<<endl; isnt display current date...

Hardly surprising as new_date is an int which you've set to the value of 0.

memcpy(write,&new_date,sizeof(int)) is true?;

If you mean, is this the right way to do it. The answer is no.

I have to admit I don't get what you're trying to do. If you're trying to display the date (possible, given you're using cout) then check out strftime()
http://www.cplusplus.com/reference/ctime/strftime/

If you want people to help you with your problem, don't put barriers in their way!

Andy
Last edited on
tnx
i think solved it ........tnx
string date(){
time_t rawtime;
struct tm * timeinfo;
char buffer [80];

time (&rawtime);
timeinfo = localtime (&rawtime);

strftime (buffer,80,"%D",timeinfo);
puts (buffer);

return buffer;
}
string time(){
time_t rawtime;
struct tm * timeinfo;
char bffr [80];

time (&rawtime);
timeinfo = localtime (&rawtime);

strftime (bffr,80,"%T",timeinfo);
puts (bffr);

return bffr;
}
Topic archived. No new replies allowed.