Apostar y ganar

Click Now!

sábado, 1 de junio de 2013

Codigo para leer o manejar fecha y hora todocodigo


/* Esta es una biblioteca que permite manejar de forma adecuada fechas y horas */

#include <dos.h>

#define dias_por_400_anios   (365*400+100-3)
#define es_anio_bisiesto(y)  ((((y)%400)==0)||((((y)%4)==0)&&(((y)%100)!=0)))


enum bool
{
    false = 0,
    true = 1
};


enum Mes
{
Enero = 1,
Febrero = 2,
Marzo = 3,
Abril = 4,
Mayo = 5,
Junio = 6,
Julio = 7,
Agosto = 8,
Septiembre = 9,
Octubre = 10,
Noviembre = 11,
Diciembre = 12
};

enum DiaSemana
{
Domingo = 1,
Lunes = 2,
Martes = 3,
Miercoles = 4,
Jueves = 5,
Viernes = 6,
Sabado = 7
};


char *nombre_mes(int numero_mes)
{
     char *mes;

switch(numero_mes)
     {
 case 1:        mes="Enero";     break;
 case 2:     mes="Febrero";     break;
 case 3:     mes="Marzo";     break;
 case 4:     mes="Abril";     break;
 case 5:     mes="Mayo";     break;
 case 6:     mes="Junio";     break;
 case 7:     mes="Julio";     break;
 case 8:     mes="Agosto";     break;
 case 9: mes="Septiembre"; break;
 case 10:     mes="Octubre";     break;
 case 11: mes="Noviembre"; break;
 case 12: mes="Diciembre"; break;
     }

     return mes;
}


char *nombre_dia(DiaSemana dia_semana)
{
     char *dia;

     switch(dia_semana)
     {
           case Domingo: dia="Domingo";    break;
    case Lunes:     dia="Lunes"; break;
           case Martes:     dia="Martes"; break;
           case Miercoles: dia="Miercoles"; break;
           case Jueves:     dia="Jueves"; break;
           case Viernes: dia="Viernes";    break;
           case Sabado:      dia="Sabado"; break;
      }

      return dia;
}

struct Fecha
{
    int y, m, d;
};


struct Tiempo
{
    int h, m, s, cs;
};


long dias_pasados_400_anios(int y)
{
    return ( y > 0 ) ? 365 * y + (y-1) / 4 - (y-1) / 100 + 1 : 0;
}


long dias_pasados_mes (int y, int m)
{
    const int dias[12] = { 0, 31, 31+28, 59+31, 90+30, 120+31, 151+30, 181+31,
    212+31, 243+30, 273+31, 304+30 /*, 334+31 = 365*/ };

long resultado;

    resultado = dias[m-1];

    if ( m > Febrero && es_anio_bisiesto ( y ) )
        resultado++;

    return resultado;
}

long dias_pasados_fecha (Fecha fecha)
{
    long dias;
    dias = (fecha.y/400)*dias_por_400_anios + dias_pasados_400_anios ( fecha.y % 400 )
+ dias_pasados_mes ( fecha.y, fecha.m ) + fecha.d;

    return dias;
}

long diferencia_dias (Fecha fechaInicial, Fecha fechaFinal)
{
    return dias_pasados_fecha(fechaFinal) - dias_pasados_fecha(fechaInicial);
}

DiaSemana dia_de_la_semana(Fecha fecha)
{
long resultado;

resultado = dias_pasados_400_anios ( fecha.y % 400 ) + dias_pasados_mes ( fecha.y, fecha.m ) + fecha.d;

/* El primer dia de la semana de cada 400 anios es viernes */
resultado = (Viernes+resultado) % 7;

/* Si es 0, entonces es sabado */
if (resultado==0)
  resultado = 7;

return (DiaSemana)resultado;
}

int dias_en_el_mes (int y, int m)
{
    const int dias[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int resultado;

resultado = dias[m-1];

if ( m == Febrero && es_anio_bisiesto ( y ) )
resultado++;

return resultado;
}

int dias_en_el_anio (int y)
{
return (es_anio_bisiesto ( y ) ) ? 366 : 365;
}

void sumar_dias_fecha (int dias, Fecha *fecha )
{
    for(int i=1;i<=dias;i++)
    {
  fecha->d++;

        if(fecha->d>dias_en_el_mes(fecha->y, fecha->m))
        {
           fecha->d=1;
           fecha->m++;
        }
        if(fecha->m>12)
        {
           fecha->m=1;
  fecha->y++;
        }
    }
}

long centesimas_pasados_tiempo (int h, int m, int s, int cs)
{
return h*(60*60*100) + m*(60*100) + s*100 + cs;
}

long diferencia_centesimas (Tiempo tiempoInicial, Tiempo tiempoFinal)
{
    return centesimas_pasados_tiempo ( tiempoInicial.h, tiempoInicial.m, tiempoInicial.s, tiempoInicial.cs )
- centesimas_pasados_tiempo ( tiempoFinal.h, tiempoFinal.m, tiempoFinal.s, tiempoFinal.cs );
}

void sumar_centesimas_tiempo (long centesimas, Tiempo *tiempo )
{
    tiempo->h = centesimas / (60*60*100);
centesimas %= (60*60*100);
tiempo->m = centesimas / (60*1000);
centesimas %= (60*100);
tiempo->s = centesimas / 100;
tiempo->cs = centesimas % 100;
}

bool es_fecha_valida ( Fecha fecha )
{
if ( fecha.m < Enero || fecha.m > Diciembre )
return false;

if ( fecha.d < 0 || fecha.d >= dias_en_el_mes ( fecha.y, fecha.m ) )
return false;

return true;
}

bool es_tiempo_valido ( Tiempo tiempo )
{
    if ( tiempo.h < 0 || tiempo.h >= 24 )
return false;

if ( tiempo.m < 0 || tiempo.m >= 60 )
return false;

if ( tiempo.s < 0 || tiempo.s >= 60 )
return false;
if ( tiempo.cs < 0 || tiempo.cs >= 100 )
return false;

return true;
}


void leer_fecha_tiempo_local (Fecha *fecha, Tiempo *tiempo )
{
struct date d;
struct time t;

getdate(&d);
fecha->y = d.da_year;
fecha->m = d.da_mon;
fecha->d = d.da_day;

gettime(&t);
tiempo->h = t.ti_hour;
tiempo->m = t.ti_min;
tiempo->s = t.ti_sec;
tiempo->cs = t.ti_hund;
}

char *fecha_formateada (Fecha fecha)
{
char *dia=nombre_dia(dia_de_la_semana(fecha));
char *mes=nombre_mes(fecha.m);

char f[100];
sprintf(f, "%s, %02d de %s del %d", dia, fecha.d, mes, fecha.y);

return f;
}

char *tiempo_formateado (Tiempo tiempo)
{
char t[50];

sprintf(t, "%02d:%02d:%02d %02d", tiempo.h, tiempo.m, tiempo.s, tiempo.cs);
return t;
}

No hay comentarios:

Publicar un comentario