2

我的问题:我需要使用脚本以HH:MI:SS格式获取两个日期(时间)之间的差异。例如,如果我有两次:

2012/08/24 13:04:23
2012/08/24 14:15:32

我的结果是:

1:11:09

要求:我是使用 SAP 的 BusinessObjects 数据服务设计器编写的,所以我的脚本必须满足他们的语法规则(点击这里查看长 pdf 文档...如果您不熟悉 SAP 的语法,只知道它真的很有限。 ..)。


我试过的:目前我有一个函数(见下文),它将为用户提供一个他们可以在SQL函数中使用的 SQL 语句,这将为他们提供他们正在寻找的东西,但我想让这个数据存储不可知.

这是我当前的功能(不是我真正想要的):

#Use this with a SQL function. For example: SQL('DSS_USER', GetTimeDifferenceSQL($time_1, $time_2));

return '
with seconds as (
  select 24*60*60*(to_date(\'[$time_1]\', \'YYYY.MM.DD hh24:mi:ss\')
                    - to_date(\'[$time_2]\', \'YYYY.MM.DD hh24:mi:ss\')) seconds_diff 
  from dual
)
select to_char(trunc(sum(seconds_diff)/3600), \'FM999999990\')  || \':\' || to_char(trunc(mod(sum(seconds_diff),3600)/60), \'FM00\') || \':\' || to_char(mod(sum(seconds_diff),60), \'FM00\')
from seconds
';

这不起作用,因为我希望它独立于数据存储并单独在脚本中计算。

我还尝试将日期转换为十进制数字,然后JED_Time(int)在它们上使用该函数,这很有效,除了十进制是以 10 为基数而时间是以任何为基数......所以这行不通。


我的拖延:我很难接受时间并没有真正确定的基础这一事实......任何帮助都会非常棒!谢谢!

4

2 回答 2

1

更简单的方法是使用数据类型 Interval。

要获取两个日期时间之间的秒数:

interval_to_char( $dtFrom - $dtTo , 'SS');

获取一个区间并返回该区间的字符表示。

interval_to_char( [in] InputInterval As interval, [in] FormatString As varchar ) As varchar

此函数还可用于获取分钟和小时。

于 2013-04-23T10:40:01.860 回答
0

这可能完全准确,也可能不完全准确,但我想我明白了。输入非常受欢迎。希望有一天这会对某人有所帮助!

#Make sure that when we're finding the difference, we always take the lesser date from the greater date. We'll negate it at the end.
if ($time_1 < $time_2)
begin
  $temp_time = $time_1;
  $time_1 = $time_2;
  $time_2 = $temp_time;
end

#Get all value differences from the two times
$nanoseconds = to_decimal(to_char($time_1, 'FF'), '.', null, 0) - to_decimal(to_char($time_2, 'FF'), '.', null, 0);
$seconds = to_decimal(to_char($time_1, 'SS'), '.', null, 0) - to_decimal(to_char($time_2, 'SS'), '.', null, 0);
$minutes = to_decimal(to_char($time_1, 'MI'), '.', null, 0) - to_decimal(to_char($time_2, 'MI'), '.', null, 0);
$hours = to_decimal(to_char($time_1, 'HH24'), '.', null, 0) - to_decimal(to_char($time_2, 'HH24'), '.', null, 0);
$days = interval_to_char($time_1 - $time_2, 'D');

#fix nanoseconds
if ($nanoseconds >= 1000000000)
begin
  $seconds = $seconds + 1;
  $nanoseconds = $nanoseconds - 1000000000;
end

if ($nanoseconds < 0)
begin
  $seconds = $seconds -1;
  $nanoseconds = $nanoseconds + 1000000000;
end

#fix seconds
if ($seconds >= 60)
begin
  $minutes = $minutes + 1;
  $seconds = $seconds - 60;
end

if ($seconds < 0)
begin
  $minutes = $minutes -1;
  $seconds = $seconds + 60;
end

#fix minutes
if ($minutes >= 60)
begin
  $hours = $hours + 1;
  $minutes = $minutes - 60;
end

if ($minutes < 0)
begin
  $hours = $hours -1;
  $minutes = $minutes + 60;
end

#fix hours
if ($hours >= 24)
begin
  $days = $days + 1;
  $hours = $hours - 24;
end

if ($hours < 0)
begin
  $days = $days - 1;
  $hours = $hours + 24;
end

#fix days
if (trunc($days/365, 0) >= 1)
begin
  $years = trunc($days/365, 0);
  $days = $days - ($years * 365);
end

if (round($days/7, 0) > 0)
begin
  $weeks = round($days/7, 0);
  $days = $days - ($weeks * 7);
end

$ret = '';

if ($years > 0)
begin
  $ret = $years||' year'||ifthenelse($years = 1, '', 's')||' ';
end

if ($weeks > 0)
begin
  $ret = $ret||$weeks||' week'||ifthenelse($weeks = 1, '', 's')||' ';
end

if ($days > 0)
begin
  $ret = $ret||$days||' day'||ifthenelse($days = 1, '', 's')||' ';
end

$ret = $ret||$hours||':'||lpad($minutes, 2, '0')||':'||lpad($seconds, 2, '0')||':'||lpad(round($nanoseconds/1000000, 0), 3, '0');

#Negate it if the parameter values were swapped at the beginning (using $temp_time)
if ($temp_time is not null)
begin
  $ret = '-'||$ret;
end

return $ret;
于 2012-08-20T21:05:23.147 回答