【问题标题】:Start Date Calculated Given End Date and Working Hours PL/SQL给定结束日期和工作时间 PL/SQL 计算的开始日期
【发布时间】:2012-07-20 12:58:28
【问题描述】:

使用 Oracle PL/SQL 我需要使用截止日期和完成任务所需的工作时间来计算任务的开始日期。

鉴于截止日期为 2012 年 7 月 24 日 17:00,完成任务所需的时间为 20 小时,并使用上午 8 点至下午 5 点的工作时间(1 小时午餐 - 所以每天最多 8 小时)我需要计算在 PL/SQL 中显示开始日期/时间是什么...应该在 2012 年 7 月 22 日 13:00 出现。

【问题讨论】:

  • 我们需要知道午餐时间。星期几重要吗(仅在周一至周五工作)?另外,你试过什么?
  • @AdamHawkes 是的,仅限周一至周五。午餐时间各不相同,但这并不重要,只是每天的工作不超过 8 小时。

标签: sql oracle datetime plsql


【解决方案1】:

以下代码可以作为起点:

function task_start_date(
  p_due_date date,
  p_working_hours number
) return date
is
  l_start_date date;
begin
  -- Subtract full days
  l_start_date := p_due_date - trunc(p_working_hours / 8);
  -- Subtract remaining hours
  l_start_date := l_start_date - mod(p_working_hours, 8) / 24;
  -- Fix date if the due date is before 8AM
  if to_number(to_char(l_start_date, 'HH24')) < 8 then
    l_start_date := l_start_date - 15 / 24;
  end if;
  return l_start_date;
end task_start_date;

请注意,该函数不会始终考虑午餐时间。您需要准确定义午餐时间并相应地调整函数。

【讨论】:

  • 你确定没有简单的单线? :) 我不太关心午餐时间,而且这段代码似乎工作得很好(需要进一步测试)......但如果它在上午 8 点之前,我真的不明白修复日期的代码。如果它最终在早上 8 点之前开始,它会推迟一天吗?我不明白 15/24 应该做什么。
  • 假设截止日期是 7 月 24 日 10:00,需要 11 个小时。然后我先减去一天(8 小时),然后再减去 3 小时。结果是 7 月 23 日 7:00,也就是当天工作开始之前。所以我又减去了 15 个小时(从下午 5 点到第二天早上 8 点的时间)。结果是 7 月 22 日 16:00。
  • 啊,我现在明白了。所以我可以添加更多 if 语句来确定它是在星期六还是星期日,然后减去 48/24 或 24/24 小时并将开始日期推回星期五,对吗?
猜你喜欢
  • 2012-04-06
  • 2011-03-27
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
相关资源
最近更新 更多