/** * Converts date string/object into IMAP date/time format */ protected function date_format($date) { if (empty($date)) { return null; } if (!is_object($date) || !is_a($date, 'DateTime')) { try { $timestamp = rcube_utils::strtotime($date); $date = new DateTime("@" . $timestamp); } catch (Exception $e) { return null; } } return $date->format('d-M-Y H:i:s O'); }
/** * Convert the given date to a human readable form * This uses the date formatting properties from config * * @param mixed Date representation (string, timestamp or DateTime object) * @param string Date format to use * @param bool Enables date convertion according to user timezone * * @return string Formatted date string */ public function format_date($date, $format = null, $convert = true) { if (is_object($date) && is_a($date, 'DateTime')) { $timestamp = $date->format('U'); } else { if (!empty($date)) { $timestamp = rcube_utils::strtotime($date); } if (empty($timestamp)) { return ''; } try { $date = new DateTime("@" . $timestamp); } catch (Exception $e) { return ''; } } if ($convert) { try { // convert to the right timezone $stz = date_default_timezone_get(); $tz = new DateTimeZone($this->config->get('timezone')); $date->setTimezone($tz); date_default_timezone_set($tz->getName()); $timestamp = $date->format('U'); } catch (Exception $e) { } } // define date format depending on current time if (!$format) { $now = time(); $now_date = getdate($now); $today_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday'], $now_date['year']); $week_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday'] - 6, $now_date['year']); $pretty_date = $this->config->get('prettydate'); if ($pretty_date && $timestamp > $today_limit && $timestamp <= $now) { $format = $this->config->get('date_today', $this->config->get('time_format', 'H:i')); $today = true; } else { if ($pretty_date && $timestamp > $week_limit && $timestamp <= $now) { $format = $this->config->get('date_short', 'D H:i'); } else { $format = $this->config->get('date_long', 'Y-m-d H:i'); } } } // strftime() format if (preg_match('/%[a-z]+/i', $format)) { $format = strftime($format, $timestamp); if ($stz) { date_default_timezone_set($stz); } return $today ? $this->gettext('today') . ' ' . $format : $format; } // parse format string manually in order to provide localized weekday and month names // an alternative would be to convert the date() format string to fit with strftime() $out = ''; for ($i = 0; $i < strlen($format); $i++) { if ($format[$i] == "\\") { // skip escape chars continue; } // write char "as-is" if ($format[$i] == ' ' || $format[$i - 1] == "\\") { $out .= $format[$i]; } else { if ($format[$i] == 'D') { $out .= $this->gettext(strtolower(date('D', $timestamp))); } else { if ($format[$i] == 'l') { $out .= $this->gettext(strtolower(date('l', $timestamp))); } else { if ($format[$i] == 'M') { $out .= $this->gettext(strtolower(date('M', $timestamp))); } else { if ($format[$i] == 'F') { $out .= $this->gettext('long' . strtolower(date('M', $timestamp))); } else { if ($format[$i] == 'x') { $out .= strftime('%x %X', $timestamp); } else { $out .= date($format[$i], $timestamp); } } } } } } } if ($today) { $label = $this->gettext('today'); // replcae $ character with "Today" label (#1486120) if (strpos($out, '$') !== false) { $out = preg_replace('/\\$/', $label, $out, 1); } else { $out = $label . ' ' . $out; } } if ($stz) { date_default_timezone_set($stz); } return $out; }
/** * rcube:utils::strtotime() */ function test_strtotime() { // this test depends on system timezone if not set date_default_timezone_set('UTC'); $test = array('1' => 1, '' => 0, 'abc-555' => 0, '2013-04-22' => 1366588800, '2013/04/22' => 1366588800, '2013.04.22' => 1366588800, '22-04-2013' => 1366588800, '22/04/2013' => 1366588800, '22.04.2013' => 1366588800, '22.4.2013' => 1366588800, '20130422' => 1366588800, '2013/06/21 12:00:00 UTC' => 1371816000, '2013/06/21 12:00:00 Europe/Berlin' => 1371808800); foreach ($test as $datetime => $ts) { $result = rcube_utils::strtotime($datetime); $this->assertSame($ts, $result, "Error parsing date: {$datetime}"); } }
/** * Setter for address record fields * * @param string Field name * @param string Field value * @param string Type/section name */ public function set($field, $value, $type = 'HOME') { $field = strtolower($field); $type_uc = strtoupper($type); switch ($field) { case 'name': case 'displayname': $this->raw['FN'][0][0] = $this->displayname = $value; break; case 'surname': $this->raw['N'][0][0] = $this->surname = $value; break; case 'firstname': $this->raw['N'][0][1] = $this->firstname = $value; break; case 'middlename': $this->raw['N'][0][2] = $this->middlename = $value; break; case 'prefix': $this->raw['N'][0][3] = $value; break; case 'suffix': $this->raw['N'][0][4] = $value; break; case 'nickname': $this->raw['NICKNAME'][0][0] = $this->nickname = $value; break; case 'organization': $this->raw['ORG'][0][0] = $this->organization = $value; break; case 'photo': if (strpos($value, 'http:') === 0) { // TODO: fetch file from URL and save it locally? $this->raw['PHOTO'][0] = array(0 => $value, 'url' => true); } else { $this->raw['PHOTO'][0] = array(0 => $value, 'base64' => (bool) preg_match('![^a-z0-9/=+-]!i', $value)); } break; case 'email': $this->raw['EMAIL'][] = array(0 => $value, 'type' => array_filter(array('INTERNET', $type_uc))); $this->email[] = $value; break; case 'im': // save IM subtypes into extension fields $typemap = array_flip($this->immap); if ($field = $typemap[strtolower($type)]) { $this->raw[$field][] = array(0 => $value); } break; case 'birthday': case 'anniversary': if (($val = rcube_utils::strtotime($value)) && ($fn = self::$fieldmap[$field])) { $this->raw[$fn][] = array(0 => date('Y-m-d', $val), 'value' => array('date')); } break; case 'address': if ($this->addresstypemap[$type_uc]) { $type = $this->addresstypemap[$type_uc]; } $value = $value[0] ? $value : array('', '', $value['street'], $value['locality'], $value['region'], $value['zipcode'], $value['country']); // fall through if not empty if (!strlen(join('', $value))) { break; } default: if ($field == 'phone' && $this->phonetypemap[$type_uc]) { $type = $this->phonetypemap[$type_uc]; } if (($tag = self::$fieldmap[$field]) && (is_array($value) || strlen($value))) { $index = count($this->raw[$tag]); $this->raw[$tag][$index] = (array) $value; if ($type) { $typemap = array_flip($this->typemap); $this->raw[$tag][$index]['type'] = explode(',', $typemap[$type_uc] ? $typemap[$type_uc] : $type); } } break; } }
function rcube_strtotime($date) { return rcube_utils::strtotime($date); }
/** * rcube:utils::strtotime() */ function test_strtotime() { $test = array('1' => 1, '' => 0, '2013-04-22' => 1366581600, '2013/04/22' => 1366581600, '2013.04.22' => 1366581600, '22-04-2013' => 1366581600, '22/04/2013' => 1366581600, '22.04.2013' => 1366581600, '22.4.2013' => 1366581600, '20130422' => 1366581600); foreach ($test as $datetime => $ts) { $result = rcube_utils::strtotime($datetime); $this->assertSame($ts, $result, "Error parsing date: {$datetime}"); } }
/** * Sort messages by specified header field * * @param array $messages Array of rcube_message_header objects * @param string $field Name of the property to sort by * @param string $flag Sorting order (ASC|DESC) * * @return array Sorted input array */ public static function sortHeaders($messages, $field, $flag) { // Strategy: First, we'll create an "index" array. // Then, we'll use sort() on that array, and use that to sort the main array. $field = empty($field) ? 'uid' : strtolower($field); $flag = empty($flag) ? 'ASC' : strtoupper($flag); $index = array(); $result = array(); reset($messages); while (list($key, $headers) = each($messages)) { $value = null; switch ($field) { case 'arrival': $field = 'internaldate'; case 'date': case 'internaldate': case 'timestamp': $value = rcube_utils::strtotime($headers->{$field}); if (!$value && $field != 'timestamp') { $value = $headers->timestamp; } break; default: // @TODO: decode header value, convert to UTF-8 $value = $headers->{$field}; if (is_string($value)) { $value = str_replace('"', '', $value); if ($field == 'subject') { $value = preg_replace('/^(Re:\\s*|Fwd:\\s*|Fw:\\s*)+/i', '', $value); } $data = strtoupper($value); } } $index[$key] = $value; } if (!empty($index)) { // sort index if ($flag == 'ASC') { asort($index); } else { arsort($index); } // form new array based on index while (list($key, $val) = each($index)) { $result[$key] = $messages[$key]; } } return $result; }
/** * Compare search value with contact data * * @param string $colname Data name * @param string|array $value Data value * @param string $search Search value * @param int $mode Search mode * * @return bool Comparision result */ protected function compare_search_value($colname, $value, $search, $mode) { // The value is a date string, for date we'll // use only strict comparison (mode = 1) // @TODO: partial search, e.g. match only day and month if (in_array($colname, $this->date_cols)) { return ($value = rcube_utils::strtotime($value)) && ($search = rcube_utils::strtotime($search)) && date('Ymd', $value) == date('Ymd', $search); } // composite field, e.g. address foreach ((array) $value as $val) { $val = mb_strtolower($val); switch ($mode) { case 1: $got = $val == $search; break; case 2: $got = $search == substr($val, 0, strlen($search)); break; default: $got = strpos($val, $search) !== false; } if ($got) { return true; } } return false; }
function rcube_strtotime($date) { _deprecation_warning(__FUNCTION__); return rcube_utils::strtotime($date); }