I want to get time and date separately to put it into an array. I would like the calling function to be like int date(int Day,int Month,int Year)
but it isnt correct. How can I define arguments Year Month Day and Hour min sec of function to use them ?
using namespace std;
int date() {
time_t currentTime;
struct tm *localTime;
time(¤tTime);
localTime = localtime(¤tTime);
int Day = localTime->tm_mday;
int Month = localTime->tm_mon + 1;
int Year = localTime->tm_year + 1900;
return (0);
}
int time() {
time_t currentTime;
struct tm *localTime;
time(¤tTime);
localTime = localtime(¤tTime);
int Hour = localTime->tm_hour;
int Min = localTime->tm_min;
int Sec = localTime->tm_sec;
return (0);
}
int main() {
unsigned int new_date=date();
char write[4];
memcpy(write,&new_date,4);
unsigned int new_time=time();
char wrt[4];
memcpy(wrt,&new_time,4);
}
You have to declare the function to pass arguments by reference:
// pass arguments by reference to change them in the function
int date(int &Day, int &Month, int&Year) {
time_t currentTime;
struct tm *localTime;
time(¤tTime);
localTime = localtime(¤tTime);
Day = localTime->tm_mday; // use the reference argument
Month = localTime->tm_mon + 1;
Year = localTime->tm_year + 1900;
return (0);
}
You can then use it like this in main()
:
int d,m,y;
date(d,m,y);
cout << d<<"/"<<m<<"/"<<y<<endl;
You can of course do the same thing for time.