Domanda
C'è un "principiante" modo facile per prendere il tempo corrente utilizzando <ctime>
ad un oggetto Date che ha
int month
int day
int year
per le sue variabili membro? Grazie.
Soluzione
time_t tt = time(NULL); // get current time as time_t
struct tm* t = localtime(&tt) // convert t_time to a struct tm
cout << "Month " << t->tm_mon
<< ", Day " << t->tm_mday
<< ", Year " << t->tm_year
<< endl
I int tm
struct sono tutti 0-based (0 = gennaio, 1 = febbraio) ed è possibile ottenere diverse misure giorno, il giorno in mesi (tm_mday
), settimana (tm_wday
) e anno (tm_yday
).
Altri suggerimenti
Se c'è localtime_r quindi si dovrebbe utilizzare localtime_r , piuttosto che localtime in quanto questa è la versione rientrante di localtime.
#include <ctime>
#include <iostream>
int main()
{
time_t tt = time(NULL); // get current time as time_t
tm tm_buf;
tm* t = localtime_r(&tt, &tm_buf); // convert t_time to a struct tm
std::cout << "Month " << t->tm_mon
<< ", Day " << t->tm_mday
<< ", Year " << t->tm_year
<< std::endl;
return 0;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow