Permalink
Browse files

Improve date and time parsing to understand localized dates and respe…

…ct formats stored in the configuration
  • Loading branch information...
1 parent b9da804 commit e10a309c21f7d240d60c5b188b49e1d0908cf649 @nilshoerrmann nilshoerrmann committed Mar 13, 2011
@@ -802,7 +802,7 @@ public function __viewInfo(){
case 'version':
$fieldset = new XMLElement('fieldset');
$fieldset->appendChild(new XMLElement('legend', __('Version')));
- $fieldset->appendChild(new XMLElement('p', $value . ', ' . __('released on') . ' ' . Lang::localizeDate(DateTimeObj::get(__SYM_DATE_FORMAT__, strtotime(Lang::standardizeDate($about['release-date']))))));
+ $fieldset->appendChild(new XMLElement('p', $value . ', ' . __('released on') . ' ' . DateTimeObj::format(__SYM_DATE_FORMAT__, $about['release-date'], true)));
break;
case 'description':
@@ -58,7 +58,7 @@ public function __viewIndex(){
if(!is_null($a->get('last_seen'))) {
$td3 = Widget::TableData(
- Lang::localizeDate(DateTimeObj::get(__SYM_DATETIME_FORMAT__, strtotime(Lang::standardizeDate($a->get('last_seen')))))
+ DateTimeObj::format(__SYM_DATETIME_FORMAT__, $a->get('last_seen'), true)
);
} else {
$td3 = Widget::TableData('Unknown', 'inactive');
@@ -27,12 +27,16 @@ public static function setDefaultTimezone($timezone){
/**
* Given a `$format`, and a `$timestamp`,
- * return the date in the format provided. This function is a basic wrapper
- * for PHP's date function. If the `$timestamp` is omitted,
+ * return the date in the format provided. This function is a basic
+ * wrapper for PHP's DateTime object. If the `$timestamp` is omitted,
* the current timestamp will be used. Optionally, you pass a
* timezone identifier with this function to localise the output
*
- * @link http://www.php.net/manual/en/function.date.php
+ * If you like to display a date in the backend, please make use
+ * of `DateTimeObj::format()` which allows date and time localization
+ *
+ * @see class.datetimeobj.php#format()
+ * @link http://www.php.net/manual/en/book.datetime.php
* @param string $format
* A valid PHP date format
* @param integer $timestamp (optional)
@@ -41,21 +45,79 @@ public static function setDefaultTimezone($timezone){
* @param string $timezone (optional)
* The timezone associated with the timestamp
* @return string
- * The formatted date.
+ * The formatted date
*/
- public static function get($format, $timestamp = null, $timezone = null){
- if(is_null($timestamp) || $timestamp == 'now') $timestamp = time();
- if(is_null($timezone)) $timezone = date_default_timezone_get();
-
- $current_timezone = date_default_timezone_get();
-
- if($current_timezone != $timezone) self::setDefaultTimezone($timezone);
-
- $ret = date($format, $timestamp);
-
- if($current_timezone != $timezone) self::setDefaultTimezone($current_timezone);
+ public static function get($format, $timestamp = 'now', $timezone = null) {
+
+ // Parse date
+ if(ctype_digit($timestamp)) {
+ $timestamp = '@' . $timestamp;
+ }
+ $date = new DateTime($timestamp);
+
+ // Timezone
+ if($timezone !== null) {
+ $date->setTimezone(new DateTimeZone($timezone));
+ }
+
+ // Format date
+ return $date->format($format);
+ }
+
+ /**
+ * Formats the given date and time `$string` based on the given `$format`.
+ * Optionally the result will be localized and respect a timezone differing
+ * from the system default. The default output is ISO 8601.
+ *
+ * @since Symphony 2.2.1
+ * @param string $string (optional)
+ * A string containing date and time, defaults to the current date and time
+ * @param string $format (optional)
+ * A valid PHP date format, defaults to ISO 8601
+ * @param boolean $localize (optional)
+ * Localizes the output, if true, defaults to false
+ * @param string $timezone (optional)
+ * The timezone associated with the timestamp
+ * @return string
+ * The formatted date
+ */
+ public static function format($string = 'now', $format = 'c', $localize = false, $timezone = null) {
+
+ // Timestamp
+ if(ctype_digit($string)) {
+ $date = DateTime::createFromFormat('U', $string);
+ }
+
+ // Date string
+ else {
+
+ // Standardize date
+ $string = Lang::standardizeDate($string);
+
+ // Apply system date format
+ $date = DateTime::createFromFormat(
+ Symphony::$Configuration->get('date_format', 'region') .
+ Symphony::$Configuration->get('datetime_separator', 'region') .
+ Symphony::$Configuration->get('time_format', 'region'),
+ $string
+ );
+
+ // Handle non-standard dates
+ if($date === false) {
+ $date = new DateTime($string);
+ }
+ }
+
+ // Format date
+ $date = $date->format($format);
- return $ret;
+ // Localize date
+ if($localize === true) {
+ $date = Lang::localizeDate($date);
+ }
+
+ // Return custom formatted date, use ISO 8601 date by default
+ return $date;
}
/**
@@ -64,12 +126,12 @@ public static function get($format, $timestamp = null, $timezone = null){
* @param string $format
* A valid PHP date format
* @param integer $timestamp (optional)
- * A unix timestamp to format. 'now' or omitting this parameter will
+ * A unix timestamp to format. Omitting this parameter will
* result in the current time being used
* @return string
* The formatted date in GMT
*/
- public static function getGMT($format, $timestamp=NULL){
+ public static function getGMT($format, $timestamp = 'now'){
return self::get($format, $timestamp, 'GMT');
}
@@ -88,7 +150,7 @@ public static function getGMT($format, $timestamp=NULL){
* specified by the `$format`.
*/
public static function getTimeAgo($format){
- return '<abbr class="timeago" title="'.self::get('r').'">'.self::get($format).'</abbr>';
+ return '<abbr class="timeago" title="' . self::get('r') . '">' . self::get($format) . '</abbr>';
}
}
@@ -531,21 +531,19 @@ public static function localizeDate($string) {
*/
public static function standardizeDate($string) {
- // Get date and time separator
- $separator = Symphony::$Configuration->get('datetime_separator', 'region');
-
// Only standardize dates in localized environments
if(self::isLocalized()) {
+
+ // Translate names to English
foreach(self::$_dates as $english => $locale) {
-
- // Translate names to English
$string = str_replace($locale, $english, $string);
+ }
- // Replace custom date and time separator with space:
- // This is important, otherwise PHP's strtotime() function may break
- if($separator != ' ') {
- $string = str_replace($separator, ' ', $string);
- }
+ // Replace custom date and time separator with space:
+ // This is important, otherwise PHP's strtotime() function may break
+ $separator = Symphony::$Configuration->get('datetime_separator', 'region');
+ if($separator != ' ') {
+ $string = str_replace($separator, ' ', $string);
}
}
@@ -38,7 +38,7 @@ function processRecordGroup(&$wrapper, $element, $group, $ds, &$entryManager, &$
if(isset($ds->dsParamPARAMOUTPUT)){
if($ds->dsParamPARAMOUTPUT == 'system:id') $param_pool[$key][] = $entry->get('id');
- elseif($ds->dsParamPARAMOUTPUT == 'system:date') $param_pool[$key][] = DateTimeObj::get('c', strtotime(Lang::standardizeDate($entry->creationDate)));
+ elseif($ds->dsParamPARAMOUTPUT == 'system:date') $param_pool[$key][] = DateTimeObj::format($entry->creationDate);
elseif($ds->dsParamPARAMOUTPUT == 'system:author') $param_pool[$key][] = $entry->get('author_id');
}
@@ -70,7 +70,12 @@ function processRecordGroup(&$wrapper, $element, $group, $ds, &$entryManager, &$
if(!$param_output_only){
if(is_array($ds->dsParamINCLUDEDELEMENTS) && in_array('system:date', $ds->dsParamINCLUDEDELEMENTS)){
- $xEntry->appendChild(General::createXMLDateObject(strtotime(Lang::standardizeDate($entry->creationDate)), 'system-date'));
+ $xEntry->appendChild(
+ General::createXMLDateObject(
+ DateTimeObj::format($entry->creationDate, 'U'),
+ 'system-date'
+ )
+ );
}
$xGroup->appendChild($xEntry);
}
@@ -284,7 +289,7 @@ function processRecordGroup(&$wrapper, $element, $group, $ds, &$entryManager, &$
if(isset($this->dsParamPARAMOUTPUT)){
if($this->dsParamPARAMOUTPUT == 'system:id') $param_pool[$key][] = $entry->get('id');
- elseif($this->dsParamPARAMOUTPUT == 'system:date') $param_pool[$key][] = DateTimeObj::get('c', strtotime(Lang::standardizeDate($entry->creationDate)));
+ elseif($this->dsParamPARAMOUTPUT == 'system:date') $param_pool[$key][] = DateTimeObj::format($entry->creationDate);
elseif($this->dsParamPARAMOUTPUT == 'system:author') $param_pool[$key][] = $entry->get('author_id');
}
@@ -317,7 +322,12 @@ function processRecordGroup(&$wrapper, $element, $group, $ds, &$entryManager, &$
if($this->_param_output_only) continue;
if(in_array('system:date', $this->dsParamINCLUDEDELEMENTS)){
- $xEntry->appendChild(General::createXMLDateObject(Lang::standardizeDate(strtotime($entry->creationDate)), 'system-date'));
+ $xEntry->appendChild(
+ General::createXMLDateObject(
+ DateTimeObj::format($entry->creationDate, 'U'),
+ 'system-date'
+ )
+ );
}
$result->appendChild($xEntry);
@@ -53,7 +53,7 @@ function displayPublishPanel(&$wrapper, $data = null, $error = null, $prefix = n
// New entry:
if (is_null($data) && is_null($error) && $this->get('pre_populate') == 'yes') {
- $value = Lang::localizeDate(DateTimeObj::get(__SYM_DATETIME_FORMAT__, null));
+ $value = DateTimeObj::format('now', __SYM_DATETIME_FORMAT__, true);
}
// Error entry, display original data:
@@ -63,7 +63,7 @@ function displayPublishPanel(&$wrapper, $data = null, $error = null, $prefix = n
// Empty entry:
else if (isset($data['gmt']) && !is_null($data['gmt'])) {
- $value = Lang::localizeDate(DateTimeObj::get(__SYM_DATETIME_FORMAT__, $data['gmt']));
+ $value = DateTimeObj::format($data['gmt'], __SYM_DATETIME_FORMAT__, true);
}
$label = Widget::Label($this->get('label'));
@@ -95,14 +95,14 @@ public function processRawFieldData($data, &$status, $simulate=false, $entry_id=
if (is_null($data) || $data == '') {
if ($this->get('pre_populate') == 'yes') {
- $timestamp = strtotime(Lang::standardizeDate(DateTimeObj::get(__SYM_DATETIME_FORMAT__, null)));
+ $timestamp = time();
}
}
else if ($status == self::__OK__) {
- $timestamp = strtotime(Lang::standardizeDate($data));
+ $timestamp = DateTimeObj::format($data, 'U');
}
- if (!is_null($timestamp)) {
+ if(!is_null($timestamp)) {
return array(
'value' => DateTimeObj::get('c', $timestamp),
'local' => strtotime(DateTimeObj::get('c', $timestamp)),
@@ -133,7 +133,7 @@ public function prepareTableValue($data, XMLElement $link=NULL) {
$value = null;
if (isset($data['gmt']) && !is_null($data['gmt'])) {
- $value = Lang::localizeDate(DateTimeObj::get(__SYM_DATETIME_FORMAT__, $data['gmt']));
+ $value = DateTimeObj::format( $data['gmt'], __SYM_DATETIME_FORMAT__, true);
}
return parent::prepareTableValue(array('value' => $value), $link);
@@ -965,7 +965,7 @@ function index(&$Page, &$Contents, $fields){
if(strpos($dateOption, 'j') !== false || strpos($dateOption, 'n') !== false) {
$leadingZero = ' (' . __('no leading zeros') . ')';
}
- $dateOptions[] = array($dateOption, $dateformat == $dateOption, Lang::localizeDate(DateTimeObj::get($dateOption), true) . $leadingZero);
+ $dateOptions[] = array($dateOption, $dateformat == $dateOption, DateTimeObj::format('now', $dateOption, true) . $leadingZero);
}
$label->appendChild(Widget::Select('fields[region][date_format]', $dateOptions));

0 comments on commit e10a309

Please sign in to comment.