2009-02-02 00:49:53 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-02-02 00:49:53 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class DateTest extends SapphireTest {
|
|
|
|
|
2009-04-02 19:16:34 +02:00
|
|
|
protected $originalTZ;
|
2012-03-31 09:08:25 +02:00
|
|
|
|
2009-04-02 19:16:34 +02:00
|
|
|
function setUp() {
|
|
|
|
// Set timezone to support timestamp->date conversion.
|
2012-03-31 09:08:25 +02:00
|
|
|
$this->originalTZ = date_default_timezone_get();
|
|
|
|
date_default_timezone_set('Pacific/Auckland');
|
2009-04-02 19:16:34 +02:00
|
|
|
parent::setUp();
|
|
|
|
}
|
2012-03-31 09:08:25 +02:00
|
|
|
|
2009-04-02 19:16:34 +02:00
|
|
|
function tearDown() {
|
2012-03-31 09:08:25 +02:00
|
|
|
date_default_timezone_set($this->originalTZ);
|
2009-04-02 19:16:34 +02:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
2012-03-31 09:08:25 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
function testNiceDate() {
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('31/03/2008', DBField::create_field('Date', 1206968400)->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with timestamp integers"
|
|
|
|
);
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('30/03/2008', DBField::create_field('Date', 1206882000)->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with timestamp integers"
|
|
|
|
);
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('31/03/2008', DBField::create_field('Date', '1206968400')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with timestamp strings"
|
|
|
|
);
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('30/03/2008', DBField::create_field('Date', '1206882000')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with timestamp strings"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '4/3/03')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with D/M/YY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '04/03/03')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with DD/MM/YY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '4/3/03')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with D/M/YY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '4/03/03')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with D/M/YY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '4/3/2003')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with D/M/YYYY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '4-3-2003')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with D-M-YYYY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '2003-03-04')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with YYYY-MM-DD format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '04/03/2003')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with DD/MM/YYYY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '04-03-2003')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with DD/MM/YYYY format"
|
|
|
|
);
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function testLongDate() {
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('31 March 2008', DBField::create_field('Date', 1206968400)->Long(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Long() works with numeric timestamp"
|
|
|
|
);
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('31 March 2008', DBField::create_field('Date', '1206968400')->Long(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Long() works with string timestamp"
|
|
|
|
);
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('30 March 2008', DBField::create_field('Date', 1206882000)->Long(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Long() works with numeric timestamp"
|
|
|
|
);
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('30 March 2008', DBField::create_field('Date', '1206882000')->Long(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Long() works with numeric timestamp"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('3 April 2003', DBField::create_field('Date', '2003-4-3')->Long(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Long() works with YYYY-M-D"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('3 April 2003', DBField::create_field('Date', '3/4/2003')->Long(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Long() works with D/M/YYYY"
|
|
|
|
);
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2012-02-24 04:18:36 +01:00
|
|
|
|
|
|
|
function testSetNullAndZeroValues() {
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('Date', '');
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->assertNull($date->getValue(), 'Empty string evaluates to NULL');
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('Date', null);
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->assertNull($date->getValue(), 'NULL is set as NULL');
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('Date', false);
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL');
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('Date', array());
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->assertNull($date->getValue(), 'Empty array evaluates to NULL');
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('Date', '0');
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->assertEquals('1970-01-01', $date->getValue(), 'Zero is UNIX epoch date');
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('Date', 0);
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->assertEquals('1970-01-01', $date->getValue(), 'Zero is UNIX epoch date');
|
|
|
|
}
|
|
|
|
|
2012-02-24 04:49:41 +01:00
|
|
|
function testDayOfMonth() {
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('Date', '2000-10-10');
|
2012-02-24 04:49:41 +01:00
|
|
|
$this->assertEquals('10', $date->DayOfMonth());
|
|
|
|
$this->assertEquals('10th', $date->DayOfMonth(true));
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$range = $date->RangeString(DBField::create_field('Date', '2000-10-20'));
|
2012-02-24 04:49:41 +01:00
|
|
|
$this->assertEquals('10 - 20 Oct 2000', $range);
|
2012-03-27 06:57:42 +02:00
|
|
|
$range = $date->RangeString(DBField::create_field('Date', '2000-10-20'), true);
|
2012-02-24 04:49:41 +01:00
|
|
|
$this->assertEquals('10th - 20th Oct 2000', $range);
|
|
|
|
}
|
2012-04-12 06:38:03 +02:00
|
|
|
|
|
|
|
function testExtendedDates() {
|
|
|
|
$date = DBField::create_field('Date', '1800-10-10');
|
|
|
|
$this->assertEquals('10 Oct 1800', $date->Format('d M Y'));
|
|
|
|
|
|
|
|
$date = DBField::create_field('Date', '1500-10-10');
|
|
|
|
$this->assertEquals('10 Oct 1500', $date->Format('d M Y'));
|
|
|
|
|
|
|
|
$date = DBField::create_field('Date', '3000-4-3');
|
|
|
|
$this->assertEquals('03 Apr 3000', $date->Format('d M Y'));
|
|
|
|
}
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|