diff --git a/src/utils/__tests__/PhutilUtilsTestCase.php b/src/utils/__tests__/PhutilUtilsTestCase.php --- a/src/utils/__tests__/PhutilUtilsTestCase.php +++ b/src/utils/__tests__/PhutilUtilsTestCase.php @@ -474,6 +474,8 @@ '128 bits in bytes' => 16, '1 byte in bytes' => 1, '8 bits in bytes' => 1, + '1 minute in milliseconds' => 60000, + '2 minutes in microseconds' => 120000000, ); foreach ($cases as $input => $expect) { @@ -486,7 +488,6 @@ $bad_cases = array( 'quack', '3 years in seconds', - '1 minute in milliseconds', '1 day in days', '-1 minutes in seconds', '1.5 minutes in seconds', diff --git a/src/utils/utils.php b/src/utils/utils.php --- a/src/utils/utils.php +++ b/src/utils/utils.php @@ -1113,6 +1113,7 @@ $src_unit)); } break; + case 'bytes': switch ($src_unit) { case 'byte': @@ -1131,6 +1132,59 @@ $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(