1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\DB\SQL; |
4
|
|
|
|
5
|
|
|
use Helix\DB; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Produces datetime related expressions for the instance. |
9
|
|
|
* |
10
|
|
|
* @see https://sqlite.org/lang_datefunc.html |
11
|
|
|
* @see https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format |
12
|
|
|
*/ |
13
|
|
|
trait DateTimeTrait { |
14
|
|
|
|
15
|
|
|
abstract public function __toString (); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var DB |
19
|
|
|
*/ |
20
|
|
|
protected $db; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* `YYYY-MM-DD` |
24
|
|
|
* |
25
|
|
|
* @return Text |
26
|
|
|
*/ |
27
|
|
|
public function getDate () { |
28
|
|
|
return Text::factory($this->db, $this->getDateTimeFormat('%Y-%m-%d')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* `YYYY-MM-DD hh:mm:ss` |
33
|
|
|
* |
34
|
|
|
* @return Text |
35
|
|
|
*/ |
36
|
|
|
public function getDateTime () { |
37
|
|
|
return Text::factory($this->db, $this->getDateTimeFormat([ |
38
|
|
|
'mysql' => '%Y-%m-%d %H:%i:%S', |
39
|
|
|
'sqlite' => '%Y-%m-%d %H:%M:%S' |
40
|
|
|
])); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns a text expression using a driver-appropriate format function. |
45
|
|
|
* |
46
|
|
|
* @param string|string[] $format Format, or formats keyed by driver name. |
47
|
|
|
* @return Text |
48
|
|
|
*/ |
49
|
|
|
public function getDateTimeFormat ($format) { |
50
|
|
|
if (is_array($format)) { |
51
|
|
|
$format = $format[$this->db->getDriver()]; |
52
|
|
|
} |
53
|
|
|
$format = $this->db->quote($format); |
54
|
|
|
if ($this->db->isSQLite()) { |
55
|
|
|
return Text::factory($this->db, "STRFTIME({$format},{$this})"); |
56
|
|
|
} |
57
|
|
|
return Text::factory($this->db, "DATE_FORMAT({$this},{$format})"); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* `01` to `31` |
62
|
|
|
* |
63
|
|
|
* @return Numeric |
64
|
|
|
*/ |
65
|
|
|
public function getDay () { |
66
|
|
|
return Numeric::factory($this->db, $this->getDateTimeFormat('%d')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* `0` to `6` (Sunday is `0`) |
71
|
|
|
* |
72
|
|
|
* @return Numeric |
73
|
|
|
*/ |
74
|
|
|
public function getDayOfWeek () { |
75
|
|
|
return Numeric::factory($this->db, $this->getDateTimeFormat('%w')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* `001` to `366` (365 + 1 during leap year) |
80
|
|
|
* |
81
|
|
|
* @return Numeric |
82
|
|
|
*/ |
83
|
|
|
public function getDayOfYear () { |
84
|
|
|
return Numeric::factory($this->db, $this->getDateTimeFormat('%j')); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* `00` to `23` |
89
|
|
|
* |
90
|
|
|
* @return Numeric |
91
|
|
|
*/ |
92
|
|
|
public function getHours () { |
93
|
|
|
return Numeric::factory($this->db, $this->getDateTimeFormat('%H')); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* `00` to `59` |
98
|
|
|
* |
99
|
|
|
* @return Numeric |
100
|
|
|
*/ |
101
|
|
|
public function getMinutes () { |
102
|
|
|
return Numeric::factory($this->db, $this->getDateTimeFormat([ |
103
|
|
|
'mysql' => '%i', |
104
|
|
|
'sqlite' => '%M' |
105
|
|
|
])); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* `01` to `12` |
110
|
|
|
* |
111
|
|
|
* @return Numeric |
112
|
|
|
*/ |
113
|
|
|
public function getMonth () { |
114
|
|
|
return Numeric::factory($this->db, $this->getDateTimeFormat('%m')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* `00` to `59` |
119
|
|
|
* |
120
|
|
|
* @return Numeric |
121
|
|
|
*/ |
122
|
|
|
public function getSeconds () { |
123
|
|
|
return Numeric::factory($this->db, $this->getDateTimeFormat('%S')); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* `00:00:00` to `23:59:59` |
128
|
|
|
* |
129
|
|
|
* @return Text |
130
|
|
|
*/ |
131
|
|
|
public function getTime () { |
132
|
|
|
return Text::factory($this->db, $this->getDateTimeFormat([ |
133
|
|
|
'mysql' => '%H:%i:%S', |
134
|
|
|
'sqlite' => '%H:%M:%S' |
135
|
|
|
])); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Unix timestamp. |
140
|
|
|
* |
141
|
|
|
* @return Numeric |
142
|
|
|
*/ |
143
|
|
|
public function getTimestamp () { |
144
|
|
|
if ($this->db->isSQLite()) { |
145
|
|
|
return Numeric::factory($this->db, "STRFTIME('%s',{$this})"); |
146
|
|
|
} |
147
|
|
|
return Numeric::factory($this->db, "UNIX_TIMESTAMP({$this})"); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* `00` to `53` |
152
|
|
|
* |
153
|
|
|
* @return Numeric |
154
|
|
|
*/ |
155
|
|
|
public function getWeekOfYear () { |
156
|
|
|
return Numeric::factory($this->db, $this->getDateTimeFormat([ |
157
|
|
|
'mysql' => '%U', |
158
|
|
|
'sqlite' => '%W' |
159
|
|
|
])); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* `YYYY` |
164
|
|
|
* |
165
|
|
|
* @return Numeric |
166
|
|
|
*/ |
167
|
|
|
public function getYear () { |
168
|
|
|
return Numeric::factory($this->db, $this->getDateTimeFormat('%Y')); |
169
|
|
|
} |
170
|
|
|
} |