C/Visual C - convertir un string a time_t

 
Vista:
sin imagen de perfil

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.

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
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

convertir un string a time_t

Publicado por Tom (619 intervenciones) el 22/10/2016 20:24:59
Quizás hayas compilado mal. Esto funciona:
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#define _XOPEN_SOURCE 999
/* Esta version de gcc tiene un bug en los .h, ignorar el warning de strptime() */
#include <time.h>
 
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 buffer[80];
	time_t a_time = string_to_date("20/08/1992");
 
	strftime(buffer, 80, "%d/%m/%Y", localtime(&a_time));
	printf("fecha = %s\n", buffer);
 
	return EXIT_SUCCESS;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

convertir un string a time_t

Publicado por Diego (150 intervenciones) el 22/10/2016 21:12:55
Gracias. Me funcionó.
PEro pasa algo que no entiendo y es lo siguiente:
compilé con:
1
gcc -Wall -o prueba.exe prueba.c

ejecuto el programa con:
1
./prueba.exe
Y al mostrarme la fecha en pantalla me muesta la fecha correcta o la fecha: 05/08/93830

¿por que varia el resultado cada vez que ejecuto el programa?

Consola:
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
acus@acus-desktop:~/Documentos$ gcc -Wall -o prueba.exe prueba.c
prueba.c: In function ‘string_to_date’:
prueba.c:12:2: warning: implicit declaration of function ‘strptime’ [-Wimplicit-function-declaration]
  strptime(s, "%d/%m/%Y", &a_date);
  ^
acus@acus-desktop:~/Documentos$ ./prueba.exe
fecha = 20/08/1992
acus@acus-desktop:~/Documentos$ ./prueba.exe
fecha = 20/08/1992
acus@acus-desktop:~/Documentos$ ./prueba.exe
fecha = 20/08/1992
acus@acus-desktop:~/Documentos$ ./prueba.exe
fecha = 05/08/93830
acus@acus-desktop:~/Documentos$ ./prueba.exe
fecha = 05/08/93830
acus@acus-desktop:~/Documentos$ ./prueba.exe
fecha = 20/08/1992
acus@acus-desktop:~/Documentos$ ./prueba.exe
fecha = 20/08/1992
acus@acus-desktop:~/Documentos$ ./prueba.exe
fecha = 05/08/93830
acus@acus-desktop:~/Documentos$ ./prueba.exe
fecha = 05/08/93830
acus@acus-desktop:~/Documentos$ ./prueba.exe
fecha = 20/08/1992
acus@acus-desktop:~/Documentos$ ./prueba.exe
fecha = 20/08/1992
acus@acus-desktop:~/Documentos$ ./prueba.exe
fecha = 05/08/93830
acus@acus-desktop:~/Documentos$
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

convertir un string a time_t

Publicado por Tom (619 intervenciones) el 23/10/2016 10:47:09
Pues no lo sé, mi ejemplo en mi entorno no falla nunca ...
De todos modos, te recomiendo que, tal y como dice la página man de strptime, inicialices a 0 la struct tm que se usa.
Copio y pego del man:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
 
int
main(void)
{
   struct tm tm;
   char buf[255];
 
   memset(&tm, 0, sizeof(struct tm));
   strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
   strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
   puts(buf);
   exit(EXIT_SUCCESS);
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

convertir un string a time_t

Publicado por Diego (150 intervenciones) el 23/10/2016 16:40:54
Gracias, justo hace 15 minutos descubrí que tenia que hacer memset con '\0' para que simpre me arroje la fecha correcta.
Ahora el programa funciona correctamente todas las veces que se ejecuta.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar