Академический Документы
Профессиональный Документы
Культура Документы
Estrutura de Dados
Campus
Braslia
2 semestre de 2013
2013/2
1 / 15
Precisa da biblioteca
<time.h>.
time_t .
struct tm.
time_t
typedef
2013/2
2 / 15
struct tm
campo
signicado
valores
tm_sec
segundos
[0, 59]
tm_min
minutos
[0, 59]
tm_hour
horas
[0, 23]
tm_mday
dia do ms
[1, 31]
tm_mon
ms
[0, 11]
tm_year
tm_wday
dia da semana
[0, 6]
tm_yday
dia do ano
[0, 365]
tm_isdst
horrio de vero
2013/2
3 / 15
Funo
time()
time_t
GMT.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char** argv) {
time_t horario = time(NULL);
printf("Quantidade de segundos desde 1970: %d.\n", horario);
long seg = horario%60;
long min = (horario/60)%60;
long hora = (horario/3600)%24;
printf("Hora atual (GMT): %02d:%02d:%02d.\n", hora, min, seg);
return(EXIT_SUCCESS);
}
Alex Helder (IFB)
2013/2
4 / 15
Converso de
time_t
para
struct tm
localtime().
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main() {
time_t horario = time(NULL);
struct tm *h = localtime(&horario);
printf("data: %02d/%02d/%04d; hora: %02d:%02d:%02d\n",
h->tm_mday, h->tm_mon+1, h->tm_year+1900,
h->tm_hour, h->tm_min, h->tm_sec);
return(EXIT_SUCCESS);
}
2013/2
5 / 15
Converso de
struct tm
para
time_t
mktime();
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main() {
struct tm agenda;
agenda.tm_mday = 1;
agenda.tm_mon = 3;
agenda.tm_year = 2012-1900;
time_t hora = mktime(&agenda);
if(hora < time(NULL)) printf("Data j passou!");
else printf("Data ainda no passou!");
return(EXIT_SUCCESS);
}
Alex Helder (IFB)
2013/2
6 / 15
strftime();
3
4
2013/2
7 / 15
OFF-Topic:
Locale
<locale.h>.
setlocale()
da biblioteca
Ex (Windows):
Ex (Linux):
Alex Helder (IFB)
2013/2
8 / 15
Funo
strftime()
- Cdigos de Formato
%A
%b
Ms abreviado
%B
Ms completo
%c
%C
%d
Dia do ms numrico
%D
%e
%F
%g
%G
2013/2
9 / 15
Funo
strftime()
- Cdigos de Formato
Ms abreviado
%H
Hora (0-23)
%I
Hora (1-12)
%j
%m
Ms numrico
%M
Minuto
%n
Quebra de linha
%p
AM / PM
%r
%R
%S
Segundos
%t
Tabulao horizontal
2013/2
10 / 15
Funo
strftime()
- Cdigos de Formato
%u
%U
Semana do ano
%V
%w
%W
%x
Data padro
%X
Hora padro
%y
%Y
%z
Diferena do UTC
%Z
Nome do fuso
%%
Smbolo de porcentagem
2013/2
11 / 15
Funo
strftime()
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<time.h>
<locale.h>
2013/2
12 / 15
Diferena de tempo
Para se calcular uma diferena entre dois tempos diferentes (time_t),
podemos usar a funo
difftime().
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char** argv) {
time_t horaInicial = time(NULL);
int i;
int soma = 0;
for(i = 0; i < 20000; i++) {
printf("Resultado parcial da soma: %d\n", soma);
soma += i;
}
printf("Resultado final da soma: %d\n", soma);
time_t horaFinal = time(NULL);
double diferenca = difftime(horaFinal, horaInicial);
printf("A execuo desta funo levou %f segundos.\n", diferenca);
return(EXIT_SUCCESS);
}
Alex Helder (IFB)
2013/2
13 / 15
clock().
Ela conta a quantidade de ciclos do processador desde o incio do
programa.
Se quiser obter a quantidade de segundos, divida pelo valor
CLOCKS_PER_SEC.
CLOCKS_PER_SEC
2013/2
14 / 15
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char** argv) {
int i,j;
int soma = 0;
for(i = 0; i < 100000; i++) {
for(j = 0; j < 100000; j++) {
soma += i*j;
}
}
clock_t ciclos = clock();
int segundos = ciclos/CLOCKS_PER_SEC;
printf("Ciclos: %d.\n", CLOCKS_PER_SEC);
printf("Ciclos: %d.\n", ciclos);
printf("Segundos: %d.\n", segundos);
return(EXIT_SUCCESS);
}
Alex Helder (IFB)
2013/2
15 / 15