
convertir un string a time_t
Publicado por Diego (150 intervenciones) el 22/10/2016 19:58:37
Buenas tardes, primeramente realicé una conversión de string a time_t dentro del main y funcionó correctamente.
Pero al intentar hacer una función que haga lo que hago en el main esta función no funciona y siempre el mktime me retorna: 15826838401 fecha = 13/07/2471.
¿Por qué falla mi codigo?
// Quitar /* */ para usar el main con la funcion string_to_date; y bloquear el segundo main.
Pd: todo lo hago usando linux ubuntu.
Pero al intentar hacer una función que haga lo que hago en el main esta función no funciona y siempre el mktime me retorna: 15826838401 fecha = 13/07/2471.
¿Por qué falla mi codigo?
// Quitar /* */ para usar el main con la funcion string_to_date; y bloquear el segundo main.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#define _XOPEN_SOURCE /* requerido por glibc2 */
#define _GNU_SOURCE
#define __USE_BSD
#include <time.h>
/*
time_t string_to_date(char *s);
int main()
{
char *s;
char buffer[80];
time_t a_time;
s = malloc(sizeof(char) * 11);
s[10] = '\0';
strcpy(s, "20/08/1992");
a_time = string_to_date(s);
strftime(buffer, 80, "%d/%m/%Y", localtime(&a_time));
printf("fecha = %s\n",buffer);
return EXIT_SUCCESS;
}
time_t string_to_date(char *s)
{
struct tm a_date;
strptime(s, "%d/%m/%Y", &a_date);
return mktime(&a_date);
}
*/
int main(){
char *s;
char buffer[80];
struct tm a_date;
time_t a_time;
s = malloc(sizeof(char) * 11);
s[10] = '\0';
strcpy(s, "20/08/1992");
strptime(s, "%d/%m/%Y", &a_date);
a_time = mktime(&a_date);
strftime(buffer, 80, "%d/%m/%Y", localtime(&a_time));
printf("fecha = %s\n",buffer);
return EXIT_SUCCESS;
}
Pd: todo lo hago usando linux ubuntu.
Valora esta pregunta


0