BUGFIX Changed Date.php and DateTime.php to use DateTime->Format instead of strtotime to handle extended dates on 32bit environment(fixes #7311)

This commit is contained in:
Yuki Awano 2012-05-16 00:36:24 +09:00
parent 7dc2d61871
commit c04b01fc2a
5 changed files with 111 additions and 19 deletions

View File

@ -46,7 +46,14 @@ class Date extends DBField {
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;
}
}
}
@ -54,42 +61,42 @@ class Date extends DBField {
* 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,7 +108,7 @@ class Date extends DBField {
if($this->value) {
$format = 'j';
if ($includeOrdinal) $format .= 'S';
return date($format, strtotime($this->value));
return $this->Format($format);
}
}
@ -109,14 +116,14 @@ class Date extends DBField {
* 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 @@ class Date extends DBField {
* @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);
}
}
/**

View File

@ -40,7 +40,15 @@ class SS_Datetime extends Date {
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;
}
}
}
@ -48,23 +56,23 @@ class SS_Datetime extends Date {
* 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 @@ class SS_Datetime extends Date {
}
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) {

View File

@ -265,7 +265,7 @@ class GridFieldTest extends SapphireTest {
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 File

@ -61,6 +61,36 @@ class DateTest extends SapphireTest {
);
}
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 @@ class DateTest extends SapphireTest {
"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', '');

View File

@ -50,5 +50,43 @@ class SS_DatetimeTest extends SapphireTest {
$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());
}
}