time.h relacionado con formato de hora y fecha es un archivo de cabecera de la biblioteca estándar del lenguaje de programación C que contiene funciones para manipular y formatear la fecha y hora del sistema.
main.cpp@0:bf5629d7a926, 2015-07-21 (annotated)
- Committer:
- apm_litoral
- Date:
- Tue Jul 21 01:35:07 2015 +0000
- Revision:
- 0:bf5629d7a926
ime.h relacionado con formato de hora y fecha es un archivo de cabecera de la biblioteca est?ndar del lenguaje de programaci?n C que contiene funciones para manipular y formatear la fecha y hora del sistema.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
apm_litoral | 0:bf5629d7a926 | 1 | #include "mbed.h" |
apm_litoral | 0:bf5629d7a926 | 2 | |
apm_litoral | 0:bf5629d7a926 | 3 | #include <stdio.h> |
apm_litoral | 0:bf5629d7a926 | 4 | #include <stdlib.h> |
apm_litoral | 0:bf5629d7a926 | 5 | #include <time.h> |
apm_litoral | 0:bf5629d7a926 | 6 | #include <string.h> |
apm_litoral | 0:bf5629d7a926 | 7 | |
apm_litoral | 0:bf5629d7a926 | 8 | #define LONG_CADENA 128 |
apm_litoral | 0:bf5629d7a926 | 9 | |
apm_litoral | 0:bf5629d7a926 | 10 | int main(void) |
apm_litoral | 0:bf5629d7a926 | 11 | { |
apm_litoral | 0:bf5629d7a926 | 12 | time_t fecha; |
apm_litoral | 0:bf5629d7a926 | 13 | struct tm *temp; |
apm_litoral | 0:bf5629d7a926 | 14 | struct tm fecha_tm; |
apm_litoral | 0:bf5629d7a926 | 15 | char buffer[32]; |
apm_litoral | 0:bf5629d7a926 | 16 | |
apm_litoral | 0:bf5629d7a926 | 17 | /* obtener fecha (time_t) */ |
apm_litoral | 0:bf5629d7a926 | 18 | if ((fecha = time(NULL)) == (time_t) -1) |
apm_litoral | 0:bf5629d7a926 | 19 | return EXIT_FAILURE; |
apm_litoral | 0:bf5629d7a926 | 20 | |
apm_litoral | 0:bf5629d7a926 | 21 | /* obtener fecha (struct tm) */ |
apm_litoral | 0:bf5629d7a926 | 22 | temp = localtime(&fecha); |
apm_litoral | 0:bf5629d7a926 | 23 | memcpy(&fecha_tm, temp, sizeof fecha_tm); |
apm_litoral | 0:bf5629d7a926 | 24 | |
apm_litoral | 0:bf5629d7a926 | 25 | /* imprimir mediante las funciones de la biblioteca time.h */ |
apm_litoral | 0:bf5629d7a926 | 26 | printf("\nTime as a basic string with ctime(&fecha) = %s", ctime(&fecha)); |
apm_litoral | 0:bf5629d7a926 | 27 | |
apm_litoral | 0:bf5629d7a926 | 28 | strftime(buffer, 32, "%a %b %d %H:%M:%S %Y\n", &fecha_tm); |
apm_litoral | 0:bf5629d7a926 | 29 | printf("Time using time.h with &fecha_tm: %s", buffer); |
apm_litoral | 0:bf5629d7a926 | 30 | |
apm_litoral | 0:bf5629d7a926 | 31 | printf("Time as a estructure localtime(&fecha):\n"); |
apm_litoral | 0:bf5629d7a926 | 32 | strftime(buffer,32,"%a %d %b %Y",localtime(&fecha));//Thu 20 Aug 2015 |
apm_litoral | 0:bf5629d7a926 | 33 | printf("Fecha: Guayaquil %s\n", buffer); |
apm_litoral | 0:bf5629d7a926 | 34 | strftime(buffer,32,"%H:%M:%S, %p",localtime(&fecha));//Thu 20 Aug 2015 |
apm_litoral | 0:bf5629d7a926 | 35 | printf("Hora: %s\n\r", buffer); |
apm_litoral | 0:bf5629d7a926 | 36 | |
apm_litoral | 0:bf5629d7a926 | 37 | /* imprimir manualmente */ |
apm_litoral | 0:bf5629d7a926 | 38 | printf("Estructure tm:\n\r"); |
apm_litoral | 0:bf5629d7a926 | 39 | printf("tm_sec: %d\n\r",fecha_tm.tm_sec); |
apm_litoral | 0:bf5629d7a926 | 40 | printf("tm_min: %d\n\r",fecha_tm.tm_min); |
apm_litoral | 0:bf5629d7a926 | 41 | printf("tm_hour: %d\n\r",fecha_tm.tm_hour); |
apm_litoral | 0:bf5629d7a926 | 42 | printf("tm_mday: %d\n\r",fecha_tm.tm_mday); |
apm_litoral | 0:bf5629d7a926 | 43 | printf("tm_mon: %d\n\r",fecha_tm.tm_mon); |
apm_litoral | 0:bf5629d7a926 | 44 | printf("tm_year: %d\n\r",fecha_tm.tm_year+1900); |
apm_litoral | 0:bf5629d7a926 | 45 | printf("tm_wday: %d\n\r",fecha_tm.tm_wday); |
apm_litoral | 0:bf5629d7a926 | 46 | printf("tm_yday: %d\n\r",fecha_tm.tm_yday); |
apm_litoral | 0:bf5629d7a926 | 47 | printf("tm_isdst: %d\n\r",fecha_tm.tm_isdst); |
apm_litoral | 0:bf5629d7a926 | 48 | printf("\n\rExample:\n\r"); |
apm_litoral | 0:bf5629d7a926 | 49 | printf("Time as ISO-8601: %d%03d\n\r", fecha_tm.tm_year + 1900,fecha_tm.tm_yday); |
apm_litoral | 0:bf5629d7a926 | 50 | |
apm_litoral | 0:bf5629d7a926 | 51 | return EXIT_SUCCESS; |
apm_litoral | 0:bf5629d7a926 | 52 | } |
apm_litoral | 0:bf5629d7a926 | 53 | |
apm_litoral | 0:bf5629d7a926 | 54 | /* |
apm_litoral | 0:bf5629d7a926 | 55 | |
apm_litoral | 0:bf5629d7a926 | 56 | ctime(&fecha):Thu Aug 20 19:58:42 2015 |
apm_litoral | 0:bf5629d7a926 | 57 | &fecha_tm: Thu Aug 20 19:58:42 2015 |
apm_litoral | 0:bf5629d7a926 | 58 | Time: 20 19:58:42 2015 |
apm_litoral | 0:bf5629d7a926 | 59 | Estructura tm: |
apm_litoral | 0:bf5629d7a926 | 60 | tm_sec: 42 //Indica los segundos después de un minuto (o sea los segundos xD) |
apm_litoral | 0:bf5629d7a926 | 61 | tm_min: 58 //Indica los minutos depués de una hora (los minutos vaya) |
apm_litoral | 0:bf5629d7a926 | 62 | tm_hour: 19 //Indica el numero de horas despues de media noche (lo que es la hora[0,23]) |
apm_litoral | 0:bf5629d7a926 | 63 | tm_mday: 20 //El dia del mes[1,31] |
apm_litoral | 0:bf5629d7a926 | 64 | tm_mon: 7 //Meses que han pasado dede enero [0,11], por lo tanto Diciembre es el mes 11 y enero el mes 0 |
apm_litoral | 0:bf5629d7a926 | 65 | tm_year: 115 //Años desde 1900, si quieres saber el año actual sumas 1900 |
apm_litoral | 0:bf5629d7a926 | 66 | tm_wday: 4 //Dia de la semana, desde el domingo [0,6] |
apm_litoral | 0:bf5629d7a926 | 67 | tm_yday: 231 //Dias desde el 1 de Enero [0,365] |
apm_litoral | 0:bf5629d7a926 | 68 | tm_isdst: -1 //No se xDD, algo de daylight |
apm_litoral | 0:bf5629d7a926 | 69 | Ejemplo:Tiempo segun ISO-8601: 2015231 |
apm_litoral | 0:bf5629d7a926 | 70 | |
apm_litoral | 0:bf5629d7a926 | 71 | */ |