tengo una duda, hay alguna funcion que me convierta una fecha ingresada del tipo 12/12/2012 en dias transcurridos desde enero ejm: 365 ?
Puedes utilizar un objeto de tipo "struct tm" (definición en <time.h (http://web.archive.org/web/20060901132234/http://www-ccs.ucsd.edu/c/time.html)>) asignando manualmente los valores para el día, mes y año. A continuación ajustas los campos mediante la función mktime (prototipo en el mismo encabezado). Después de eso basta con utilizar el campo "tm_yday" de la estructura.
Un saludo
hajajaja, bueno, mas o menos entendi, no he usado mucho la libreria time.h pero tengo esto:
typedef struct
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}tm;
luego se supene que ingrese los datos manualemnte algo asi?
int main()
{
tm *fecha;
printf("ingrese dia\n");
scanf("%d",&fecha->tm_mday);
printf("ingrese mes");
scanf("%d",&fecha->tm_mon);
printf("ingrese año");
scanf("%d",&fecha->tm_year);
}
y luego que?
no se, estoy perdido XD
Cita de: J.cE en 21 Febrero 2014, 20:04 PMno he usado mucho la libreria time.h pero tengo esto:
typedef struct
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}tm;
No lo uses, en su lugar incluyes <time.h> y utilizas el tipo "struct tm". El porque se indica en la referencia:
Citarstruct tm contains members that describe various properties of the calendar time. The members shown above can occur in any order, interspersed with additional members.
Cita de: J.cE en 21 Febrero 2014, 20:04 PMy luego que?
Llamas a la funcion mktime e imprimes el valor del campo "tm_yday". Por ejemplo:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
struct tm d = {0};
puts("Introduce la fecha (aaaa mm dd):");
if (scanf("%d %d %d", &d.tm_year, &d.tm_mon, &d.tm_mday) != 3)
return EXIT_FAILURE;
d.tm_year -= 1900; /* 0 == 1900, 100 == 2000, etc. */
d.tm_mon--; /* 0 == Enero, 1 == Febrero, etc. */
if (mktime(&d) == (time_t) -1){
puts("Fecha no valida");
return EXIT_FAILURE;
}
printf("%4d/%02d/%02d ==> Dias transcurridos: %d\n",
d.tm_year + 1900, d.tm_mon + 1, d.tm_mday, d.tm_yday);
return EXIT_SUCCESS;
}
Un saludo