Greetings programmers.
I am declaring the following class in C++:
class Fecha {
public:
Fecha();
Fecha(int dia, int mes, int anio);
Fecha(const Fecha& orig);
virtual ~Fecha();
int getDia();
void setDia(const int dia);
int getMes();
void setMes(const int mes);
int getAnio();
void setAnio(const int anio);
private:
int _dia;
int _mes;
int _anio;
};
Well, it turns out that I have to tell the default constructor (the one in the .cpp file ) to assign the data corresponding to the current date to the attributes. I have looked carefully at the following post: How to get current time and date in C++?
I have not made anything clear, because I do not want to obtain the date as a string or anything like that, but rather that each variable (attribute) be assigned the data that it bears by name. But I haven't found a way to do it. Only two codes were taken out separately:
#include <stdio.h>
#include <time.h>
int main ()
{
time_t current_time;
struct tm local_time;
time ( ¤t_time );
localtime_s(&local_time, ¤t_time);
int Year = local_time.tm_year + 1900;
int Month = local_time.tm_mon + 1;
int Day = local_time.tm_mday;
int Hour = local_time.tm_hour;
int Min = local_time.tm_min;
int Sec = local_time.tm_sec;
return 0;
}
This, lets me understand that a part of it is missing, because localtime_s
it is not recognized.
#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>
using namespace boost::gregorian;
int main()
{
date d = day_clock::universal_day();
std::cout << d.day() << " " << d.month() << " " << d.year();
}
This other uses a library that, of course, has little "by default", and even less that I can download considering that this project must be able to run on any PC (important, not only on Windows).
So, I'm out of ideas to get the date.
Thank you very much once again for your help. Greetings.
It doesn't recognize you
localtime_s( )
because that's a POSIX function, only available on POSIX-compliant systems.The ISO standard has a very similar function and it should be available on any platform:
localtime( )
:However, since you are in C++, you can use the functions available in this language (as of C++11):
As you can see, there is not much difference. The real work continues
localtime( )
.