mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #5537 from IgorNadj/patch-niceformats
ENH: moving hardcoded nice date/time formats to config
This commit is contained in:
commit
b634b1a67d
@ -20,6 +20,14 @@
|
|||||||
*/
|
*/
|
||||||
class Date extends DBField {
|
class Date extends DBField {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @config
|
||||||
|
* @see SS_DateTime::nice_format
|
||||||
|
* @see Time::nice_format
|
||||||
|
*/
|
||||||
|
private static $nice_format = 'd/m/Y';
|
||||||
|
|
||||||
|
|
||||||
public function setValue($value, $record = null) {
|
public function setValue($value, $record = null) {
|
||||||
if($value === false || $value === null || (is_string($value) && !strlen($value))) {
|
if($value === false || $value === null || (is_string($value) && !strlen($value))) {
|
||||||
// don't try to evaluate empty values with strtotime() below, as it returns "1970-01-01" when it should be
|
// don't try to evaluate empty values with strtotime() below, as it returns "1970-01-01" when it should be
|
||||||
@ -59,10 +67,10 @@ class Date extends DBField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the date in the format dd/mm/yy
|
* Returns the date in the format specified by the config value nice_format, or dd/mm/yy by default
|
||||||
*/
|
*/
|
||||||
public function Nice() {
|
public function Nice() {
|
||||||
if($this->value) return $this->Format('d/m/Y');
|
if($this->value) return $this->Format($this->config()->nice_format);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,6 +25,14 @@
|
|||||||
*/
|
*/
|
||||||
class SS_Datetime extends Date implements TemplateGlobalProvider {
|
class SS_Datetime extends Date implements TemplateGlobalProvider {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @config
|
||||||
|
* @see Date::nice_format
|
||||||
|
* @see Time::nice_format
|
||||||
|
*/
|
||||||
|
private static $nice_format = 'd/m/Y g:ia';
|
||||||
|
|
||||||
|
|
||||||
public function setValue($value, $record = null) {
|
public function setValue($value, $record = null) {
|
||||||
if($value === false || $value === null || (is_string($value) && !strlen($value))) {
|
if($value === false || $value === null || (is_string($value) && !strlen($value))) {
|
||||||
// don't try to evaluate empty values with strtotime() below, as it returns "1970-01-01" when it should be
|
// don't try to evaluate empty values with strtotime() below, as it returns "1970-01-01" when it should be
|
||||||
@ -54,11 +62,12 @@ class SS_Datetime extends Date implements TemplateGlobalProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the date and time (in 12-hour format) using the format string 'd/m/Y g:ia' e.g. '31/01/2014 2:23pm'.
|
* Returns the date and time in the format specified by the config value nice_format, or 'd/m/Y g:ia'
|
||||||
|
* by default (e.g. '31/01/2014 2:23pm').
|
||||||
* @return string Formatted date and time.
|
* @return string Formatted date and time.
|
||||||
*/
|
*/
|
||||||
public function Nice() {
|
public function Nice() {
|
||||||
if($this->value) return $this->Format('d/m/Y g:ia');
|
if($this->value) return $this->Format($this->config()->nice_format);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,6 +16,14 @@
|
|||||||
*/
|
*/
|
||||||
class Time extends DBField {
|
class Time extends DBField {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @config
|
||||||
|
* @see Date::nice_format
|
||||||
|
* @see SS_DateTime::nice_format
|
||||||
|
*/
|
||||||
|
private static $nice_format = 'g:ia';
|
||||||
|
|
||||||
|
|
||||||
public function setValue($value, $record = null) {
|
public function setValue($value, $record = null) {
|
||||||
if($value) {
|
if($value) {
|
||||||
if(preg_match( '/(\d{1,2})[:.](\d{2})([a|A|p|P|][m|M])/', $value, $match )) $this->TwelveHour( $match );
|
if(preg_match( '/(\d{1,2})[:.](\d{2})([a|A|p|P|][m|M])/', $value, $match )) $this->TwelveHour( $match );
|
||||||
@ -26,13 +34,13 @@ class Time extends DBField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a user friendly format for time
|
* Returns the time in the format specified by the config value nice_format, or 12 hour format by default
|
||||||
* in a 12 hour format.
|
* e.g. "3:15pm"
|
||||||
*
|
*
|
||||||
* @return string Time in 12 hour format
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function Nice() {
|
public function Nice() {
|
||||||
if($this->value) return date('g:ia', strtotime($this->value));
|
if($this->value) return $this->Format($this->config()->nice_format);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -59,6 +59,10 @@ class DateTest extends SapphireTest {
|
|||||||
$this->assertEquals('04/03/2003', DBField::create_field('Date', '04-03-2003')->Nice(),
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '04-03-2003')->Nice(),
|
||||||
"Date->Nice() works with DD/MM/YYYY format"
|
"Date->Nice() works with DD/MM/YYYY format"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$date = DBField::create_field('Date', '2003-03-04');
|
||||||
|
Config::inst()->update('Date', 'nice_format', 'd F Y');
|
||||||
|
$this->assertEquals('04 March 2003', $date->Nice());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNiceUS(){
|
public function testNiceUS(){
|
||||||
|
@ -62,6 +62,9 @@ class SS_DatetimeTest extends SapphireTest {
|
|||||||
public function testNice() {
|
public function testNice() {
|
||||||
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
|
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
|
||||||
$this->assertEquals('31/12/2001 10:10pm', $date->Nice());
|
$this->assertEquals('31/12/2001 10:10pm', $date->Nice());
|
||||||
|
|
||||||
|
Config::inst()->update('SS_Datetime', 'nice_format', 'd F Y, H:i:s');
|
||||||
|
$this->assertEquals('31 December 2001, 22:10:59', $date->Nice());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNice24() {
|
public function testNice24() {
|
||||||
|
16
tests/model/TimeTest.php
Normal file
16
tests/model/TimeTest.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @package framework
|
||||||
|
* @subpackage tests
|
||||||
|
*/
|
||||||
|
class TimeTest extends SapphireTest {
|
||||||
|
|
||||||
|
public function testNice() {
|
||||||
|
$time = DBField::create_field('Time', '17:15:55');
|
||||||
|
$this->assertEquals('5:15pm', $time->Nice());
|
||||||
|
|
||||||
|
Config::inst()->update('Time', 'nice_format', 'H:i:s');
|
||||||
|
$this->assertEquals('17:15:55', $time->Nice());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user