function phutil_units()

in src/utils/utils.php [1127:1260]


function phutil_units($description) {
  $matches = null;
  if (!preg_match('/^(\d+) (\w+) in (\w+)$/', $description, $matches)) {
    throw new InvalidArgumentException(
      pht(
        'Unable to parse unit specification (expected a specification in the '.
        'form "%s"): %s',
        '5 days in seconds',
        $description));
  }

  $quantity = (int)$matches[1];
  $src_unit = $matches[2];
  $dst_unit = $matches[3];

  $is_divisor = false;

  switch ($dst_unit) {
    case 'seconds':
      switch ($src_unit) {
        case 'second':
        case 'seconds':
          $factor = 1;
          break;
        case 'minute':
        case 'minutes':
          $factor = 60;
          break;
        case 'hour':
        case 'hours':
          $factor = 60 * 60;
          break;
        case 'day':
        case 'days':
          $factor = 60 * 60 * 24;
          break;
        default:
          throw new InvalidArgumentException(
            pht(
              'This function can not convert from the unit "%s".',
              $src_unit));
      }
      break;

    case 'bytes':
      switch ($src_unit) {
        case 'byte':
        case 'bytes':
          $factor = 1;
          break;
        case 'bit':
        case 'bits':
          $factor = 8;
          $is_divisor = true;
          break;
        default:
          throw new InvalidArgumentException(
            pht(
              'This function can not convert from the unit "%s".',
              $src_unit));
      }
      break;

    case 'milliseconds':
      switch ($src_unit) {
        case 'second':
        case 'seconds':
          $factor = 1000;
          break;
        case 'minute':
        case 'minutes':
          $factor = 1000 * 60;
          break;
        case 'hour':
        case 'hours':
          $factor = 1000 * 60 * 60;
          break;
        case 'day':
        case 'days':
          $factor = 1000 * 60 * 60 * 24;
          break;
        default:
          throw new InvalidArgumentException(
            pht(
              'This function can not convert from the unit "%s".',
              $src_unit));
      }
      break;

    case 'microseconds':
      switch ($src_unit) {
        case 'second':
        case 'seconds':
          $factor = 1000000;
          break;
        case 'minute':
        case 'minutes':
          $factor = 1000000 * 60;
          break;
        case 'hour':
        case 'hours':
          $factor = 1000000 * 60 * 60;
          break;
        case 'day':
        case 'days':
          $factor = 1000000 * 60 * 60 * 24;
          break;
        default:
          throw new InvalidArgumentException(
            pht(
              'This function can not convert from the unit "%s".',
              $src_unit));
      }
      break;

    default:
      throw new InvalidArgumentException(
        pht(
          'This function can not convert into the unit "%s".',
          $dst_unit));
  }

  if ($is_divisor) {
    if ($quantity % $factor) {
      throw new InvalidArgumentException(
        pht(
          '"%s" is not an exact quantity.',
          $description));
    }
    return (int)($quantity / $factor);
  } else {
    return $quantity * $factor;
  }
}