如何获取两个日期之间在Informix中的工作日数 [英] How to get number of working days in Informix between two dates

查看:294
本文介绍了如何获取两个日期之间在Informix中的工作日数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Informix中创建函数来计算两个给定日期之间的工作日数. 我用假期和两个Informix函数创建了表"prazkal":

I need to create function in Informix that calculates number of working days in between two given dates. I have created table "prazkal" with holidays, and two Informix functions:

create function is_holiday(d datetime year to day)
returning boolean;
    define hcnt integer;

    if weekday(d) = 0 or weekday(d) = 6 then
        return 't';
    end if;

    ---code that check if 'd' is marked as holiday in calendar
    select count(*) into hcnt from prazkal where datpra = d;
    if hcnt > 0 then
      return 't';
    end if;

    return 'f';
end function;

create function work_days(start_d DATE, end_d DATE)
returning integer;
define new_d datetime year to day;
define count_days integer;
define i integer;
    let i = 0;

    let count_days = end_d - start_d;
    let new_d = CAST(end_d AS DATETIME YEAR TO DAY);


    while i < count_days
        let new_d = new_d - interval(1) day to day;
        if not is_holiday(new_d) then
            let i = i + 1;
        end if;
    end while


    return i;
end function;

我的两个功能都从该帖子中进行了修改. 函数work_days的结果是两个日期(end_d,start_d)之间的天数,而不是工作天数. 我在哪里出错了?

Both of my functions are modified from this post. Result of function work_days is number of days between two dates (end_d, start_d), and not number of working days. Where did I made error?

推荐答案

您的循环将i递增,直到它是count_days中的数字,因此答案将始终是count_days(计算速度非常慢).您还需要第二个条件new_d > start_d:

Your loop increments i until it is the number in count_days, so the answer will always be count_days (calculated very slowly). You need a second condition new_d > start_d too:

while i < count_days and new_d > start_d
    let new_d = new_d - interval(1) day to day;
    if not is_holiday(new_d) then
        let i = i + 1;
    end if;
end while

>是适当的,因为循环递减然后检查new_d,所以最后一个循环测试start_d.

The > is appropriate since the loop decrements and then checks new_d, so the last cycle tests start_d.

这篇关于如何获取两个日期之间在Informix中的工作日数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆