aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/datetime.php
diff options
context:
space:
mode:
authorIgor Wiedler <igor@wiedler.ch>2013-07-14 11:34:58 -0400
committerIgor Wiedler <igor@wiedler.ch>2013-07-14 11:34:58 -0400
commitf6865dc33a8cedb7d21a64401ed80d2edbac36bd (patch)
tree66aa645c4d86636f0bce04a7125a7757a466b224 /phpBB/phpbb/datetime.php
parentfa3cdb6bf2cfdca9c26168bd258752718bc8a43f (diff)
parentc15bde161a93fc2abc48cacd7e5a71c682880e52 (diff)
downloadforums-f6865dc33a8cedb7d21a64401ed80d2edbac36bd.tar
forums-f6865dc33a8cedb7d21a64401ed80d2edbac36bd.tar.gz
forums-f6865dc33a8cedb7d21a64401ed80d2edbac36bd.tar.bz2
forums-f6865dc33a8cedb7d21a64401ed80d2edbac36bd.tar.xz
forums-f6865dc33a8cedb7d21a64401ed80d2edbac36bd.zip
Merge remote-tracking branch 'upstream/develop' into ticket/11574
* upstream/develop: (575 commits) [ticket/11702] Fix forum_posts left over for link-click counts in viewforum.php [ticket/11696] Move file to new directory [ticket/11696] Rename constructor to __construct() [ticket/11696] Remove manual loading of db_tools in extension controller test [ticket/11696] Rename db_tools.php so it can be autoloaded [ticket/11698] Moving all autoloadable files to phpbb/ [ticket/11694] Do not locate assets with root path [ticket/11692] Don't update search_type in dev migration if already appended [ticket/11675] Fix template loop [ticket/11690] Old module class names may get autoloaded by class_exists [ticket/9649] Display information on index for moderators on unapproved posts [ticket/10999] Fix assets_version in ACP [prep-release-3.0.12] More changelog items for the 3.0.12 release. [ticket/11687] Add assets_version to phpbb_config [ticket/11686] Not checking for phpBB Debug errors on functional tests [ticket/11670] Consistency with logo: Replace "phpBB(tm)" with "phpBB(R)". [ticket/11674] Do not include vendor folder if there are no dependencies. [ticket/11685] Remove logout confirmation page [ticket/11684] Remove useless confirmation page after login and admin login [ticket/9657] Define user before injecting ... Conflicts: phpBB/includes/functions_container.php phpBB/install/database_update.php phpBB/install/index.php
Diffstat (limited to 'phpBB/phpbb/datetime.php')
-rw-r--r--phpBB/phpbb/datetime.php158
1 files changed, 158 insertions, 0 deletions
diff --git a/phpBB/phpbb/datetime.php b/phpBB/phpbb/datetime.php
new file mode 100644
index 0000000000..3c6d4971b9
--- /dev/null
+++ b/phpBB/phpbb/datetime.php
@@ -0,0 +1,158 @@
+<?php
+/**
+*
+* @package phpBB3
+* @copyright (c) 2012 phpBB Group
+* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
+*/
+
+/**
+* phpBB custom extensions to the PHP DateTime class
+* This handles the relative formats phpBB employs
+*/
+class phpbb_datetime extends DateTime
+{
+ /**
+ * String used to wrap the date segment which should be replaced by today/tomorrow/yesterday
+ */
+ const RELATIVE_WRAPPER = '|';
+
+ /**
+ * @var user User who is the context for this DateTime instance
+ */
+ protected $user;
+
+ /**
+ * @var array Date formats are preprocessed by phpBB, to save constant recalculation they are cached.
+ */
+ static protected $format_cache = array();
+
+ /**
+ * Constructs a new instance of phpbb_datetime, expanded to include an argument to inject
+ * the user context and modify the timezone to the users selected timezone if one is not set.
+ *
+ * @param string $time String in a format accepted by strtotime().
+ * @param DateTimeZone $timezone Time zone of the time.
+ * @param user User object for context.
+ */
+ public function __construct($user, $time = 'now', DateTimeZone $timezone = null)
+ {
+ $this->user = $user;
+ $timezone = $timezone ?: $this->user->timezone;
+
+ parent::__construct($time, $timezone);
+ }
+
+ /**
+ * Formats the current date time into the specified format
+ *
+ * @param string $format Optional format to use for output, defaults to users chosen format
+ * @param boolean $force_absolute Force output of a non relative date
+ * @return string Formatted date time
+ */
+ public function format($format = '', $force_absolute = false)
+ {
+ $format = $format ? $format : $this->user->date_format;
+ $format = self::format_cache($format, $this->user);
+ $relative = ($format['is_short'] && !$force_absolute);
+ $now = new self($this->user, 'now', $this->user->timezone);
+
+ $timestamp = $this->getTimestamp();
+ $now_ts = $now->getTimeStamp();
+
+ $delta = $now_ts - $timestamp;
+
+ if ($relative)
+ {
+ /*
+ * Check the delta is less than or equal to 1 hour
+ * and the delta not more than a minute in the past
+ * and the delta is either greater than -5 seconds or timestamp
+ * and current time are of the same minute (they must be in the same hour already)
+ * finally check that relative dates are supported by the language pack
+ */
+ if ($delta <= 3600 && $delta > -60 &&
+ ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60))
+ && isset($this->user->lang['datetime']['AGO']))
+ {
+ return $this->user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60)));
+ }
+ else
+ {
+ $midnight = clone $now;
+ $midnight->setTime(0, 0, 0);
+
+ $midnight = $midnight->getTimestamp();
+
+ $day = false;
+
+ if ($timestamp > $midnight + 86400)
+ {
+ $day = 'TOMORROW';
+ }
+ else if ($timestamp > $midnight)
+ {
+ $day = 'TODAY';
+ }
+ else if ($timestamp > $midnight - 86400)
+ {
+ $day = 'YESTERDAY';
+ }
+
+ if ($day !== false)
+ {
+ // Format using the short formatting and finally swap out the relative token placeholder with the correct value
+ return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang']));
+ }
+ }
+ }
+
+ return strtr(parent::format($format['format_long']), $format['lang']);
+ }
+
+ /**
+ * Magic method to convert DateTime object to string
+ *
+ * @return Formatted date time, according to the users default settings.
+ */
+ public function __toString()
+ {
+ return $this->format();
+ }
+
+ /**
+ * Pre-processes the specified date format
+ *
+ * @param string $format Output format
+ * @param user $user User object to use for localisation
+ * @return array Processed date format
+ */
+ static protected function format_cache($format, $user)
+ {
+ $lang = $user->lang_name;
+
+ if (!isset(self::$format_cache[$lang]))
+ {
+ self::$format_cache[$lang] = array();
+ }
+
+ if (!isset(self::$format_cache[$lang][$format]))
+ {
+ // Is the user requesting a friendly date format (i.e. 'Today 12:42')?
+ self::$format_cache[$lang][$format] = array(
+ 'is_short' => strpos($format, self::RELATIVE_WRAPPER) !== false,
+ 'format_short' => substr($format, 0, strpos($format, self::RELATIVE_WRAPPER)) . self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER . substr(strrchr($format, self::RELATIVE_WRAPPER), 1),
+ 'format_long' => str_replace(self::RELATIVE_WRAPPER, '', $format),
+ 'lang' => array_filter($user->lang['datetime'], 'is_string'),
+ );
+
+ // Short representation of month in format? Some languages use different terms for the long and short format of May
+ if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false))
+ {
+ self::$format_cache[$lang][$format]['lang']['May'] = $user->lang['datetime']['May_short'];
+ }
+ }
+
+ return self::$format_cache[$lang][$format];
+ }
+}