仅供交流,欢迎指正
输出结果展示:

以下为函数的sql:
CREATE OR REPLACE FUNCTION public.my_calendar(year integer, month integer, day integer)
RETURNS character varying
LANGUAGE plpgsql
AS $function$
declare
days integer;–current month have days
all_days integer default 0;–from 1900 to current month have days
flag_count integer;–current day is ‘Mon Tue Wed Thu Fri Sat Sun’?
result text default ‘’;–return result
begin
–count all_days
–count 1900 to current year all_days
for years in 1900…year-1 loop
if (years%4 = 0 and years%100 != 0) or years%400 = 0 then
all_days := all_days+366;
else
all_days := all_days+365;
end if;
end loop;
–count current year 1 to month all_days
for months in 1…month-1 loop
if months in (1,3,5,7,8,10,12) then
days := 31;
elseif months = 2 then
if (year%4 = 0 and year%100 != 0) or year%400 = 0 then
days := 29;
else
days :=28;
end if;
else
days :=30;
end if;
all_days := all_days+days;
end loop;
–result format control
flag_count := all_days%7;
result := result||chr(10)||‘Mon Tue Wed Thu Fri Sat Sun’||chr(10);
for i in 1…flag_count loop
result := result||’ ';
end loop;
–current month have days
if month in (1,3,5,7,8,10,12) then
days := 31;
elseif month = 2 then
if (year%4 = 0 and year%100 != 0) or year%400 = 0 then
days := 29;
else
days :=28;
end if;
else
days :=30;
end if;
for i in 1…days loop
if (i+flag_count)%7 = 0 then
if i = day then
–current day add “()”
result := result||’(’||i::varchar||’)’||’ ‘||chr(10);
else
result := result||lpad(i::varchar,3,’ ‘)||’ ‘||chr(10);
end if;
else
if i = day then
–current day add “()”
result := result||’(’||i::varchar||’)’||’ ‘;
else
result := result||lpad(i::varchar,3,’ ‘)||’ ';
end if;
end if;
end loop;
return result;
end;
$function$;




