{
===============================================================================
  Copyright (©) 2002. Rodax Software.
===============================================================================
  Los contenidos de este fichero son propiedad de Rodax Software titular del
  copyright. Este fichero sólo podrá ser copiado, distribuido y utilizado,
  en su totalidad o en parte, con el permiso escrito de Rodax Software, o de
  acuerdo con los términos y condiciones establecidas en el acuerdo/contrato
  bajo el que se suministra.
  -----------------------------------------------------------------------------
  Web:  www.rodax-software.com
===============================================================================
  Fecha primera versión: 01-11-2002
  Versión actual: 1.0.0
  Fecha versión actual: 01-11-2002
===============================================================================
  Modificaciones:

    Fecha         Comentarios
    ---------------------------------------------------------------------------
===============================================================================
}

unit DateFunc;

interface

const
  FECHA_NULA: TDateTime = {-693594} 0;

{ Funciones de fecha y hora }
function EsMes(Mes : string) : Boolean;
function EsDia(Dia : string) : Boolean;
function EsFechaValida(ADate: TDateTime): Boolean; overload;
function EsFechaValida(ADate: string): Boolean; overload;
function DarFormatoInterbaseFecha(ADate: string): string;
function DarDiasPorMes(AYear, AMonth: Integer): Integer;
function DarMes(Mes: Integer): string;
function EsFechaNula(Fecha : TDateTime) : Boolean;
function DarDiaInicioMesStr : string;
function DarDiaInicioMesDat : TDateTime;
function DarDiaInicioAnoStr : string;
function DarDiaInicioAnoDat : TDateTime;

function DarDiaInicioMes(Fecha : string) : string; overload;
function DarDiaFinalMesStr : string;
function DarDiaFinalMesDat : TDateTime;
function DarDiaFinalMes(Fecha : string) : string; overload;

function DarDiaInicioAno(Fecha : string) : string; overload;
function DarDiaFinalAnoStr : string;
function DarDiaFinalAnoDat : TDateTime;
function DarDiaFinalAno(Fecha : string) : string; overload;

function DarAno(Fecha: String): Integer;

implementation

uses
  StrFunc, SysUtils;


function EsMes(Mes : String) : Boolean;
begin
  Result := False;
  if EsCadenaVacia(Mes) then
    Exit;

  if not EsNumerico(Mes) then
    Exit;

  if (StrToInt(Mes) > 12) or (StrToInt(Mes) < 1) then
    Exit;

  Result := True;
end;

function EsDia(Dia : String) : Boolean;
begin
  Result := False;
  if EsCadenaVacia(Dia) then
    Exit;

  if not EsNumerico(Dia) then
    Exit;

  if (StrToInt(Dia) > 31) or (StrToInt(Dia) < 1) then
    Exit;

  Result := True;
end;

function EsFechaValida(ADate: TDateTime): Boolean;
var
  Year, Month, Day: Word;
begin
  try
    DecodeDate(ADate, Year, Month, Day);
    Result := (Year >= 1) and (Year <= 9999) and (Month >= 1) and (Month <= 12) and
    (Day >= 1) and (Day <= DarDiasPorMes(Year, Month));
  except
    Result := False;
  end;
end;

function EsFechaValida(ADate: String): Boolean;
begin
  if EsCadenaVacia(ADate) then
    Result := False
  else begin
    try
      Result := EsFechaValida(StrToDate(ADate));
    except
      Result := False;
    end;
  end;
end;

function DarFormatoInterbaseFecha(ADate: String): String;
begin
  result := FormatDateTime('dd.mm.yyyy', StrToDate(ADate));;
end;

function EsFechaNula(Fecha : TDateTime) : Boolean;
begin
  Result := (Fecha = FECHA_NULA);
end;

function DarDiasPorMes(AYear, AMonth: Integer): Integer;
const
  DaysInMonth: array[1..12] of Integer =
    (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
begin
  Result := DaysInMonth[AMonth];
  if (AMonth = 2) and IsLeapYear(AYear) then Inc(Result); { Años bisiestos }
end;

function DarMes(Mes: Integer): String;
const
  Meses: array[1..12] of String =
    ('Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto',
     'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');
begin
  Result := Meses[Mes];
end;

function DarDiaInicioMesStr : string;
begin
  Result := DarDiaInicioMes(DateToStr(Date));
end;

function DarDiaInicioMesDat : TDateTime;
begin
  Result := StrToDate(DarDiaInicioMes(DateToStr(Date)));
end;

function DarDiaInicioAnoStr : string;
begin
  Result := DarDiaInicioAno(DateToStr(Date));
end;

function DarDiaInicioAnoDat : TDateTime;
begin
  Result := StrToDate(DarDiaInicioAno(DateToStr(Date)));
end;

function DarDiaInicioMes (Fecha : string) : string;
var
  Year, Month, Day : Word;
begin
  DecodeDate (StrToDate(Fecha), Year, Month, Day);
  Result := Format('%.2d/%.2d/%.4d', [1, Month, Year]);
end;

function DarDiaFinalMesStr : string;
begin
  Result := DarDiaFinalMes(DateToStr(Date));
end;

function DarDiaFinalMesDat : TDateTime;
begin
  Result := StrToDate(DarDiaFinalMes(DateToStr(Date)));
end;

function DarDiaFinalMes (Fecha : string) : string;
var
  Year, Month, Day : Word;
begin
  DecodeDate (StrToDate(Fecha), Year, Month, Day);
  Day := DarDiasPorMes(Year, Month);
  Result := Format('%.2d/%.2d/%.4d', [Day, Month, Year]);
end;

function DarDiaInicioAno (Fecha : string) : string;
var
  Year, Month, Day : Word;
begin
  DecodeDate (StrToDate(Fecha), Year, Month, Day);
  Result := Format('%.2d/%.2d/%.4d', [1, 1, Year]);
end;

function DarDiaFinalAnoStr : string;
begin
  Result := DarDiaFinalAno(DateToStr(Date));
end;

function DarDiaFinalAnoDat : TDateTime;
begin
  Result := StrToDate(DarDiaFinalAno(DateToStr(Date)));
end;

function DarDiaFinalAno (Fecha : string) : string;
var
  Year, Month, Day : Word;
begin
  DecodeDate (StrToDate(Fecha), Year, Month, Day);
  Day := DarDiasPorMes(Year, 12);
  Result := Format('%.2d/%.2d/%.4d', [Day, 12, Year]);
end;

function DarAno(Fecha: String): Integer;
var
  Year, Month, Day : Word;
begin
  DecodeDate (StrToDate(Fecha), Year, Month, Day);
  Result := Year;
end;

end.
