本文主要是介绍postgresql 实现计算日期间隔排除周末节假日方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前置条件:需要维护一张节假日日期表。例如创建holiday表保存当年假期日期
CREATE TABLE `holiday` (`id` BIGINT(10) ZEROFILL NOT NULL DEFAULT 0,`day` TIMESTAMP NULL DEFAULT NULL,PRIMARY KEY (`id`)
)
COMMENT='假期表'
COLLATE='utf8mb4_0900_ai_ci'
;
返回日期为xx日xx时xx分格式,可以在此基础上调整格式
-- FUNCTION: public.get_timedelay(timestamp with time zone, timestamp with time zone)-- DROP FUNCTION IF EXISTS public.get_timedelay(timestamp with time zone, timestamp with time zone);CREATE OR REPLACE FUNCTION public.get_timedelay(starttime timestamp with time zone,endtime timestamp with time zone)RETURNS textLANGUAGE 'plpgsql'COST 100VOLATILE PARALLEL UNSAFE
AS $BODY$
DECLAREv_return varchar;--返回间隔时间 xx日xx时xx分v_minute integer;--间隔分钟v_hour integer;v_temp_minute integer;v_temp_hour integer;v_day integer; --间隔天数v_all numeric;v_counter integer;v_end_weekend integer;v_weekend integer;--周一_周日 1_6_0v_holiday numeric;--匹配节假日天数v_is_weekend boolean; --是否周末v_is_holiday boolean; --是否节假日
BEGIN--计算时间间隔天数select ceil(DATE_PART('epoch', endtime::timestamp - starttime::TIMESTAMP)/60/60/24) into v_all;--减去周末、节假日v_end_weekend := cast(EXTRACT(DOW FROM (endtime)) as int);v_day := 0;v_hour := 0;v_minute := 0;v_counter := 0;while v_counter <= v_all loopv_temp_minute := 0;v_temp_hour := 0;v_is_weekend := FALSE;v_is_holiday := FALSE;--判断该日期为周几v_weekend := v_end_weekend-v_counter%7;if v_weekend < 0 thenv_weekend := v_weekend + 7;end if;if v_weekend = 6 or v_weekend = 0 thenv_is_weekend := true;end if;--判断该日期是否为节假日--日期表SELECT COUNT(*) FROM holiday WHERE Date(endtime) - DATE(day) - v_counter = 0 INTO v_holiday;if v_is_weekend = false and v_holiday > 0 thenv_is_holiday := true;end if;--累计时长if v_is_weekend = false AND v_is_holiday = false thenif v_counter = 0 thenv_minute := cast(date_part('minute', endtime::TIMESTAMP) as int);v_hour := cast(date_part('hour', endtime::TIMESTAMP) as int);elseif v_counter = v_all thenv_temp_minute := 60 - cast(date_part('minute', endtime::TIMESTAMP) as int);v_temp_hour := 23 - cast(date_part('hour', endtime::TIMESTAMP) as int);v_minute := (v_minute+v_temp_minute) % 60;v_temp_hour := v_temp_hour+FLOOR((v_minute+v_temp_minute)/60);v_hour := (v_hour+v_temp_hour) % 24;v_day := v_day + FLOOR((v_hour+v_temp_hour) / 24);elsev_day := v_day + 1;end if;end if;v_counter:= v_counter+1;end loop;--处理返回日期v_return := '';--返回日、时、分方案if v_day > 0 thenv_return := concat(v_return, v_day,'日');end if;if v_hour > 0 thenv_return := concat(v_return, v_hour,'时');end if;if v_minute > 0 thenv_return := concat(v_return,v_minute,'分');end if;RETURN v_return;EXCEPTION WHEN OTHERS THENRETURN SQLERRM;END;
$BODY$;ALTER FUNCTION public.get_timedelay(timestamp with time zone, timestamp with time zone)OWNER TO postgres;
这篇关于postgresql 实现计算日期间隔排除周末节假日方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!