Permalink
Browse files

Merge pull request #445 from yukiawano/extendedDateBugFix

BUGFIX Changed Date.php to use DateTime->Format instead of strtotime(fixes #7311)
  • Loading branch information...
halkyon committed May 18, 2012
2 parents 7c589f0 + c04b01f commit d58e37417f569f3ed5ef645d135c459471d246da
Showing with 111 additions and 19 deletions.
  1. +21 −11 model/fieldtypes/Date.php
  2. +15 −7 model/fieldtypes/Datetime.php
  3. +1 −1 tests/forms/GridFieldTest.php
  4. +36 −0 tests/model/DateTest.php
  5. +38 −0 tests/model/DatetimeTest.php
View
@@ -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');
}
/**
@@ -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');
}
/**
@@ -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);
+ }
}
/**
@@ -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() {
@@ -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) {
@@ -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);
}
/**
View
@@ -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"
@@ -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', '');
@@ -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.