Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F1046990
D229.id538.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Jun 29, 3:25 PM
Size
6 KB
Mime Type
text/x-diff
Expires
Mon, Jun 30, 3:25 PM (2 h, 21 m)
Engine
blob
Format
Raw Data
Handle
676921
Attached To
D229: INP-1565 Add support for "relative" date format
D229.id538.diff
View Options
Index: core/install/cache/class_structure.php
===================================================================
--- core/install/cache/class_structure.php
+++ core/install/cache/class_structure.php
@@ -44,6 +44,7 @@
'CustomDataEventHandler' => '/core/units/custom_data/custom_data_event_handler.php',
'CustomFieldsEventHandler' => '/core/units/custom_fields/custom_fields_event_handler.php',
'CustomFieldsTagProcessor' => '/core/units/custom_fields/custom_fields_tag_processor.php',
+ 'DateHelper' => '/core/units/helpers/date_helper.php',
'Debugger' => '/core/kernel/utility/debugger.php',
'DebuggerUtil' => '/core/kernel/utility/debugger.php',
'DeploymentHelper' => '/core/units/helpers/deployment_helper.php',
@@ -604,6 +605,13 @@
0 => 'kDBTagProcessor',
),
),
+ 'DateHelper' => array(
+ 'type' => 1,
+ 'modifiers' => 0,
+ 'extends' => array(
+ 0 => 'kHelper',
+ ),
+ ),
'Debugger' => array(
'type' => 1,
'modifiers' => 0,
Index: core/install/english.lang
===================================================================
--- core/install/english.lang
+++ core/install/english.lang
@@ -1747,6 +1747,7 @@
<PHRASE Label="la_Windows" Module="Core" Type="1">V2luZG93cw==</PHRASE>
<PHRASE Label="la_year" Module="Core" Type="1">eWVhcg==</PHRASE>
<PHRASE Label="la_Yes" Module="Core" Type="1">WWVz</PHRASE>
+ <PHRASE Label="lc_ago" Module="Core" Type="2">YWdv</PHRASE>
<PHRASE Label="lc_field_CachedDescendantCatsQty" Module="Core" Type="2">U3ViLXNlY3Rpb25zIFF1YW50aXR5</PHRASE>
<PHRASE Label="lc_field_CachedNavBar" Module="Core" Type="2">TmF2aWdhdGlvbiBCYXI=</PHRASE>
<PHRASE Label="lc_field_cachedrating" Module="Core" Type="2">UmF0aW5n</PHRASE>
@@ -1779,6 +1780,7 @@
<PHRASE Label="lc_field_topseller" Module="Core" Type="2">SXRlbSBJcyBhIFRvcCBTZWxsZXI=</PHRASE>
<PHRASE Label="lc_field_url" Module="Core" Type="2">VVJM</PHRASE>
<PHRASE Label="lc_of" Module="Core" Type="2">b2Y=</PHRASE>
+ <PHRASE Label="lc_since" Module="Core" Type="2">c2luY2U=</PHRASE>
<PHRASE Label="lc_Text_Invalid" Module="Core" Type="2">SW52YWxpZA==</PHRASE>
<PHRASE Label="lc_Text_Not_Validated" Module="Core" Type="2">Tm90IFZhbGlkYXRlZA==</PHRASE>
<PHRASE Label="lc_Text_Valid" Module="Core" Type="2">VmFsaWQ=</PHRASE>
Index: core/kernel/utility/formatters/date_formatter.php
===================================================================
--- core/kernel/utility/formatters/date_formatter.php
+++ core/kernel/utility/formatters/date_formatter.php
@@ -24,6 +24,13 @@
var $language = null;
/**
+ * Date helper.
+ *
+ * @var DateHelper
+ */
+ protected $dateHelper;
+
+ /**
* Create date formatter
*
* @access public
@@ -264,6 +271,12 @@
$options['format'] = $options['input_format'];
}
+ if ( preg_match('/^relative(.*)/', $options['format'], $regs) ) {
+ $detalization_level = trim($regs[1], ':');
+
+ return $this->RelativeFormat($value, ($detalization_level < 1) ? 1 : $detalization_level);
+ }
+
if (!$options['use_timezone']) {
return gmdate($options['format'], $value);
}
@@ -278,6 +291,30 @@
return date($format, $value);
}
+ /**
+ * Formats value as relation to current time;
+ *
+ * @param integer $from Date.
+ * @param integer $detalization_level Detalization level.
+ *
+ * @return string
+ */
+ protected function RelativeFormat($from, $detalization_level)
+ {
+ if ( !isset($this->dateHelper) ) {
+ $this->dateHelper = $this->Application->recallObject('DateHelper');
+ }
+
+ if ( $from == TIMENOW ) {
+ $from = TIMENOW - 1;
+ }
+
+ $suffix = $this->Application->Phrase($from > TIMENOW ? 'lc_since' : 'lc_ago');
+ $res = $this->dateHelper->getHumanTime(abs($from - TIMENOW), $detalization_level);
+
+ return $res . ' ' . $suffix;
+ }
+
function HumanFormat($format)
{
$patterns = Array('/m/',
@@ -506,4 +543,4 @@
{
return $this->Format( time(), $field, $object, $options['input_format']);
}
-}
\ No newline at end of file
+}
Index: core/units/helpers/date_helper.php
===================================================================
--- /dev/null
+++ core/units/helpers/date_helper.php
@@ -0,0 +1,81 @@
+<?php
+/**
+ * @version $Id$
+ * @package In-Portal
+ * @copyright Copyright (C) 1997 - 2011 Intechnic. All rights reserved.
+ * @license GNU/GPL
+ * In-Portal is Open Source software.
+ * This means that this software may have been modified pursuant
+ * the GNU General Public License, and as distributed it includes
+ * or is derivative of works licensed under the GNU General Public License
+ * or other free or open source software licenses.
+ * See http://www.in-portal.org/license for copyright notices and details.
+ */
+
+defined('FULL_PATH') or die('restricted access!');
+
+class DateHelper extends kHelper
+{
+
+ /**
+ * Returns human representation of given seconds (e.g. `12 days, and 5 hours`).
+ *
+ * @param integer $seconds Seconds.
+ * @param integer $detalization_level Detalization level.
+ *
+ * @return string
+ */
+ public function getHumanTime($seconds, $detalization_level)
+ {
+ $units = array(
+ 'year' => 29030400, // Seconds in a year (12 months).
+ 'month' => 2419200, // Seconds in a month (4 weeks).
+ 'week' => 604800, // Seconds in a week (7 days).
+ 'day' => 86400, // Seconds in a day (24 hours).
+ 'hour' => 3600, // Seconds in an hour (60 minutes).
+ 'minute' => 60, // Seconds in a minute (60 seconds).
+ 'second' => 1, // 1 second.
+ );
+
+ $res = '';
+ $current_detalization_level = 0;
+
+ foreach ( $units as $unit => $multiplier ) {
+ if ( $seconds >= $multiplier ) {
+ $and = (!$res || $multiplier != 1) ? '' : 'and ';
+ $res .= ', ' . $and . intval($seconds / $multiplier);
+ $res .= ' ' . $unit . (intval($seconds / $multiplier) == 1 ? '' : 's');
+ $seconds -= intval($seconds / $multiplier) * $multiplier;
+ $current_detalization_level++;
+
+ if ( $current_detalization_level >= $detalization_level ) {
+ break;
+ }
+ }
+ }
+
+ return substr($res, strlen(', '));
+ }
+
+ /**
+ * Returns human representation of given seconds in short format (e.g. `12d 5h`).
+ *
+ * @param integer $seconds Seconds.
+ *
+ * @return string
+ */
+ public function getShortHumanTime($seconds)
+ {
+ $now = new DateTime();
+ $diff_date = new DateTime();
+ $now->setTimestamp(TIMENOW);
+ $diff_date->setTimestamp(TIMENOW + $seconds);
+ $interval = $diff_date->diff($now);
+
+ $ret = $interval->format(' %yy %mm %dd %hh %imi %ss');
+ $ret = trim(preg_replace('/ 0\w+/', '', $ret));
+
+ return $ret;
+ }
+
+}
Event Timeline
Log In to Comment