这可能完全准确,也可能不完全准确,但我想我明白了。输入非常受欢迎。希望有一天这会对某人有所帮助!
#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;