Get Date and time to send to a struct type variable in c ++

1

Best regards, I have a question, I appreciate your kind support:

using namespace std;

int main(int argc, char *argv[])
{

//Fecha basado en el sistema actual de Sistema operativo
time_t now= time(0); // captura fecga y hora actual

tm *time =localtime(&now); //ceamo el puntero time y se referencia objeto time y se guarda en var now
cout << "Dia de la semana:" << time->tm_wday << endl;  //accedemos al objeto time y miemto

system("Pause");
return 0;
}

The question is how I send the date and time to a variable in c ++

    
asked by Victor Perales 23.12.2017 в 02:17
source

2 answers

3

This is the definition of the structure tm:

struct tm {
    int tm_sec;         /* seconds */
    int tm_min;         /* minutes */
    int tm_hour;        /* hours */
    int tm_mday;        /* day of the month */
    int tm_mon;         /* month */
    int tm_year;        /* year */
    int tm_wday;        /* day of the week */
    int tm_yday;        /* day in the year */
    int tm_isdst;       /* daylight saving time */
};

You can read the data and store it in a variable or structure, but if you want a unique number then just use now which is the number of seconds since January 1, 1970.

    
answered by 23.12.2017 / 03:01
source
1
  

How do I send the date and time to a variable in C ++?

I understand that by "sending" the date and time to a variable you mean saving the data. If you mean that, you've already done it:

tm *time =localtime(&now);
//  ~~~~ <-- fecha y hora guardadas en la variable time

But this is not the C ++ way to do it, the structure tm belongs to the data types of C. To do so in C ++, the header <chrono> should be used.

The header <chrono> gives access to different clocks (system clock, stable clock, high resolution clock) and several utilities to work with time measures. To get the current time just write:

auto ahora = std::chrono::system_clock::now();
//   ~~~~~ <-- fecha y hora guardadas en la variable ahora

The call to system_clock::now() returns an object of type time_point that can be used with other variables of the same type to make operations (subtract or add time, compare times ...).

    
answered by 08.01.2018 в 16:09