enhance formatDate function to accept an optional argument containing the time zone
This commit is contained in:
parent
f4eae03f20
commit
814114ab8e
|
@ -383,16 +383,26 @@ class OC_Util {
|
||||||
*
|
*
|
||||||
* @param int $timestamp
|
* @param int $timestamp
|
||||||
* @param bool $dateOnly option to omit time from the result
|
* @param bool $dateOnly option to omit time from the result
|
||||||
|
* @param DateTimeZone|string $timeZone where the given timestamp shall be converted to
|
||||||
* @return string timestamp
|
* @return string timestamp
|
||||||
* @description adjust to clients timezone if we know it
|
* @description adjust to clients timezone if we know it
|
||||||
*/
|
*/
|
||||||
public static function formatDate( $timestamp, $dateOnly = false) {
|
public static function formatDate($timestamp, $dateOnly = false, $timeZone = null) {
|
||||||
if(\OC::$server->getSession()->exists('timezone')) {
|
if (is_null($timeZone)) {
|
||||||
$systemTimeZone = intval(date('O'));
|
if (\OC::$server->getSession()->exists('timezone')) {
|
||||||
$systemTimeZone = (round($systemTimeZone / 100, 0) * 60) + ($systemTimeZone % 100);
|
$systemTimeZone = intval(date('O'));
|
||||||
$clientTimeZone = \OC::$server->getSession()->get('timezone') * 60;
|
$systemTimeZone = (round($systemTimeZone / 100, 0) * 60) + ($systemTimeZone % 100);
|
||||||
$offset = $clientTimeZone - $systemTimeZone;
|
$clientTimeZone = \OC::$server->getSession()->get('timezone') * 60;
|
||||||
$timestamp = $timestamp + $offset * 60;
|
$offset = $clientTimeZone - $systemTimeZone;
|
||||||
|
$timestamp = $timestamp + $offset * 60;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!$timeZone instanceof DateTimeZone) {
|
||||||
|
$timeZone = new DateTimeZone($timeZone);
|
||||||
|
}
|
||||||
|
$dt = new DateTime("@$timestamp");
|
||||||
|
$offset = $timeZone->getOffset($dt);
|
||||||
|
$timestamp += $offset;
|
||||||
}
|
}
|
||||||
$l = \OC::$server->getL10N('lib');
|
$l = \OC::$server->getL10N('lib');
|
||||||
return $l->l($dateOnly ? 'date' : 'datetime', $timestamp);
|
return $l->l($dateOnly ? 'date' : 'datetime', $timestamp);
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
// use OCP namespace for all classes that are considered public.
|
// use OCP namespace for all classes that are considered public.
|
||||||
// This means that they should be used by apps instead of the internal ownCloud classes
|
// This means that they should be used by apps instead of the internal ownCloud classes
|
||||||
namespace OCP;
|
namespace OCP;
|
||||||
|
use DateTimeZone;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides different helper functions to make the life of a developer easier
|
* This class provides different helper functions to make the life of a developer easier
|
||||||
|
@ -167,10 +168,11 @@ class Util {
|
||||||
* formats a timestamp in the "right" way
|
* formats a timestamp in the "right" way
|
||||||
* @param int $timestamp $timestamp
|
* @param int $timestamp $timestamp
|
||||||
* @param bool $dateOnly option to omit time from the result
|
* @param bool $dateOnly option to omit time from the result
|
||||||
|
* @param DateTimeZone|string $timeZone where the given timestamp shall be converted to
|
||||||
* @return string timestamp
|
* @return string timestamp
|
||||||
*/
|
*/
|
||||||
public static function formatDate( $timestamp, $dateOnly=false) {
|
public static function formatDate($timestamp, $dateOnly=false, $timeZone = null) {
|
||||||
return(\OC_Util::formatDate( $timestamp, $dateOnly ));
|
return(\OC_Util::formatDate($timestamp, $dateOnly, $timeZone));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -37,6 +37,30 @@ class Test_Util extends PHPUnit_Framework_TestCase {
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testFormatDateWithTZ() {
|
||||||
|
date_default_timezone_set("UTC");
|
||||||
|
|
||||||
|
$result = OC_Util::formatDate(1350129205, false, 'Europe/Berlin');
|
||||||
|
$expected = 'October 13, 2012 13:53';
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Exception
|
||||||
|
*/
|
||||||
|
function testFormatDateWithInvalidTZ() {
|
||||||
|
OC_Util::formatDate(1350129205, false, 'Mordor/Barad-dûr');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testFormatDateWithTZFromSession() {
|
||||||
|
date_default_timezone_set("UTC");
|
||||||
|
|
||||||
|
\OC::$server->getSession()->set('timezone', 3);
|
||||||
|
$result = OC_Util::formatDate(1350129205, false);
|
||||||
|
$expected = 'October 13, 2012 14:53';
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
}
|
||||||
|
|
||||||
function testCallRegister() {
|
function testCallRegister() {
|
||||||
$result = strlen(OC_Util::callRegister());
|
$result = strlen(OC_Util::callRegister());
|
||||||
$this->assertEquals(30, $result);
|
$this->assertEquals(30, $result);
|
||||||
|
|
Loading…
Reference in New Issue