En este capítulo del tutorial de C++ veremos como crear formatos de fecha y hora personalizados a través de la librería ctime y la estructura de datos tm.
Más información: http://www.tutorialspoint.com/cplusplus/cpp_date_time.htm
ctime.cpp
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
/*
struct tm {
int tm_sec; // seconds of minutes from 0 to 61
int tm_min; // minutes of hour from 0 to 59
int tm_hour; // hours of day from 0 to 24
int tm_mday; // day of month from 1 to 31
int tm_mon; // month of year from 0 to 11
int tm_year; // year since 1900
int tm_wday; // days since sunday
int tm_yday; // days since January 1st
int tm_isdst; // hours of daylight savings time
}
*/
/* fecha/hora actual basado en el sistema actual */
time_t now = time(0);
/* Objeto de una estructura tm con fecha/hora local */
tm * time = localtime(&now);
vector<string> dia_semana;
dia_semana.push_back("Domingo");
dia_semana.push_back("Lunes");
dia_semana.push_back("Martes");
dia_semana.push_back("Miercoles");
dia_semana.push_back("Jueves");
dia_semana.push_back("Viernes");
dia_semana.push_back("Sabado");
vector<string> mes;
mes.push_back("Enero");
mes.push_back("Febrero");
mes.push_back("Marzo");
mes.push_back("Abril");
mes.push_back("Mayo");
mes.push_back("Junio");
mes.push_back("Julio");
mes.push_back("Agosto");
mes.push_back("Septiembre");
mes.push_back("Octubre");
mes.push_back("Noviembre");
mes.push_back("Diciembre");
int year = 1900 + time->tm_year;
//Formato=hoy miercoles, 27 de mayo del 2015
cout << "Hoy " << dia_semana[time->tm_wday] << ", ";
cout << time->tm_mday << " de " << mes[time->tm_mon] << " del " << year << endl;
cout << time->tm_hour << ":" << time->tm_min << ":" << time->tm_sec << endl;
system("PAUSE");
return 0;
}
No hay comentarios:
Publicar un comentario