Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15445581
D16123.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D16123.diff
View Options
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
@@ -471,6 +471,9 @@
'1 hour in seconds' => 3600,
'1 day in seconds' => 86400,
'3 days in seconds' => 259200,
+ '128 bits in bytes' => 16,
+ '1 byte in bytes' => 1,
+ '8 bits in bytes' => 1,
);
foreach ($cases as $input => $expect) {
@@ -487,6 +490,10 @@
'1 day in days',
'-1 minutes in seconds',
'1.5 minutes in seconds',
+ '7 bits in bytes',
+ '2 hours in bytes',
+ '1 dram in bytes',
+ '24 bits in seconds',
);
foreach ($bad_cases as $input) {
diff --git a/src/utils/utils.php b/src/utils/utils.php
--- a/src/utils/utils.php
+++ b/src/utils/utils.php
@@ -1070,6 +1070,8 @@
$src_unit = $matches[2];
$dst_unit = $matches[3];
+ $is_divisor = false;
+
switch ($dst_unit) {
case 'seconds':
switch ($src_unit) {
@@ -1096,6 +1098,24 @@
$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;
default:
throw new InvalidArgumentException(
pht(
@@ -1103,7 +1123,17 @@
$dst_unit));
}
- return $quantity * $factor;
+ 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;
+ }
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Mar 28, 2:44 PM (3 w, 6 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7431617
Default Alt Text
D16123.diff (1 KB)
Attached To
Mode
D16123: Support "x bits in bytes" in phutil_units()
Attached
Detach File
Event Timeline
Log In to Comment