/*
   This function returns the HMS time elements corresponding
   to a given fraction of a day argument string.

   Output can have the formats:
   01:20:30.123  or  01h 20m 30.123s
*/

   function Day_Frac_to_HMS ($FracDayStr, $Decimals=3, $OutMode=':')
{
   $Q = 32;
   $q = trim($Decimals);

   $OutMode = StrToUpper(substr(trim($OutMode),0,1));
   $OutMode = ($OutMode == 'H')? 'hms':':';

// Remove any numerical (- +) sign
// which will be restored on exit.
   $NumSign = '';
   if ($FracDayStr < 0)
      {
       $FracDayStr = substr($FracDayStr,1,StrLen($FracDayStr));
       $NumSign = '-';
      }
   if (substr($FracDayStr,0,1) == '+')
      {
       $FracDayStr = substr($FracDayStr,1,StrLen($FracDayStr));
       $NumSign = '+';
      }

   $days = trim($FracDayStr);
   $dd   = bcAdd($days, '0');

   $hours = bcMul('24', bcSub($days, $dd, $Q), $Q);
   $HH    = bcAdd($hours, '0');
   $HH    = ($HH < 10)? "0$HH" : $HH;

   $minutes = bcMul('60', bcSub($hours, $HH, $Q),$Q);
   $mm = bcAdd($minutes, '0');
   $mm = ($mm < 10)? "0$mm" : $mm;

// ----------------------------------------
// Round the seconds value to (q) decimals.
   $seconds = bcMul('60', bcSub($minutes, $mm, $Q),$Q);

   $ss = bcAdd($seconds, '0.' . Str_Repeat('0', $q) . '5', $q);
   $ss = RTrim(RTrim($ss, '0'), '.');
   $ss = ($ss < 10)? "0$ss" : $ss;

// Round seconds to (q) decimals.
   $ss = SPrintf("%0.$q"."f", $ss);
   $ss = ($ss < 10)? "0$ss" : $ss;

// ---------------------------------------------
// PATCH TO TRY AND FIX THAT BLASTED 60S GLITCH.

   if (IntVal($ss) == 60)
   {
    $ss = '00';
    $mm = IntVal($mm) + 1;
    $mm = ($mm < 10)? "0$mm" : $mm;
   }

   if (IntVal($mm) == 60)
   {
    $mm = '00';
    $HH = IntVal($HH) + 1;
    $HH = ($HH < 10)? "0$HH" : $HH;
   }

   if (IntVal($HH) == 24)
   {
    $HH = '00';
    $dd = IntVal($dd) + 1;
    $dd = ($dd < 10)? "0$dd" : $dd;
   }
    $dd = ($dd == 0)? '' : "$ddd  ";

// Return time string according to (OutMode) switch.
   return ($OutMode == ':')? "$NumSign$dd$HH:$mm:$ss"
                           : "$NumSign$dd$HH".'h '."$mm".'m '."$ss".'s';

} // END OF  Day_Frac_to_HMS (...)