Theme: use format_date function for daily date

This commit is contained in:
ArthurHoaro 2017-03-28 20:40:14 +02:00
parent b64d83cd2b
commit 81bd104daa
6 changed files with 66 additions and 9 deletions

View file

@ -321,24 +321,26 @@ function normalize_spaces($string)
* otherwise default format '%c' will be returned. * otherwise default format '%c' will be returned.
* *
* @param DateTime $date to format. * @param DateTime $date to format.
* @param bool $time Displays time if true.
* @param bool $intl Use international format if true. * @param bool $intl Use international format if true.
* *
* @return bool|string Formatted date, or false if the input is invalid. * @return bool|string Formatted date, or false if the input is invalid.
*/ */
function format_date($date, $intl = true) function format_date($date, $time = true, $intl = true)
{ {
if (! $date instanceof DateTime) { if (! $date instanceof DateTime) {
return false; return false;
} }
if (! $intl || ! class_exists('IntlDateFormatter')) { if (! $intl || ! class_exists('IntlDateFormatter')) {
return strftime('%c', $date->getTimestamp()); $format = $time ? '%c' : '%x';
return strftime($format, $date->getTimestamp());
} }
$formatter = new IntlDateFormatter( $formatter = new IntlDateFormatter(
setlocale(LC_TIME, 0), setlocale(LC_TIME, 0),
IntlDateFormatter::LONG, IntlDateFormatter::LONG,
IntlDateFormatter::LONG $time ? IntlDateFormatter::LONG : IntlDateFormatter::NONE
); );
return $formatter->format($date); return $formatter->format($date);

View file

@ -698,6 +698,7 @@ function showDaily($pageBuilder, $LINKSDB, $conf, $pluginManager)
'linksToDisplay' => $linksToDisplay, 'linksToDisplay' => $linksToDisplay,
'cols' => $columns, 'cols' => $columns,
'day' => $dayDate->getTimestamp(), 'day' => $dayDate->getTimestamp(),
'dayDate' => $dayDate,
'previousday' => $previousday, 'previousday' => $previousday,
'nextday' => $nextday, 'nextday' => $nextday,
); );

View file

@ -11,7 +11,16 @@ class UtilsDeTest extends UtilsTest
public function testDateFormat() public function testDateFormat()
{ {
$date = DateTime::createFromFormat('Ymd_His', '20170101_101112'); $date = DateTime::createFromFormat('Ymd_His', '20170101_101112');
$this->assertRegExp('/1. Januar 2017 (um )?10:11:12 GMT\+0?3(:00)?/', format_date($date, true)); $this->assertRegExp('/1\. Januar 2017 (um )?10:11:12 GMT\+0?3(:00)?/', format_date($date, true, true));
}
/**
* Test date_format() without time.
*/
public function testDateFormatNoTime()
{
$date = DateTime::createFromFormat('Ymd_His', '20170101_101112');
$this->assertRegExp('/1\. Januar 2017/', format_date($date, false,true));
} }
/** /**
@ -20,7 +29,16 @@ public function testDateFormat()
public function testDateFormatDefault() public function testDateFormatDefault()
{ {
$date = DateTime::createFromFormat('Ymd_His', '20170101_101112'); $date = DateTime::createFromFormat('Ymd_His', '20170101_101112');
$this->assertEquals('So 01 Jan 2017 10:11:12 EAT', format_date($date, false)); $this->assertEquals('So 01 Jan 2017 10:11:12 EAT', format_date($date, true, false));
}
/**
* Test date_format() using builtin PHP function strftime without time.
*/
public function testDateFormatDefaultNoTime()
{
$date = DateTime::createFromFormat('Ymd_His', '20170201_101112');
$this->assertEquals('01.02.2017', format_date($date, false, false));
} }
/** /**

View file

@ -11,7 +11,16 @@ class UtilsEnTest extends UtilsTest
public function testDateFormat() public function testDateFormat()
{ {
$date = DateTime::createFromFormat('Ymd_His', '20170101_101112'); $date = DateTime::createFromFormat('Ymd_His', '20170101_101112');
$this->assertRegExp('/January 1, 2017 (at )?10:11:12 AM GMT\+0?3(:00)?/', format_date($date, true)); $this->assertRegExp('/January 1, 2017 (at )?10:11:12 AM GMT\+0?3(:00)?/', format_date($date, true, true));
}
/**
* Test date_format() without time.
*/
public function testDateFormatNoTime()
{
$date = DateTime::createFromFormat('Ymd_His', '20170101_101112');
$this->assertRegExp('/January 1, 2017/', format_date($date, false, true));
} }
/** /**
@ -20,7 +29,16 @@ public function testDateFormat()
public function testDateFormatDefault() public function testDateFormatDefault()
{ {
$date = DateTime::createFromFormat('Ymd_His', '20170101_101112'); $date = DateTime::createFromFormat('Ymd_His', '20170101_101112');
$this->assertEquals('Sun 01 Jan 2017 10:11:12 AM EAT', format_date($date, false)); $this->assertEquals('Sun 01 Jan 2017 10:11:12 AM EAT', format_date($date, true, false));
}
/**
* Test date_format() using builtin PHP function strftime without time.
*/
public function testDateFormatDefaultNoTime()
{
$date = DateTime::createFromFormat('Ymd_His', '20170201_101112');
$this->assertEquals('02/01/2017', format_date($date, false, false));
} }
/** /**

View file

@ -14,13 +14,31 @@ public function testDateFormat()
$this->assertRegExp('/1 janvier 2017 (à )?10:11:12 UTC\+0?3(:00)?/', format_date($date)); $this->assertRegExp('/1 janvier 2017 (à )?10:11:12 UTC\+0?3(:00)?/', format_date($date));
} }
/**
* Test date_format() without time.
*/
public function testDateFormatNoTime()
{
$date = DateTime::createFromFormat('Ymd_His', '20170101_101112');
$this->assertRegExp('/1 janvier 2017/', format_date($date, false, true));
}
/** /**
* Test date_format() using builtin PHP function strftime. * Test date_format() using builtin PHP function strftime.
*/ */
public function testDateFormatDefault() public function testDateFormatDefault()
{ {
$date = DateTime::createFromFormat('Ymd_His', '20170101_101112'); $date = DateTime::createFromFormat('Ymd_His', '20170101_101112');
$this->assertEquals('dim. 01 janv. 2017 10:11:12 EAT', format_date($date, false)); $this->assertEquals('dim. 01 janv. 2017 10:11:12 EAT', format_date($date, true, false));
}
/**
* Test date_format() using builtin PHP function strftime without time.
*/
public function testDateFormatDefaultNoTime()
{
$date = DateTime::createFromFormat('Ymd_His', '20170201_101112');
$this->assertEquals('01/02/2017', format_date($date, false, false));
} }
/** /**

View file

@ -44,7 +44,7 @@ <h2 class="window-title">
</div> </div>
</div> </div>
<div> <div>
<h3 class="window-subtitle">{function="strftime('%A %d, %B %Y', $day)"}</h3> <h3 class="window-subtitle">{function="format_date($dayDate, false)"}</h3>
<div id="plugin_zone_about_daily" class="plugin_zone"> <div id="plugin_zone_about_daily" class="plugin_zone">
{loop="$daily_about_plugin"} {loop="$daily_about_plugin"}