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.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <time.h>
00006 #include <string.h>
00007 
00008 #define LONG_CADENA 128
00009 
00010 int main(void)
00011 {
00012    time_t fecha;
00013    struct tm *temp;
00014    struct tm fecha_tm;
00015    char buffer[32];
00016    
00017    /* obtener fecha (time_t) */
00018    if ((fecha = time(NULL)) == (time_t) -1)
00019       return EXIT_FAILURE;
00020    
00021    /* obtener fecha (struct tm) */
00022    temp = localtime(&fecha);
00023    memcpy(&fecha_tm, temp, sizeof fecha_tm);
00024    
00025    /* imprimir mediante las funciones de la biblioteca time.h */
00026    printf("\nTime as a basic string with ctime(&fecha) = %s", ctime(&fecha));
00027    
00028    strftime(buffer, 32, "%a %b %d %H:%M:%S %Y\n", &fecha_tm);
00029    printf("Time using time.h with &fecha_tm: %s", buffer);
00030    
00031    printf("Time as a estructure  localtime(&fecha):\n");
00032    strftime(buffer,32,"%a %d %b %Y",localtime(&fecha));//Thu 20 Aug 2015
00033    printf("Fecha: Guayaquil %s\n", buffer);
00034    strftime(buffer,32,"%H:%M:%S, %p",localtime(&fecha));//Thu 20 Aug 2015
00035    printf("Hora:  %s\n\r", buffer);
00036    
00037    /* imprimir manualmente */
00038    printf("Estructure tm:\n\r");
00039    printf("tm_sec: %d\n\r",fecha_tm.tm_sec);
00040    printf("tm_min: %d\n\r",fecha_tm.tm_min);
00041    printf("tm_hour: %d\n\r",fecha_tm.tm_hour);
00042    printf("tm_mday: %d\n\r",fecha_tm.tm_mday);
00043    printf("tm_mon: %d\n\r",fecha_tm.tm_mon);
00044    printf("tm_year: %d\n\r",fecha_tm.tm_year+1900);
00045    printf("tm_wday: %d\n\r",fecha_tm.tm_wday);
00046    printf("tm_yday: %d\n\r",fecha_tm.tm_yday);
00047    printf("tm_isdst: %d\n\r",fecha_tm.tm_isdst);
00048    printf("\n\rExample:\n\r");
00049    printf("Time as ISO-8601:  %d%03d\n\r", fecha_tm.tm_year + 1900,fecha_tm.tm_yday);
00050    
00051    return EXIT_SUCCESS;
00052 }
00053 
00054 /*
00055 
00056 ctime(&fecha):Thu Aug 20 19:58:42 2015
00057 &fecha_tm: Thu Aug 20 19:58:42 2015
00058 Time: 20 19:58:42 2015
00059 Estructura tm:
00060 tm_sec: 42      //Indica los segundos después de un minuto (o sea los segundos xD)
00061 tm_min: 58      //Indica los minutos depués de una hora (los minutos vaya)
00062 tm_hour: 19     //Indica el numero de horas despues de media noche  (lo que es la hora[0,23])
00063 tm_mday: 20     //El dia del mes[1,31]
00064 tm_mon: 7       //Meses que han pasado dede enero [0,11], por lo tanto Diciembre es el mes 11 y enero el mes 0
00065 tm_year: 115    //Años desde 1900, si quieres saber el año actual sumas 1900
00066 tm_wday: 4      //Dia de la semana, desde el domingo [0,6]
00067 tm_yday: 231    //Dias desde el 1 de Enero [0,365]
00068 tm_isdst: -1    //No se xDD, algo de daylight
00069 Ejemplo:Tiempo segun ISO-8601:  2015231
00070 
00071 */