00ad873b83a8e076f56c1becc060c9cce61866ab
[yaffs-website] / web / core / modules / views / src / Plugin / views / query / MysqlDateSql.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\query;
4
5 use Drupal\Core\Database\Connection;
6
7 /**
8  * MySQL-specific date handling.
9  *
10  * @internal
11  *   This class should only be used by the Views SQL query plugin.
12  *
13  * @see \Drupal\views\Plugin\views\query\Sql
14  */
15 class MysqlDateSql implements DateSqlInterface {
16
17   /**
18    * The database connection.
19    *
20    * @var \Drupal\Core\Database\Connection
21    */
22   protected $database;
23
24   /**
25    * An array of PHP-to-MySQL replacement patterns.
26    */
27   protected static $replace = [
28     'Y' => '%Y',
29     'y' => '%y',
30     'M' => '%b',
31     'm' => '%m',
32     'n' => '%c',
33     'F' => '%M',
34     'D' => '%a',
35     'd' => '%d',
36     'l' => '%W',
37     'j' => '%e',
38     'W' => '%v',
39     'H' => '%H',
40     'h' => '%h',
41     'i' => '%i',
42     's' => '%s',
43     'A' => '%p',
44   ];
45
46   /**
47    * Constructs the MySQL-specific date sql class.
48    *
49    * @param \Drupal\Core\Database\Connection $database
50    *   The database connection.
51    */
52   public function __construct(Connection $database) {
53     $this->database = $database;
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function getDateField($field, $string_date) {
60     if ($string_date) {
61       return $field;
62     }
63
64     // Base date field storage is timestamp, so the date to be returned here is
65     // epoch + stored value (seconds from epoch).
66     return "DATE_ADD('19700101', INTERVAL $field SECOND)";
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getDateFormat($field, $format) {
73     $format = strtr($format, static::$replace);
74     return "DATE_FORMAT($field, '$format')";
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function setTimezoneOffset($offset) {
81     $this->database->query("SET @@session.time_zone = '$offset'");
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function setFieldTimezoneOffset(&$field, $offset) {
88     if (!empty($offset)) {
89       $field = "($field + INTERVAL $offset SECOND)";
90     }
91   }
92
93 }