2007-08-18 04:33:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Represents a date-time field.
|
2009-04-02 19:17:04 +02:00
|
|
|
* The field currently supports New Zealand date format (DD/MM/YYYY),
|
|
|
|
* or an ISO 8601 formatted date and time (Y-m-d H:i:s).
|
|
|
|
* Alternatively you can set a timestamp that is evaluated through
|
|
|
|
* PHP's built-in date() and strtotime() function according to your system locale.
|
|
|
|
*
|
2009-06-18 02:10:02 +02:00
|
|
|
* For all computations involving the current date and time,
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* please use {@link SS_Datetime::now()} instead of PHP's built-in date() and time()
|
2009-06-18 02:10:02 +02:00
|
|
|
* methods. This ensures that all time-based computations are testable with mock dates
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* through {@link SS_Datetime::set_mock_now()}.
|
2009-06-18 02:10:02 +02:00
|
|
|
*
|
2010-10-15 01:58:09 +02:00
|
|
|
* Example definition via {@link DataObject::$db}:
|
|
|
|
* <code>
|
|
|
|
* static $db = array(
|
2010-10-19 06:53:11 +02:00
|
|
|
* "Expires" => "SS_Datetime",
|
2010-10-15 01:58:09 +02:00
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
2009-04-02 19:17:04 +02:00
|
|
|
* @todo Add localization support, see http://open.silverstripe.com/ticket/2931
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
|
|
|
*/
|
2012-03-16 02:16:07 +01:00
|
|
|
class SS_Datetime extends Date implements TemplateGlobalProvider {
|
2009-05-19 02:35:15 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setValue($value, $record = null) {
|
2012-02-24 04:18:36 +01:00
|
|
|
if($value === false || $value === null || (is_string($value) && !strlen($value))) {
|
2012-09-26 23:34:00 +02:00
|
|
|
// don't try to evaluate empty values with strtotime() below, as it returns "1970-01-01" when it should be
|
|
|
|
// saved as NULL in database
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->value = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-18 23:24:33 +02:00
|
|
|
// Default to NZ date format - strtotime expects a US date
|
2012-02-27 22:14:02 +01:00
|
|
|
if(preg_match('#^([0-9]+)/([0-9]+)/([0-9]+)$#', $value, $parts)) {
|
2009-05-18 23:24:33 +02:00
|
|
|
$value = "$parts[2]/$parts[1]/$parts[3]";
|
2009-05-19 02:35:15 +02:00
|
|
|
}
|
2012-02-27 22:14:02 +01:00
|
|
|
|
2009-05-19 02:35:15 +02:00
|
|
|
if(is_numeric($value)) {
|
|
|
|
$this->value = date('Y-m-d H:i:s', $value);
|
|
|
|
} elseif(is_string($value)) {
|
2012-05-15 17:36:24 +02:00
|
|
|
// $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;
|
|
|
|
}
|
2009-05-19 02:35:15 +02:00
|
|
|
}
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
|
2010-10-15 01:58:09 +02:00
|
|
|
/**
|
|
|
|
* Returns the date in the raw SQL-format, e.g. “2006-01-18 16:32:04”
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Nice() {
|
2012-05-15 17:36:24 +02:00
|
|
|
if($this->value) return $this->Format('d/m/Y g:ia');
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2012-02-24 04:18:36 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Nice24() {
|
2012-05-15 17:36:24 +02:00
|
|
|
if($this->value) return $this->Format('d/m/Y H:i');
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2012-02-24 04:18:36 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Date() {
|
2012-05-15 17:36:24 +02:00
|
|
|
if($this->value) return $this->Format('d/m/Y');
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2012-02-24 04:18:36 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Time() {
|
2012-05-15 17:36:24 +02:00
|
|
|
if($this->value) return $this->Format('g:ia');
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2012-02-24 04:18:36 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Time24() {
|
2012-05-15 17:36:24 +02:00
|
|
|
if($this->value) return $this->Format('H:i');
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function requireField() {
|
2009-09-30 00:01:41 +02:00
|
|
|
$parts=Array('datatype'=>'datetime', 'arrayValue'=>$this->arrayValue);
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
$values=Array('type'=>'SS_Datetime', 'parts'=>$parts);
|
2008-11-23 02:01:03 +01:00
|
|
|
DB::requireField($this->tableName, $this->name, $values);
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function URLDatetime() {
|
2012-05-15 17:36:24 +02:00
|
|
|
if($this->value) return $this->Format('Y-m-d%20H:i:s');
|
2007-10-29 01:55:52 +01:00
|
|
|
}
|
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
FEATURE New DatetimeField class (form field wrapper composed of DateField andTimeField)
FEATURE New DateField and TimeField form classes with more consistent API and easier localization
API CHANGE Date/time parsing in DateField, TimeField and DatetimeField defaults to i18n::get_locale() ('en_US') instead of using en_NZ/en_GB specific parsing. Use i18n::set_locale('en_NZ') in mysite/_config.php to revert to old behaviour.
API CHANGE constructor parameter in TimeField needs to be in ISO date notation (not PHP's date())
API CHANGE TimeField, DateField and related subclasses use Zend_Date for date parsing, meaning they're stricer than the previously used strtotime()
API CHANGE Removed DMYCalendarDateField and CalendarDateField, use DateField with setConfig('showcalendar')
API CHANGE Removed CompositeDateField, DMYDateField, use DateField with setConfig('dmyfields')
API CHANGE Removed DropdownTimeField, use TimeField with setConfig('showdropdown')
API CHANGE Removed PopupDateTimeField, use DatetimeField
API CHANGE Changed 'date', 'month' and 'year' HTML field names to lowercase in DMYDateField
API CHANGE Removed support for ambiguous date formats in DateField, e.g. '06/03/03'. Use DateField->setConfig('dateformat', '<format>') to revert to this behaviour.
API CHANGE Removed flag from DateField, CalendarDateField etc., use DateField->setConfig('min') and DateField->setConfig('max')
ENHANCEMENT Using Zend_Date for DateField and TimeField, with more robust date handling, starting localization support. Set globally via i18n::set_locale(), or for a field instance through setLocale(). Note: Javascript validation is not localized yet. (from r99360)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102859 467b73ca-7a2a-4603-9d3b-597d59a354a9
2010-04-14 06:38:40 +02:00
|
|
|
return new DatetimeField($this->name, $title);
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2009-06-18 02:10:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected static $mock_now = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns either the current system date as determined
|
|
|
|
* by date(), or a mocked date through {@link set_mock_now()}.
|
|
|
|
*
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @return SS_Datetime
|
2009-06-18 02:10:02 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function now() {
|
2009-06-18 02:10:02 +02:00
|
|
|
if(self::$mock_now) {
|
|
|
|
return self::$mock_now;
|
|
|
|
} else {
|
2012-03-27 06:57:42 +02:00
|
|
|
return DBField::create_field('SS_Datetime', date('Y-m-d H:i:s'));
|
2009-06-18 02:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock the system date temporarily, which is useful for time-based unit testing.
|
|
|
|
* Use {@link clear_mock_now()} to revert to the current system date.
|
|
|
|
* Caution: This sets a fixed date that doesn't increment with time.
|
|
|
|
*
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @param SS_Datetime|string $datetime Either in object format, or as a SS_Datetime compatible string.
|
2009-06-18 02:10:02 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_mock_now($datetime) {
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
if($datetime instanceof SS_Datetime) {
|
2009-06-18 02:10:02 +02:00
|
|
|
self::$mock_now = $datetime;
|
|
|
|
} elseif(is_string($datetime)) {
|
2012-03-27 06:57:42 +02:00
|
|
|
self::$mock_now = DBField::create_field('SS_Datetime', $datetime);
|
2009-06-18 02:10:02 +02:00
|
|
|
} else {
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
throw new Exception('SS_Datetime::set_mock_now(): Wrong format: ' . $datetime);
|
2009-06-18 02:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear any mocked date, which causes
|
|
|
|
* {@link Now()} to return the current system date.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function clear_mock_now() {
|
2009-06-18 02:10:02 +02:00
|
|
|
self::$mock_now = null;
|
|
|
|
}
|
2012-03-16 02:16:07 +01:00
|
|
|
|
|
|
|
public static function get_template_global_variables() {
|
|
|
|
return array(
|
|
|
|
'Now' => array('method' => 'now', 'casting' => 'SS_Datetime'),
|
|
|
|
);
|
|
|
|
}
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
|