Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15421617
D8126.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D8126.diff
View Options
Index: src/__phutil_library_map__.php
===================================================================
--- src/__phutil_library_map__.php
+++ src/__phutil_library_map__.php
@@ -384,6 +384,7 @@
'phutil_tag' => 'markup/render.php',
'phutil_tag_div' => 'markup/render.php',
'phutil_unescape_uri_path_component' => 'markup/render.php',
+ 'phutil_units' => 'utils/utils.php',
'phutil_utf8_console_strlen' => 'utils/utf8.php',
'phutil_utf8_convert' => 'utils/utf8.php',
'phutil_utf8_hard_wrap' => 'utils/utf8.php',
Index: src/utils/__tests__/PhutilUtilsTestCase.php
===================================================================
--- src/utils/__tests__/PhutilUtilsTestCase.php
+++ src/utils/__tests__/PhutilUtilsTestCase.php
@@ -461,6 +461,47 @@
phutil_loggable_string("a\x1Fb"));
}
+ public function testPhutilUnits() {
+ $cases = array(
+ '0 seconds in seconds' => 0,
+ '1 second in seconds' => 1,
+ '2 seconds in seconds' => 2,
+ '100 seconds in seconds' => 100,
+ '2 minutes in seconds' => 120,
+ '1 hour in seconds' => 3600,
+ '1 day in seconds' => 86400,
+ '3 days in seconds' => 259200,
+ );
+
+ foreach ($cases as $input => $expect) {
+ $this->assertEqual(
+ $expect,
+ phutil_units($input),
+ 'phutil_units("'.$input.'")');
+ }
+ $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',
+ );
+
+ foreach ($bad_cases as $input) {
+ $caught = null;
+ try {
+ phutil_units($input);
+ } catch (InvalidArgumentException $ex) {
+ $caught = $ex;
+ }
+
+ $this->assertEqual(
+ true,
+ ($caught instanceof InvalidArgumentException),
+ 'phutil_units("'.$input.'")');
+ }
+ }
}
Index: src/utils/utils.php
===================================================================
--- src/utils/utils.php
+++ src/utils/utils.php
@@ -988,3 +988,71 @@
// is broken and return `false`.
return false;
}
+
+
+/**
+ * Convert a human-readable unit description into a numeric one. This function
+ * allows you to replace this:
+ *
+ * COUNTEREXAMPLE
+ * $ttl = (60 * 60 * 24 * 30); // 30 days
+ *
+ * ...with this:
+ *
+ * $ttl = phutil_units('30 days in seconds');
+ *
+ * ...which is self-documenting and difficult to make a mistake with.
+ *
+ * @param string Human readable description of a unit quantity.
+ * @return int Quantity of specified unit.
+ */
+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 "5 days in seconds"): %s',
+ $description));
+ }
+
+ $quantity = (int)$matches[1];
+ $src_unit = $matches[2];
+ $dst_unit = $matches[3];
+
+ 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;
+ default:
+ throw new InvalidArgumentException(
+ pht(
+ 'This function can not convert into the unit "%s".',
+ $dst_unit));
+ }
+
+ return $quantity * $factor;
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Mar 23, 1:21 AM (1 w, 4 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7717665
Default Alt Text
D8126.diff (3 KB)
Attached To
Mode
D8126: Implement a basic `phutil_units()` function for writing "30 days in seconds" readably
Attached
Detach File
Event Timeline
Log In to Comment