Skip to content

Commit b66dda5

Browse files
committed
#14 - C++
1 parent 928e11d commit b66dda5

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include <iostream>
2+
#include <chrono>
3+
#include <string>
4+
#include <ctime>
5+
6+
/*
7+
* EJERCICIO:
8+
* Crea dos variables utilizando los objetos fecha (date, o semejante) de tu lenguaje:
9+
* - Una primera que represente la fecha (día, mes, año, hora, minuto, segundo) actual.
10+
* - Una segunda que represente tu fecha de nacimiento (te puedes inventar la hora).
11+
* Calcula cuántos años han transcurrido entre ambas fechas.
12+
*
13+
* DIFICULTAD EXTRA (opcional):
14+
* Utilizando la fecha de tu cumpleaños, formatéala y muestra su resultado de
15+
* 10 maneras diferentes. Por ejemplo:
16+
* - Día, mes y año.
17+
* - Hora, minuto y segundo.
18+
* - Día de año.
19+
* - Día de la semana.
20+
* - Nombre del mes.
21+
* (lo que se te ocurra...)
22+
*/
23+
24+
/*************************************************************
25+
********************* ZONA DE FUNCIONES **********************
26+
*************************************************************/
27+
std::string currenTime() {
28+
std::time_t t = std::time(nullptr);
29+
std::tm* now = std::localtime(&t);
30+
31+
char buffer[128];
32+
std::strftime(buffer, std::size(buffer), "%d/%m/%Y %X" ,now);
33+
34+
return buffer;
35+
};
36+
37+
std::string formatDate(const char* fmt, std::tm* date) {
38+
char buffer[80];
39+
std::strftime(buffer, std::size(buffer), fmt, date);
40+
41+
return buffer;
42+
}
43+
44+
// FUNCION PRINCIPAL
45+
int main() {
46+
// La siguiente instruccion imprime la fecha actual a la hora
47+
// de ejecutar el código
48+
std::cout << "Tiempo actual: [ " << currenTime() << " ]\n";
49+
50+
// Creacion de una estructura customDate
51+
struct tm customDate = { };
52+
customDate.tm_year = 2004 - 1900; // Año de mi nacimiento
53+
customDate.tm_mon = 5; // Mes de mi nacimiento: Junio - 05
54+
customDate.tm_mday = 28; // Dia de nacimiento
55+
customDate.tm_hour = 12; // Hora de nacimiento
56+
customDate.tm_min = 59; // Minuto de nacimiento
57+
58+
// Se crea una varlable de tipo buffer para almacenar la fecha ya formateada
59+
char buffer[80];
60+
std::strftime(buffer, std::size(buffer), "%d/%m/%Y %X %P", &customDate);
61+
62+
// De imprime la fecha
63+
std::cout << "Fecha de nacimiento: [ " << buffer << " ]\n";
64+
65+
// Convertir la fecha personalizada a un punto en el tiempo desde el epoch (1 de enero de 1970)
66+
std::time_t customTime = std::mktime(&customDate);
67+
68+
// Crear un objeto de tiempo con la fecha personalizada
69+
std::chrono::system_clock::time_point customPoint = std::chrono::system_clock::from_time_t(customTime);
70+
71+
// Obtener el tiempo actual
72+
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
73+
74+
// Calcular la diferencia entre la fecha personalizada y el tiempo actual
75+
auto difference = std::chrono::duration_cast<std::chrono::hours>(now - customPoint);
76+
77+
// Imprimir la diferencia en días
78+
std::cout << "Diferencia en años: " << (float)difference.count() / 8760.0 << std::endl;
79+
80+
81+
/**************************************************************************************
82+
************* IMPRESION DE LA FECHA DE NACIMIENTO EN DISTINTOS FORMATOS ***************
83+
***************************************************************************************/
84+
std::cout << "\n************ FORMATO DE FECHA DE NACIMIENTO **********\n";
85+
std::tm* localTime = std::localtime(&customTime);
86+
std::cout << "Formato 1: " << formatDate("%d/%m/%Y", localTime) << std::endl; // Formato: día/mes/año
87+
std::cout << "Formato 2: " << formatDate("%m/%d/%Y", localTime) << std::endl; // Formato: mes/día/año
88+
std::cout << "Formato 3: " << formatDate("%Y-%m-%d", localTime) << std::endl; // Formato: año-mes-día
89+
std::cout << "Formato 4: " << formatDate("%d %b %Y", localTime) << std::endl; // Formato: día abreviado mes año (ejemplo: 01 Jan 2024)
90+
std::cout << "Formato 5: " << formatDate("%A, %d de %B de %Y", localTime) << std::endl; // Formato: día de semana, día de mes de año (ejemplo: viernes, 01 de enero de 2024)
91+
std::cout << "Formato 6: " << formatDate("%H:%M:%S", localTime) << std::endl; // Formato: hora:minuto:segundo
92+
std::cout << "Formato 7: " << formatDate("%I:%M:%S %p", localTime) << std::endl; // Formato: hora:minuto:segundo AM/PM (ejemplo: 05:30:15 PM)
93+
std::cout << "Formato 8: " << formatDate("%Y-%m-%d %H:%M:%S", localTime) << std::endl; // Formato: año-mes-día hora:minuto:segundo
94+
std::cout << "Formato 9: " << formatDate("%Y%m%d%H%M%S", localTime) << std::endl; // Formato: año mes día hora minuto segundo (ejemplo: 20240101233000)
95+
std::cout << "Formato 10: " << formatDate("%Y-%m-%dT%H:%M:%SZ", localTime) << std::endl; // Formato: año-mes-díaThora:minuto:segundoZ (ejemplo: 2024-01-01T23:30:00Z)
96+
97+
98+
return 0;
99+
}
100+
101+
// std::string currentTime() {
102+
// std::time_t t = std::time(nullptr);
103+
// std::tm* now = std::localtime(&t);
104+
105+
// char buffer[128];
106+
// std::strftime(buffer, std::size(buffer), "%d/%m/%Y %X" ,now);
107+
108+
// return buffer;
109+
// }

0 commit comments

Comments
 (0)