Skip to content

Commit

Permalink
Merge pull request #445 from yukiawano/extendedDateBugFix
Browse files Browse the repository at this point in the history
BUGFIX Changed Date.php to use DateTime->Format instead of strtotime(fixes #7311)
  • Loading branch information
Sean Harvey committed May 18, 2012
2 parents 7c589f0 + c04b01f commit d58e374
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 19 deletions.
32 changes: 21 additions & 11 deletions model/fieldtypes/Date.php
Expand Up @@ -46,50 +46,57 @@ function setValue($value, $record = null) {
if(is_numeric($value)) {
$this->value = date('Y-m-d', $value);
} elseif(is_string($value)) {
$this->value = date('Y-m-d', strtotime($value));
try{
$date = new DateTime($value);
$this->value = $date->Format('Y-m-d');
return;
}catch(Exception $e){
$this->value = null;
return;
}
}
}

/**
* Returns the date in the format dd/mm/yy
*/
function Nice() {
if($this->value) return date('d/m/Y', strtotime($this->value));
if($this->value) return $this->Format('d/m/Y');
}

/**
* Returns the date in US format: “01/18/2006”
*/
function NiceUS() {
if($this->value) return date('m/d/Y', strtotime($this->value));
if($this->value) return $this->Format('m/d/Y');
}

/**
* Returns the year from the given date
*/
function Year() {
if($this->value) return date('Y', strtotime($this->value));
if($this->value) return $this->Format('Y');
}

/**
* Returns the Full day, of the given date.
*/
function Day(){
if($this->value) return date('l', strtotime($this->value));
if($this->value) return $this->Format('l');
}

/**
* Returns a full textual representation of a month, such as January.
*/
function Month() {
if($this->value) return date('F', strtotime($this->value));
if($this->value) return $this->Format('F');
}

/**
* Returns the short version of the month such as Jan
*/
function ShortMonth() {
if($this->value) return date('M', strtotime($this->value));
if($this->value) return $this->Format('M');
}

/**
Expand All @@ -101,22 +108,22 @@ function DayOfMonth($includeOrdinal = false) {
if($this->value) {
$format = 'j';
if ($includeOrdinal) $format .= 'S';
return date($format, strtotime($this->value));
return $this->Format($format);
}
}

/**
* Returns the date in the format 24 December 2006
*/
function Long() {
if($this->value) return date('j F Y', strtotime($this->value));
if($this->value) return $this->Format('j F Y');
}

/**
* Returns the date in the format 24 Dec 2006
*/
function Full() {
if($this->value) return date('j M Y', strtotime($this->value));
if($this->value) return $this->Format('j M Y');
}

/**
Expand All @@ -126,7 +133,10 @@ function Full() {
* @return string The date in the requested format
*/
function Format($format) {
if($this->value) return date($format, strtotime($this->value));
if($this->value){
$date = new DateTime($this->value);
return $date->Format($format);
}
}

/**
Expand Down
22 changes: 15 additions & 7 deletions model/fieldtypes/Datetime.php
Expand Up @@ -40,31 +40,39 @@ function setValue($value, $record = null) {
if(is_numeric($value)) {
$this->value = date('Y-m-d H:i:s', $value);
} elseif(is_string($value)) {
$this->value = date('Y-m-d H:i:s', strtotime($value));
// $this->value = date('Y-m-d H:i:s', strtotime($value));
try{
$date = new DateTime($value);
$this->value = $date->Format('Y-m-d H:i:s');
return;
}catch(Exception $e){
$this->value = null;
return;
}
}
}

/**
* Returns the date in the raw SQL-format, e.g. “2006-01-18 16:32:04”
*/
function Nice() {
if($this->value) return date('d/m/Y g:ia', strtotime($this->value));
if($this->value) return $this->Format('d/m/Y g:ia');
}

function Nice24() {
if($this->value) return date('d/m/Y H:i', strtotime($this->value));
if($this->value) return $this->Format('d/m/Y H:i');
}

function Date() {
if($this->value) return date('d/m/Y', strtotime($this->value));
if($this->value) return $this->Format('d/m/Y');
}

function Time() {
if($this->value) return date('g:ia', strtotime($this->value));
if($this->value) return $this->Format('g:ia');
}

function Time24() {
if($this->value) return date('H:i', strtotime($this->value));
if($this->value) return $this->Format('H:i');
}

function requireField() {
Expand All @@ -74,7 +82,7 @@ function requireField() {
}

function URLDatetime() {
if($this->value) return date('Y-m-d%20H:i:s', strtotime($this->value));
if($this->value) return $this->Format('Y-m-d%20H:i:s');
}

public function scaffoldFormField($title = null, $params = null) {
Expand Down
2 changes: 1 addition & 1 deletion tests/forms/GridFieldTest.php
Expand Up @@ -265,7 +265,7 @@ public function testGetCastedValue() {
public function testGetCastedValueObject() {
$obj = new GridField('testfield', 'testfield');
$value = $obj->getCastedValue('This is a sentance. This ia another.', 'Date');
$this->assertEquals('1970-01-01', $value);
$this->assertEquals(null, $value);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/model/DateTest.php
Expand Up @@ -61,6 +61,36 @@ function testNiceDate() {
);
}

function testNiceUS(){
$this->assertEquals('03/31/2008', DBField::create_field('Date', 1206968400)->NiceUs(),
"Date->NiceUs() works with timestamp integers"
);
}

function testYear(){
$this->assertEquals('2008', DBField::create_field('Date', 1206968400)->Year(),
"Date->Year() works with timestamp integers"
);
}

function testDay(){
$this->assertEquals('Monday', DBField::create_field('Date', 1206968400)->Day(),
"Date->Day() works with timestamp integers"
);
}

function testMonth(){
$this->assertEquals('March', DBField::create_field('Date', 1206968400)->Month(),
"Date->Month() works with timestamp integers"
);
}

function testShortMonth(){
$this->assertEquals('Mar', DBField::create_field('Date', 1206968400)->ShortMonth(),
"Date->ShortMonth() works with timestamp integers"
);
}

function testLongDate() {
$this->assertEquals('31 March 2008', DBField::create_field('Date', 1206968400)->Long(),
"Date->Long() works with numeric timestamp"
Expand All @@ -81,6 +111,12 @@ function testLongDate() {
"Date->Long() works with D/M/YYYY"
);
}

function testFull(){
$this->assertEquals('31 Mar 2008', DBField::create_field('Date', 1206968400)->Full(),
"Date->Full() works with timestamp integers"
);
}

function testSetNullAndZeroValues() {
$date = DBField::create_field('Date', '');
Expand Down
38 changes: 38 additions & 0 deletions tests/model/DatetimeTest.php
Expand Up @@ -50,5 +50,43 @@ function testSetNullAndZeroValues() {
$date = DBField::create_field('SS_Datetime', 0);
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'Numeric zero is UNIX epoch time');
}

function testExtendedDateTimes() {
$date = DBField::create_field('SS_Datetime', '1500-10-10 15:32:24');
$this->assertEquals('10 Oct 1500 15 32 24', $date->Format('d M Y H i s'));

$date = DBField::create_field('SS_Datetime', '3000-10-10 15:32:24');
$this->assertEquals('10 Oct 3000 15 32 24', $date->Format('d M Y H i s'));
}

function testNice() {
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
$this->assertEquals('31/12/2001 10:10pm', $date->Nice());
}

function testNice24() {
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
$this->assertEquals('31/12/2001 22:10', $date->Nice24());
}

function testDate() {
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
$this->assertEquals('31/12/2001', $date->Date());
}

function testTime() {
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
$this->assertEquals('10:10pm', $date->Time());
}

function testTime24() {
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
$this->assertEquals('22:10', $date->Time24());
}

function testURLDateTime(){
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
$this->assertEquals('2001-12-31%2022:10:59', $date->URLDateTime());
}

}

0 comments on commit d58e374

Please sign in to comment.