In the header file time.h
there are functions and types of data to manipulate time and date information.
The type of data struct tm
defines a structure to be used in functions working with date and time.
struct tm {
int tm_sec; // Indica os segundos de 0 a 59
int tm_min; // Indica os minutos de 0 a 59
int tm_hour; // Indica as horas de 0 a 24
int tm_mday: // Indica os dias do mês de 1 a 31
int tm_mon; // Indica os meses do ano de 0 a 11
int tm_year; // Indica o ano a partir de 1900
int tm_wday; // Indica o dia da semana de 0 (domingo) até 6 (sábado)
int tm_yday; // Indica o dia do ano de 1 a 365
int tm_isdst; // Indica o horário de verão se for diferente de zero
};
The type of data time_t
is used to represent calendar time.
The function localtime()
receives the time in seconds of a variable of type time_t
, converts to local time, stores the data in its structure and returns a pointer to a struct
of the kind tm
with the local data.
Example of use:
#include <stdio.h>
#include <time.h>
int main(void)
{
struct tm *p;
time_t seconds;
time(&seconds);
p = localtime(&seconds);
printf("Dia do ano: %d\n", p->tm_yday);
printf("Data: %d/%d/%d\n", p->tm_mday, p->tm_mon + 1, p->tm_year + 1900);
printf("Hora: %d:%d:%d\n", p->tm_hour, p->tm_min, p->tm_sec);
printf("\nGeral: %s\n", ctime(&seconds));
return 0;
}