{$mode objfpc}{$H-}{$R+}{$T+}{$Q+}{$V+}{$D+}{$X-}{$warnings on}
program fecha_valida_v02;

// Mejora la versión v01 porque dia_ok() se descompone en una
// nueva función, dias_en_mes()


function anyo_ok(anyo: integer): boolean;
const
	MinAnyo = 1900;
	MaxAnyo = 2123;
begin
	result := (anyo >= MinAnyo) and (anyo <= MaxAnyo);
end;

function mes_ok(mes: integer): boolean;
const
	Ene = 1;
	Dic = 12;
begin
	result := (mes >= Ene) and (mes <= Dic);
end;

function es_bisiesto(anyo: integer): boolean;
begin
	result := (Anyo mod 4 = 0) and (Anyo mod 100 <> 0) or
		(Anyo mod 400 = 0);
end;

function dias_en_mes(anyio, mes: integer): integer;
begin
	case mes of
	1, 3, 5, 7, 8, 10, 12:
		result := 31;   // Este ';' es necesario
	2:
   		if es_bisiesto(anyio) then 
   			result := 29
   		else 
   			result := 28;
   	otherwise
   		result := 30;
	end;
end;

function dia_ok(anyio, mes, dia: integer): boolean;
begin
	result := (dia >= 1) and (dia <= dias_en_mes(anyio, mes));
end;

function fecha_ok(anyo, mes, dia: integer): boolean;
begin
	result := anyo_ok(anyo) and mes_ok(mes) and dia_ok(anyo, mes, dia);
end;

const
	Anyo = 2018;
	Mes = 11;
	Dia = 31;

begin
	writeln(fecha_ok(Anyo, Mes, Dia));
end.


