View source
<?php
function date_sql_concat($array) {
global $db_type;
switch ($db_type) {
case 'mysql':
case 'mysqli':
return "CONCAT(" . implode(",", $array) . ")";
case 'pgsql':
return implode(" || ", $array);
}
}
function date_sql_coalesce($array) {
global $db_type;
switch ($db_type) {
case 'mysql':
case 'mysqli':
case 'pgsql':
return "COALESCE(" . implode(',', $array) . ")";
}
}
function date_sql_pad($str, $size = 2, $pad = '0', $side = 'l') {
switch ($side) {
case 'r':
return "RPAD({$str}, {$size}, '{$pad}')";
default:
return "LPAD({$str}, {$size}, '{$pad}')";
}
}
class date_sql_handler {
var $db_type = 'mysql';
var $date_type = DATE_DATETIME;
var $db_timezone = 'UTC';
var $local_timezone = NULL;
var $db_timezone_field = NULL;
var $local_timezone_field = NULL;
var $offset_field = NULL;
function construct($date_type = DATE_DATETIME, $local_timezone = NULL) {
$this->db_type = $GLOBALS['db_type'];
$this->date_type = $date_type;
$this->db_timezone = 'UTC';
$this->local_timezone = isset($local_timezone) ? $local_timezone : date_default_timezone_name();
if (isset($this->definition['content_field'])) {
$this->date_handler->date_type = $this->definition['content_field']['type'];
}
date_api_set_db_timezone();
}
function db_tz_support($reset = FALSE) {
$has_support = variable_get('date_db_tz_support', -1);
if ($has_support == -1 || $reset) {
date_api_set_db_timezone();
$has_support = FALSE;
switch ($this->db_type) {
case 'mysql':
case 'mysqli':
if (version_compare(db_version(), '4.1.3', '>=')) {
$test = db_result(db_query("SELECT CONVERT_TZ('2008-02-15 12:00:00', 'UTC', 'US/Central')"));
if ($test == '2008-02-15 06:00:00') {
$has_support = TRUE;
}
}
break;
case 'pgsql':
$test = db_result(db_query("SELECT '2008-02-15 12:00:00 UTC' AT TIME ZONE 'US/Central'"));
if ($test == '2008-02-15 06:00:00') {
$has_support = TRUE;
}
break;
}
variable_set('date_db_tz_support', $has_support);
}
return $has_support;
}
function set_db_timezone($offset = '+00:00') {
static $already_set = FALSE;
$type = $GLOBALS['db_type'];
if (!$already_set) {
if (($type == 'mysqli' || $type == 'mysql') && version_compare(db_version(), '4.1.3', '>=')) {
db_query("SET @@session.time_zone = '{$offset}'");
}
elseif ($type == 'pgsql') {
db_query("SET TIME ZONE INTERVAL '{$offset}' HOUR TO MINUTE");
}
$already_set = TRUE;
}
}
function get_offset() {
if (!empty($this->db_timezone) && !empty($this->local_timezone)) {
if ($this->db_timezone != $this->local_timezone) {
$date = date_now($this->db_timezone);
date_timezone_set($date, timezone_open($this->local_timezone));
return date_offset_get($date);
}
}
return 0;
}
function sql_field($field, $offset = NULL) {
if (drupal_strtoupper($field) == 'NOW') {
$this->local_timezone = 'UTC';
return $this
->sql_offset('NOW()', $offset);
}
switch ($this->db_type) {
case 'mysql':
case 'mysqli':
switch ($this->date_type) {
case DATE_UNIX:
$field = "FROM_UNIXTIME({$field})";
break;
case DATE_ISO:
if (version_compare(db_version(), '4.1.1', '>=')) {
$field = "STR_TO_DATE({$field}, '%Y-%m-%%dT%T')";
}
else {
$field = "REPLACE({$field}, 'T', ' ')";
}
break;
case DATE_DATETIME:
break;
}
break;
case 'pgsql':
switch ($this->date_type) {
case DATE_UNIX:
$field = "{$field}::ABSTIME";
break;
case DATE_ISO:
$field = "TO_DATE({$field}, 'FMYYYY-FMMM-FMDDTFMHH24:FMMI:FMSS')";
break;
case DATE_DATETIME:
break;
}
break;
}
return $this
->sql_tz($field, $offset);
}
function sql_offset($field, $offset = NULL) {
if (!empty($offset)) {
switch ($this->db_type) {
case 'mysql':
case 'mysqli':
if (version_compare(db_version(), '4.1.1', '>=')) {
return "ADDTIME({$field}, SEC_TO_TIME({$offset}))";
}
else {
return "DATE_ADD(CAST({$field} AS DATETIME), INTERVAL {$offset} SECOND)";
}
case 'pgsql':
return "({$field} + INTERVAL '{$offset} SECONDS')";
}
}
return $field;
}
function sql_date_math($field, $direction, $count, $granularity) {
$granularity = drupal_strtoupper($granularity);
switch ($this->db_type) {
case 'mysql':
case 'mysqli':
switch ($direction) {
case 'ADD':
return "DATE_ADD(CAST({$field} AS DATETIME), INTERVAL {$count} {$granularity})";
case 'SUB':
return "DATE_SUB(CAST({$field} AS DATETIME), INTERVAL {$count} {$granularity})";
}
case 'pgsql':
$granularity .= 'S';
switch ($direction) {
case 'ADD':
return "({$field} + INTERVAL '{$count} {$granularity}')";
case 'SUB':
return "({$field} - INTERVAL '{$count} {$granularity}')";
}
}
return $field;
}
function sql_tz($field, $offset = NULL) {
$db_zone = $this->db_timezone_field ? $this->db_timezone_field : "'{$this->db_timezone}'";
$localzone = $this->local_timezone_field ? $this->local_timezone_field : "'{$this->local_timezone}'";
if ($offset !== NULL) {
return $this
->sql_offset($field, $offset);
}
elseif ($db_zone == $localzone) {
return $this
->sql_offset($field, 0);
}
elseif (!$this
->db_tz_support()) {
if (!empty($this->offset_field)) {
return $this
->sql_offset($field, $this->offset_field);
}
else {
return $this
->sql_offset($field, $this
->get_offset());
}
}
else {
switch ($this->db_type) {
case 'mysql':
case 'mysqli':
return "CONVERT_TZ({$field}, {$db_zone}, {$localzone})";
case 'pgsql':
return "{$field}::timestamp with time zone AT TIME ZONE {$localzone}";
}
}
}
function sql_format($format, $field) {
switch ($this->db_type) {
case 'mysql':
case 'mysqli':
$replace = array(
'Y' => '%Y',
'y' => '%y',
'm' => '%m',
'n' => '%c',
'd' => '%%d',
'j' => '%e',
'H' => '%H',
'i' => '%i',
's' => '%%s',
'\\WW' => 'W%U',
);
$format = strtr($format, $replace);
return "DATE_FORMAT({$field}, '{$format}')";
case 'pgsql':
$replace = array(
'Y' => 'YYYY',
'y' => 'Y',
'm' => 'MM',
'n' => 'M',
'd' => 'DD',
'j' => 'D',
'H' => 'HH24',
'i' => 'MI',
's' => 'SS',
'\\T' => '"T"',
);
$format = strtr($format, $replace);
return "TO_CHAR({$field}, '{$format}')";
}
}
function sql_extract($extract_type, $field) {
switch (drupal_strtoupper($extract_type)) {
case 'DATE':
return $field;
case 'YEAR':
return "EXTRACT(YEAR FROM({$field}))";
case 'MONTH':
return "EXTRACT(MONTH FROM({$field}))";
case 'DAY':
return "EXTRACT(DAY FROM({$field}))";
case 'HOUR':
return "EXTRACT(HOUR FROM({$field}))";
case 'MINUTE':
return "EXTRACT(MINUTE FROM({$field}))";
case 'SECOND':
return "EXTRACT(SECOND FROM({$field}))";
case 'WEEK':
switch ($this->db_type) {
case 'mysql':
case 'mysqli':
return "WEEK({$field}, 3)";
case 'pgsql':
return "EXTRACT(WEEK FROM({$field}))";
}
case 'DOW':
switch ($this->db_type) {
case 'mysql':
case 'mysqli':
return "INTEGER(DAYOFWEEK({$field}) - 1)";
case 'pgsql':
return "EXTRACT(DOW FROM({$field}))";
}
case 'DOY':
switch ($this->db_type) {
case 'mysql':
case 'mysqli':
return "DAYOFYEAR({$field})";
case 'pgsql':
return "EXTRACT(DOY FROM({$field}))";
}
}
}
function sql_where_date($type, $field, $operator, $value, $adjustment = NULL) {
$type = drupal_strtoupper($type);
if (drupal_strtoupper($value) == 'NOW') {
$value = $this
->sql_field('NOW', $adjustment);
}
elseif ($type == 'FIELD') {
$value = $this
->sql_field($value, $adjustment);
}
elseif ($type == 'DATE') {
$date = date_make_date($value, date_default_timezone_name(), DATE_DATETIME);
if (!empty($adjustment)) {
date_modify($date, $adjustment . ' seconds');
}
if (empty($this->db_timezone_field) && empty($this->local_timezone_field) && $this->db_timezone_field != $this->local_timezone_field) {
date_timezone_set($date, timezone_open($this->db_timezone));
$this->local_timezone = $this->db_timezone;
}
$value = "'" . date_format_date($date, 'custom', DATE_FORMAT_DATETIME) . "'";
}
if ($this->local_timezone != $this->db_timezone) {
$field = $this
->sql_field($field);
}
else {
$field = $this
->sql_field($field, 0);
}
return "{$field} {$operator} {$value}";
}
function sql_where_extract($part, $field, $operator, $value, $adjustment = NULL) {
$field = $this
->sql_field($field, $adjustment);
return $this
->sql_extract($part, $field) . " {$operator} {$value}";
}
function sql_where_format($format, $field, $operator, $value, $adjustment = NULL) {
$field = $this
->sql_field($field, $adjustment);
return $this
->sql_format($format, $field) . " {$operator} '{$value}'";
}
function date_parts($limit = NULL) {
$parts = array(
'year' => date_t('Year', 'datetime'),
'month' => date_t('Month', 'datetime'),
'day' => date_t('Day', 'datetime'),
'hour' => date_t('Hour', 'datetime'),
'minute' => date_t('Minute', 'datetime'),
'second' => date_t('Second', 'datetime'),
);
if (!empty($limit)) {
$last = FALSE;
foreach ($parts as $key => $part) {
if ($last) {
unset($parts[$key]);
}
if ($key == $limit) {
$last = TRUE;
}
}
}
return $parts;
}
function part_info($op = NULL, $part = NULL) {
$info = array();
$info['min'] = array(
'year' => 100,
'month' => 1,
'day' => 1,
'hour' => 0,
'minute' => 0,
'second' => 0,
);
$info['max'] = array(
'year' => 4000,
'month' => 12,
'day' => 31,
'hour' => 23,
'minute' => 59,
'second' => 59,
);
$info['format'] = array(
'year' => 'Y',
'month' => 'm',
'day' => 'd',
'hour' => 'H',
'minute' => 'i',
'second' => 's',
);
$info['sep'] = array(
'year' => '',
'month' => '-',
'day' => '-',
'hour' => ' ',
'minute' => ':',
'second' => ':',
);
$info['empty_now'] = array(
'year' => date('Y'),
'month' => date('m'),
'day' => min('28', date('d')),
'hour' => date('H'),
'minute' => date('i'),
'second' => date('s'),
);
$info['empty_min'] = array(
'year' => '1000',
'month' => '01',
'day' => '01',
'hour' => '00',
'minute' => '00',
'second' => '00',
);
$info['empty_max'] = array(
'year' => '9999',
'month' => '12',
'day' => '31',
'hour' => '23',
'minute' => '59',
'second' => '59',
);
if (!empty($op)) {
if (!empty($part)) {
return $info[$op][$part];
}
else {
return $info[$op];
}
}
return $info;
}
function complete_date($selected, $type = 'now') {
if (empty($selected)) {
return '';
}
if (array_key_exists('week', $selected)) {
$dates = date_week_range($selected['week'], $selected['year']);
switch ($type) {
case 'empty_now':
case 'empty_min':
case 'min':
return date_format($dates[0], 'Y-m-d H:i:s');
case 'empty_max':
case 'max':
return date_format($dates[1], 'Y-m-d H:i:s');
default:
return;
}
}
$compare = array_merge($this
->part_info('empty_' . $type), $selected);
if ($type == 'max') {
$compare['day'] = date_days_in_month($compare['year'], $compare['month']);
}
$value = '';
$separators = $this
->part_info('sep');
foreach ($this
->date_parts() as $key => $name) {
$value .= $separators[$key] . (!empty($selected[$key]) ? $selected[$key] : $compare[$key]);
}
return $value;
}
function format_help($format) {
$replace = array(
'Y' => 'YYYY',
'm' => 'MM',
'd' => 'DD',
'H' => 'HH',
'i' => 'MM',
's' => 'SS',
'\\T' => 'T',
);
return strtr($format, $replace);
}
function part_is_valid($value, $type) {
if (!preg_match('/^[0-9]*$/', $value)) {
return FALSE;
}
$value = intval($value);
if ($value <= 0) {
return FALSE;
}
switch ($type) {
case 'year':
if ($value < DATE_MIN_YEAR) {
return FALSE;
}
break;
case 'month':
if ($value < 0 || $value > 12) {
return FALSE;
}
break;
case 'day':
if ($value < 0 || $value > 31) {
return FALSE;
}
break;
case 'week':
if ($value < 0 || $value > 53) {
return FALSE;
}
}
return TRUE;
}
function views_formats($granularity, $type = 'sql') {
$formats = array(
'display',
'sql',
);
$long = str_replace(':i', ':i:s', variable_get('date_format_long', 'l, F j, Y - H:i'));
switch ($granularity) {
case 'year':
$formats['display'] = 'Y';
$formats['sql'] = 'Y';
break;
case 'month':
$formats['display'] = date_limit_format($long, array(
'year',
'month',
));
$formats['sql'] = 'Y-m';
break;
case 'day':
$formats['display'] = date_limit_format($long, array(
'year',
'month',
'day',
));
$formats['sql'] = 'Y-m-d';
break;
case 'hour':
$formats['display'] = date_limit_format($long, array(
'year',
'month',
'day',
'hour',
));
$formats['sql'] = 'Y-m-d\\TH';
break;
case 'minute':
$formats['display'] = date_limit_format($long, array(
'year',
'month',
'day',
'hour',
'minute',
));
$formats['sql'] = 'Y-m-d\\TH:i';
break;
case 'second':
$formats['display'] = date_limit_format($long, array(
'year',
'month',
'day',
'hour',
'minute',
'second',
));
$formats['sql'] = 'Y-m-d\\TH:i:s';
break;
case 'week':
$formats['display'] = 'F j Y (W)';
$formats['sql'] = 'Y-\\WW';
break;
}
return $formats[$type];
}
function granularity_form($granularity) {
$form = array(
'#title' => t('Granularity'),
'#type' => 'radios',
'#default_value' => $granularity,
'#options' => $this
->date_parts(),
);
return $form;
}
function arg_parts($argument) {
$values = array();
if (empty($argument) || is_array($argument)) {
return array(
'date' => array(),
'period' => array(),
);
}
$fromto = explode('--', $argument);
foreach ($fromto as $arg) {
$parts = array();
if ($arg == '@') {
$parts['date'] = date_array(date_now());
}
elseif (preg_match('/(\\d{4})?-?(W)?(\\d{1,2})?-?(\\d{1,2})?[T\\s]?(\\d{1,2})?:?(\\d{1,2})?:?(\\d{1,2})?/', $arg, $matches)) {
$date = array();
if (!empty($matches[1])) {
$date['year'] = $matches[1];
}
if (!empty($matches[3])) {
if (empty($matches[2])) {
$date['month'] = $matches[3];
}
else {
$date['week'] = $matches[3];
}
}
if (!empty($matches[4])) {
$date['day'] = $matches[4];
}
if (!empty($matches[5])) {
$date['hour'] = $matches[5];
}
if (!empty($matches[6])) {
$date['minute'] = $matches[6];
}
if (!empty($matches[7])) {
$date['second'] = $matches[7];
}
$parts['date'] = $date;
}
if (preg_match('/^P(\\d{1,4}[Y])?(\\d{1,2}[M])?(\\d{1,2}[W])?(\\d{1,2}[D])?([T]{0,1})?(\\d{1,2}[H])?(\\d{1,2}[M])?(\\d{1,2}[S])?/', $arg, $matches)) {
$period = array();
if (!empty($matches[1])) {
$period['year'] = str_replace('Y', '', $matches[1]);
}
if (!empty($matches[2])) {
$period['month'] = str_replace('M', '', $matches[2]);
}
if (!empty($matches[3])) {
$period['week'] = str_replace('W', '', $matches[3]);
}
if (!empty($matches[4])) {
$period['day'] = str_replace('D', '', $matches[4]);
}
if (!empty($matches[6])) {
$period['hour'] = str_replace('H', '', $matches[6]);
}
if (!empty($matches[7])) {
$period['minute'] = str_replace('M', '', $matches[7]);
}
if (!empty($matches[8])) {
$period['second'] = str_replace('S', '', $matches[8]);
}
$parts['period'] = $period;
}
$values[] = $parts;
}
return $values;
}
function arg_replace($arg) {
if (!preg_match('/([+|-])\\s?([0-9]{1,32})\\s?([day(s)?|week(s)?|month(s)?|year(s)?|hour(s)?|minute(s)?|second(s)?]{1,10})/', $arg, $results)) {
return str_replace('now', '@', $arg);
}
$direction = $results[1];
$count = $results[2];
$item = $results[3];
$replace = array(
'now' => '@',
'+' => 'P',
'-' => 'P-',
'years' => 'Y',
'year' => 'Y',
'months' => 'M',
'month' => 'M',
'weeks' => 'W',
'week' => 'W',
'days' => 'D',
'day' => 'D',
'hours' => 'H',
'hour' => 'H',
'minutes' => 'M',
'minute' => 'M',
'seconds' => 'S',
'second' => 'S',
' ' => '',
' ' => '',
);
$prefix = in_array($item, array(
'hours',
'hour',
'minutes',
'minute',
'seconds',
'second',
)) ? 'T' : '';
return $prefix . strtr($direction, $replace) . $count . strtr($item, $replace);
}
function arg_granularity($arg) {
$granularity = '';
$parts = $this
->arg_parts($arg);
$date = !empty($parts[0]['date']) ? $parts[0]['date'] : (!empty($parts[1]['date']) ? $parts[1]['date'] : array());
foreach ($date as $key => $part) {
$granularity = $key;
}
return $granularity;
}
function arg_range($arg) {
$parts = $this
->arg_parts($arg);
if (empty($parts[0]['date']) && !empty($parts[0]['period']) && empty($parts[1])) {
$min_date = date_now();
$max_date = drupal_clone($min_date);
foreach ($parts[0]['period'] as $part => $value) {
date_modify($max_date, "+{$value} {$part}");
}
date_modify($max_date, '-1 second');
return array(
$min_date,
$max_date,
);
}
if (empty($parts[0]['date']) && !empty($parts[0]['period']) && !empty($parts[1]['period'])) {
$min_date = date_now();
$max_date = drupal_clone($min_date);
foreach ($parts[0]['period'] as $part => $value) {
date_modify($min_date, "+{$value} {$part}");
}
date_modify($min_date, '-1 second');
foreach ($parts[1]['period'] as $part => $value) {
date_modify($max_date, "+{$value} {$part}");
}
date_modify($max_date, '-1 second');
return array(
$min_date,
$max_date,
);
}
if (!empty($parts[0]['date'])) {
$value = date_fuzzy_datetime($this
->complete_date($parts[0]['date'], 'min'));
$min_date = date_make_date($value, date_default_timezone_name(), DATE_ISO);
if (empty($parts[1]) || empty($parts[1]['date']) && empty($parts[1]['period'])) {
$value = date_fuzzy_datetime($this
->complete_date($parts[0]['date'], 'max'));
$max_date = date_make_date($value, date_default_timezone_name(), DATE_ISO);
return array(
$min_date,
$max_date,
);
}
elseif (!empty($parts[1]['period'])) {
foreach ($parts[1]['period'] as $part => $value) {
$max_date = drupal_clone($min_date);
date_modify($max_date, "+{$value} {$part}");
}
date_modify($max_date, '-1 second');
return array(
$min_date,
$max_date,
);
}
}
if (!empty($parts[1]['date'])) {
$value = date_fuzzy_datetime($this
->complete_date($parts[1]['date'], 'max'));
$max_date = date_make_date($value, date_default_timezone_name(), DATE_ISO);
if (isset($min_date)) {
return array(
$min_date,
$max_date,
);
}
}
if (!empty($parts[0]['period'])) {
$min_date = date_now();
foreach ($parts[0]['period'] as $part => $value) {
date_modify($min_date, "{$value} {$part}");
}
return array(
$min_date,
$max_date,
);
}
$now = date_now();
return array(
$now,
$now,
);
}
}