From 6f7d095e3f4b09847c5963646b2f4a817d68ba39 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sun, 18 Apr 2010 22:11:04 +0100 Subject: [feature/new-tz-handling] Wrapper around DateTime for new date time handling. Wrapped PHP's DateTime with some extensions for supporting phpBB's relative date formats and provided the DateTime::getTimestamp() method to PHP < 5.3. PHPBB3-9558 --- phpBB/includes/datetime.php | 160 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 phpBB/includes/datetime.php (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php new file mode 100644 index 0000000000..b1e95e58a3 --- /dev/null +++ b/phpBB/includes/datetime.php @@ -0,0 +1,160 @@ +_user = $user ? $user : $GLOBALS['user']; + + $timezone = (!$timezone && $this->_user->tz instanceof DateTimeZone) ? $this->_user->tz : $timezone; + + parent::__construct($time, $timezone); + } + + /** + * Returns a UNIX timestamp representation of the date time. + * + * @return int UNIX timestamp + */ + public function getTimestamp() + { + static $compat; + + if (!isset($compat)) + { + $compat = !method_exists('DateTime', 'getTimestamp'); + } + + return !$compat ? parent::getTimestamp() : (int) parent::format('U'); + } + + /** + * 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; + $relative = (strpos($format, self::RELATIVE_WRAPPER) !== false && !$force_absolute); + $now = new self('now', $this->_user->tz, $this->_user); + $delta = $now->getTimestamp() - $this->getTimestamp(); + + if ($relative) + { + if ($delta <= 3600 && ($delta >= -5 || (($now->getTimestamp() / 60) % 60) == (($this->getTimestamp() / 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(); + $gmepoch = $this->getTimestamp(); + + if (!($gmepoch < $midnight - 86400 || $gmepoch > $midnight + 172800)) + { + $day = false; + + if ($gmepoch > $midnight + 86400) + { + $day = 'TOMORROW'; + } + else if ($gmepoch > $midnight) + { + $day = 'TODAY'; + } + else if ($gmepoch > $midnight - 86400) + { + $day = 'YESTERDAY'; + } + + if ($day !== false) + { + $format = self::_format_cache($format, $this->_user); + + return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->_user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang'])); + } + } + } + } + + $format = self::_format_cache($format, $this->_user); + + return strtr(parent::format($format['format_long']), $format['lang']); + } + + /** + * 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' => $user->lang['datetime'], + ); + + // 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]; + } +} -- cgit v1.2.1 From e8b60fc3d8b483fda50c715e59b73861cd3ced9e Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sun, 18 Apr 2010 22:26:33 +0100 Subject: [feature/new-tz-handling] Use phpbb_datetime rather than phpbb_DateTime. PHPBB3-9558 --- phpBB/includes/datetime.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index b1e95e58a3..d14693faa3 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -11,7 +11,7 @@ * phpBB custom extensions to the PHP DateTime class * This handles the relative formats phpBB employs */ -class phpbb_DateTime extends DateTime +class phpbb_datetime extends DateTime { /** * String used to wrap the date segment which should be replaced by today/tomorrow/yesterday @@ -29,7 +29,7 @@ class phpbb_DateTime extends DateTime static protected $format_cache = array(); /** - * Constructs a new instance of phpbb_DateTime, expanded to include an argument to inject + * 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(). -- cgit v1.2.1 From a5c3ff376911f2f25595f1a540c7a16395dac67d Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Tue, 20 Apr 2010 19:30:05 +0100 Subject: [feature/new-tz-handling] Renamed old variables and removed extra conditional. PHPBB3-9558 --- phpBB/includes/datetime.php | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index d14693faa3..92aef88599 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -87,32 +87,27 @@ class phpbb_datetime extends DateTime $midnight = clone $now; $midnight->setTime(0, 0, 0); - $midnight = $midnight->getTimestamp(); - $gmepoch = $this->getTimestamp(); + $midnight = $midnight->getTimestamp(); + $timestamp = $this->getTimestamp(); - if (!($gmepoch < $midnight - 86400 || $gmepoch > $midnight + 172800)) + if ($timestamp > $midnight + 86400) { - $day = false; - - if ($gmepoch > $midnight + 86400) - { - $day = 'TOMORROW'; - } - else if ($gmepoch > $midnight) - { - $day = 'TODAY'; - } - else if ($gmepoch > $midnight - 86400) - { - $day = 'YESTERDAY'; - } - - if ($day !== false) - { - $format = self::_format_cache($format, $this->_user); - - return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->_user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang'])); - } + $day = 'TOMORROW'; + } + else if ($timestamp > $midnight) + { + $day = 'TODAY'; + } + else if ($timestamp > $midnight - 86400) + { + $day = 'YESTERDAY'; + } + + if ($day !== false) + { + $format = self::_format_cache($format, $this->_user); + + return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->_user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang'])); } } } -- cgit v1.2.1 From 522f65d079c61e9ea7f718b67e37a9e58968b0f0 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:21:40 +0100 Subject: [feature/new-tz-handling] Correct typo in member comment. PHPBB3-9558 --- phpBB/includes/datetime.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 92aef88599..3a7fc04105 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -24,7 +24,7 @@ class phpbb_datetime extends DateTime protected $_user; /** - * @var array Date formats are preprocessed by phpBB, to save constact recalculation they are cached. + * @var array Date formats are preprocessed by phpBB, to save constant recalculation they are cached. */ static protected $format_cache = array(); -- cgit v1.2.1 From c521ef1591022e69fe952ec23e6614ae36dee2e2 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:22:38 +0100 Subject: [feature/new-tz-handling] Comment and optimise phpbb_datetime::format(). - Added comments explaining the complex time computations for rendering relative date times. - Replaced some repeated method invokations with variables. PHPBB3-9558 --- phpBB/includes/datetime.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 3a7fc04105..577081e40b 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -74,11 +74,18 @@ class phpbb_datetime extends DateTime $format = $format ? $format : $this->_user->date_format; $relative = (strpos($format, self::RELATIVE_WRAPPER) !== false && !$force_absolute); $now = new self('now', $this->_user->tz, $this->_user); - $delta = $now->getTimestamp() - $this->getTimestamp(); + + $timestamp = $this->getTimestamp(); + $now_ts = $now->getTimeStamp(); + + $delta = $now_ts - $timestamp; if ($relative) { - if ($delta <= 3600 && ($delta >= -5 || (($now->getTimestamp() / 60) % 60) == (($this->getTimestamp() / 60) % 60)) && isset($this->_user->lang['datetime']['AGO'])) + // Check the delta is less than or equal to 1 hour + // 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 >= -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))); } @@ -88,7 +95,6 @@ class phpbb_datetime extends DateTime $midnight->setTime(0, 0, 0); $midnight = $midnight->getTimestamp(); - $timestamp = $this->getTimestamp(); if ($timestamp > $midnight + 86400) { @@ -107,6 +113,7 @@ class phpbb_datetime extends DateTime { $format = self::_format_cache($format, $this->_user); + // 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'])); } } -- cgit v1.2.1 From dba89a534120d48d0ba901aedd69f962536bf36d Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:24:43 +0100 Subject: [feature/new-tz-handling] Added phpbb_datetime::__toString(). New phpbb_datetime::__toString() magic method that formats the datetime according to the users default settings. PHPBB3-9558 --- phpBB/includes/datetime.php | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 577081e40b..8763697c78 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -124,6 +124,16 @@ class phpbb_datetime extends DateTime 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 * -- cgit v1.2.1 From f085735ef812a57790699a6d789cbab22f882328 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:25:52 +0100 Subject: [feature/new-tz-handling] Explained name of phpbb_datetime::getTimestamp() phpbb_datetime::getTimestamp() exists purely to support PHP 5.2 which does not implement the method. PHPBB3-9558 --- phpBB/includes/datetime.php | 1 + 1 file changed, 1 insertion(+) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 8763697c78..12d04d5ca6 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -48,6 +48,7 @@ class phpbb_datetime extends DateTime /** * Returns a UNIX timestamp representation of the date time. * + * @internal This method is for backwards compatibility with 5.2, hence why it doesn't use our method naming standards. * @return int UNIX timestamp */ public function getTimestamp() -- cgit v1.2.1 From 8fe46175aff1d9275b7337a8ced8060ff850d153 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:29:36 +0100 Subject: [feature/new-tz-handling] Fix undefined variable. PHPBB3-9558 --- phpBB/includes/datetime.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 12d04d5ca6..25da6401d7 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -97,6 +97,8 @@ class phpbb_datetime extends DateTime $midnight = $midnight->getTimestamp(); + $day = false; + if ($timestamp > $midnight + 86400) { $day = 'TOMORROW'; -- cgit v1.2.1 From 5dd7916c496b76ab2e1e28b804069dde63f7dbf0 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:47:34 +0100 Subject: [feature/new-tz-handling] Check the is_short flag stored inside the format array. Reuse the existing check store in the format array to determine if the date time format supports relative formatting. PHPBB3-9558 --- phpBB/includes/datetime.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 25da6401d7..ecb3dfcf17 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -73,7 +73,8 @@ class phpbb_datetime extends DateTime public function format($format = '', $force_absolute = false) { $format = $format ? $format : $this->_user->date_format; - $relative = (strpos($format, self::RELATIVE_WRAPPER) !== false && !$force_absolute); + $format = self::_format_cache($format, $this->_user); + $relative = ($format['is_short'] && !$force_absolute); $now = new self('now', $this->_user->tz, $this->_user); $timestamp = $this->getTimestamp(); @@ -114,8 +115,6 @@ class phpbb_datetime extends DateTime if ($day !== false) { - $format = self::_format_cache($format, $this->_user); - // 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'])); } -- cgit v1.2.1 From e9fe9ea5185679e9950e330c949cc2577bfea21d Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:52:04 +0100 Subject: [feature/new-tz-handling] Fix bug from 3.0 formatting future dates. Future dates can get formatted as 'less than a minute ago' if they occur in the future on the same minute as the current minute. PHPBB3-9558 PHPBB3-9712 --- phpBB/includes/datetime.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index ecb3dfcf17..2276d36413 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -85,9 +85,10 @@ class phpbb_datetime extends DateTime 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 >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60)) && isset($this->_user->lang['datetime']['AGO'])) + 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))); } -- cgit v1.2.1 From 6e1278655ff3f250b4f1e09aa8674bae8d86287a Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:57:43 +0100 Subject: [feature/new-tz-handling] Removed line that was missed in cc312d8. PHPBB3-9558 --- phpBB/includes/datetime.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 2276d36413..22b7f871fd 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -122,8 +122,6 @@ class phpbb_datetime extends DateTime } } - $format = self::_format_cache($format, $this->_user); - return strtr(parent::format($format['format_long']), $format['lang']); } -- cgit v1.2.1 From f17664a00c9fa3b80a887bde2cdb2424a5d5567a Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Fri, 9 Jul 2010 20:39:30 +0100 Subject: [feature/new-tz-handling] Correct a bug preventing multiple formats working. PHPBB3-9558 --- phpBB/includes/datetime.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 22b7f871fd..15e3c8b0b7 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -149,22 +149,22 @@ class phpbb_datetime extends DateTime if (!isset(self::$format_cache[$lang])) { self::$format_cache[$lang] = array(); + } - if (!isset(self::$format_cache[$lang][$format])) + 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' => $user->lang['datetime'], + ); + + // 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)) { - // 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' => $user->lang['datetime'], - ); - - // 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']; - } + self::$format_cache[$lang][$format]['lang']['May'] = $user->lang['datetime']['May_short']; } } -- cgit v1.2.1 From 2a880dafdfacb1bc56652976078a3bd3104ceaf9 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Jun 2012 11:03:10 +0200 Subject: [feature/new-tz-handling] Do not use underscore as prefix for user PHPBB3-9558 --- phpBB/includes/datetime.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 15e3c8b0b7..745c3372f1 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -21,7 +21,7 @@ class phpbb_datetime extends DateTime /** * @var user User who is the context for this DateTime instance */ - protected $_user; + protected $user; /** * @var array Date formats are preprocessed by phpBB, to save constant recalculation they are cached. @@ -38,9 +38,9 @@ class phpbb_datetime extends DateTime */ public function __construct($time = 'now', DateTimeZone $timezone = null, $user = null) { - $this->_user = $user ? $user : $GLOBALS['user']; + $this->user = $user ?: $GLOBALS['user']; - $timezone = (!$timezone && $this->_user->tz instanceof DateTimeZone) ? $this->_user->tz : $timezone; + $timezone = (!$timezone && $this->user->tz instanceof DateTimeZone) ? $this->user->tz : $timezone; parent::__construct($time, $timezone); } @@ -72,10 +72,10 @@ class phpbb_datetime extends DateTime */ public function format($format = '', $force_absolute = false) { - $format = $format ? $format : $this->_user->date_format; - $format = self::_format_cache($format, $this->_user); + $format = $format ? $format : $this->user->date_format; + $format = self::_format_cache($format, $this->user); $relative = ($format['is_short'] && !$force_absolute); - $now = new self('now', $this->_user->tz, $this->_user); + $now = new self('now', $this->user->tz, $this->user); $timestamp = $this->getTimestamp(); $now_ts = $now->getTimeStamp(); @@ -88,9 +88,9 @@ class phpbb_datetime extends DateTime // 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'])) + 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))); + return $this->user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60))); } else { @@ -117,7 +117,7 @@ class phpbb_datetime extends DateTime 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 str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang'])); } } } -- cgit v1.2.1 From fb77b6b70cb0fde351209593ea5897b2fd2e5174 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Jun 2012 11:04:49 +0200 Subject: [feature/new-tz-handling] Fix documentation in includes/datetime.php PHPBB3-9558 --- phpBB/includes/datetime.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 745c3372f1..ec2cb3f471 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -2,9 +2,8 @@ /** * * @package phpBB3 -* @version $Id$ -* @copyright (c) 2010 phpBB Group -* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 */ /** @@ -84,10 +83,12 @@ class phpbb_datetime extends DateTime 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 + /** + * 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))); -- cgit v1.2.1 From 2c4677b79ee657d2ab9f4e2592924391a7814f79 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Jun 2012 11:05:42 +0200 Subject: [feature/new-tz-handling] Remove function getTimestamp() as we require php 5.3 PHPBB3-9558 --- phpBB/includes/datetime.php | 18 ------------------ 1 file changed, 18 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index ec2cb3f471..0cb4c79a10 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -44,24 +44,6 @@ class phpbb_datetime extends DateTime parent::__construct($time, $timezone); } - /** - * Returns a UNIX timestamp representation of the date time. - * - * @internal This method is for backwards compatibility with 5.2, hence why it doesn't use our method naming standards. - * @return int UNIX timestamp - */ - public function getTimestamp() - { - static $compat; - - if (!isset($compat)) - { - $compat = !method_exists('DateTime', 'getTimestamp'); - } - - return !$compat ? parent::getTimestamp() : (int) parent::format('U'); - } - /** * Formats the current date time into the specified format * -- cgit v1.2.1 From a613caab093c3b354a7121c7989c9687d12bcdb4 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 19 Jun 2012 19:24:03 +0200 Subject: [feature/new-tz-handling] Trim comment length and remove leading underscores PHPBB3-9558 --- phpBB/includes/datetime.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 0cb4c79a10..00dc1958e5 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -39,7 +39,7 @@ class phpbb_datetime extends DateTime { $this->user = $user ?: $GLOBALS['user']; - $timezone = (!$timezone && $this->user->tz instanceof DateTimeZone) ? $this->user->tz : $timezone; + $timezone = $timezone ?: $this->user->tz; parent::__construct($time, $timezone); } @@ -54,7 +54,7 @@ class phpbb_datetime extends DateTime public function format($format = '', $force_absolute = false) { $format = $format ? $format : $this->user->date_format; - $format = self::_format_cache($format, $this->user); + $format = self::format_cache($format, $this->user); $relative = ($format['is_short'] && !$force_absolute); $now = new self('now', $this->user->tz, $this->user); @@ -68,7 +68,8 @@ class phpbb_datetime extends DateTime /** * 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) + * 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'])) @@ -125,7 +126,7 @@ class phpbb_datetime extends DateTime * @param user $user User object to use for localisation * @return array Processed date format */ - static protected function _format_cache($format, $user) + static protected function format_cache($format, $user) { $lang = $user->lang_name; -- cgit v1.2.1 From 82e195ac683f6604aee9862233ffcb4f58af64b0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Jun 2012 14:34:35 +0200 Subject: [feature/new-tz-handling] Fix code and doc layout PHPBB3-9558 --- phpBB/includes/datetime.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 00dc1958e5..317376431d 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -65,14 +65,16 @@ class phpbb_datetime extends DateTime 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'])) + 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))); } -- cgit v1.2.1 From f5bb145040a483a76fd734bb9d4025c54e7917a0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 16 Jul 2012 18:29:31 +0200 Subject: [feature/new-tz-handling] Require user argument on phpbb_datetime PHPBB3-9558 --- phpBB/includes/datetime.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 317376431d..0d80c77115 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -35,10 +35,9 @@ class phpbb_datetime extends DateTime * @param DateTimeZone $timezone Time zone of the time. * @param user User object for context. */ - public function __construct($time = 'now', DateTimeZone $timezone = null, $user = null) + public function __construct($user, $time = 'now', DateTimeZone $timezone = null) { - $this->user = $user ?: $GLOBALS['user']; - + $this->user = $user; $timezone = $timezone ?: $this->user->tz; parent::__construct($time, $timezone); @@ -56,7 +55,7 @@ class phpbb_datetime extends DateTime $format = $format ? $format : $this->user->date_format; $format = self::format_cache($format, $this->user); $relative = ($format['is_short'] && !$force_absolute); - $now = new self('now', $this->user->tz, $this->user); + $now = new self($this->user, 'now', $this->user->tz); $timestamp = $this->getTimestamp(); $now_ts = $now->getTimeStamp(); -- cgit v1.2.1 From a71e60cdbd3e47c62a565dd777a28aec6832fb88 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 18 Jul 2012 16:52:52 +0200 Subject: [feature/new-tz-handling] Rename $user->tz back to $user->timezone PHPBB3-9558 --- phpBB/includes/datetime.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/datetime.php') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 0d80c77115..b3462ddf67 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -38,7 +38,7 @@ class phpbb_datetime extends DateTime public function __construct($user, $time = 'now', DateTimeZone $timezone = null) { $this->user = $user; - $timezone = $timezone ?: $this->user->tz; + $timezone = $timezone ?: $this->user->timezone; parent::__construct($time, $timezone); } @@ -55,7 +55,7 @@ class phpbb_datetime extends DateTime $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->tz); + $now = new self($this->user, 'now', $this->user->timezone); $timestamp = $this->getTimestamp(); $now_ts = $now->getTimeStamp(); -- cgit v1.2.1