Passed
Push — master ( 08a8fa...e64906 )
by y
01:56
created

DateTimeFormatTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 199
rs 10
wmc 18

16 Methods

Rating   Name   Duplication   Size   Complexity  
A month() 0 3 1
A date() 0 3 1
A year() 0 3 1
A seconds() 0 3 1
A dayOfWeek() 0 3 1
A minutes() 0 5 1
A hours() 0 3 1
A dateFormat() 0 10 3
A iso8601() 0 5 1
A datetime() 0 6 1
A time() 0 5 1
A weekOfYear() 0 5 1
A day() 0 3 1
A timestamp() 0 6 1
A dayOfYear() 0 3 1
A julian() 0 7 1
1
<?php
2
3
namespace Helix\DB\Fluent\DateTime;
4
5
use Helix\DB\Fluent\AbstractTrait;
6
use Helix\DB\Fluent\DateTime;
7
use Helix\DB\Fluent\Num;
8
use Helix\DB\Fluent\Str;
9
10
/**
11
 * Date-time formatting.
12
 */
13
trait DateTimeFormatTrait
14
{
15
16
    use AbstractTrait;
17
18
    /**
19
     * `YYYY-MM-DD`
20
     *
21
     * Because this format is reentrant, a {@link DateTime} is returned.
22
     *
23
     * @return DateTime
24
     */
25
    public function date()
26
    {
27
        return DateTime::factory($this->db, "DATE({$this})");
28
    }
29
30
    /**
31
     * Date formatting expression using a driver-appropriate function.
32
     *
33
     * @param string|string[] $format Format, or formats keyed by driver name.
34
     * @return Str
35
     */
36
    public function dateFormat($format)
37
    {
38
        if (is_array($format)) {
39
            $format = $format[$this->db->getDriver()];
40
        }
41
        $format = $this->db->quote($format);
42
        if ($this->db->isSQLite()) {
43
            return Str::factory($this->db, "STRFTIME({$format},{$this})");
44
        }
45
        return Str::factory($this->db, "DATE_FORMAT({$this},{$format})");
46
    }
47
48
    /**
49
     * `YYYY-MM-DD hh:mm:ss`
50
     *
51
     * Because this format is reentrant, a {@link DateTime} is returned.
52
     *
53
     * @return DateTime
54
     */
55
    public function datetime()
56
    {
57
        return DateTime::fromFormat($this->db, [
58
            'mysql' => "DATE_FORMAT(%s,'%%Y-%%m-%%d %%H:%%i:%%S')",
59
            'sqlite' => "DATETIME(%s)"
60
        ], $this);
61
    }
62
63
    /**
64
     * `01` to `31`
65
     *
66
     * @return Num
67
     */
68
    public function day()
69
    {
70
        return Num::factory($this->db, $this->dateFormat('%d'));
71
    }
72
73
    /**
74
     * `0` to `6` (Sunday is `0`)
75
     *
76
     * @return Num
77
     */
78
    public function dayOfWeek()
79
    {
80
        return Num::factory($this->db, $this->dateFormat('%w'));
81
    }
82
83
    /**
84
     * `001` to `366` (365 + 1 during leap year)
85
     *
86
     * @return Num
87
     */
88
    public function dayOfYear()
89
    {
90
        return Num::factory($this->db, $this->dateFormat('%j'));
91
    }
92
93
    /**
94
     * `00` to `23`
95
     *
96
     * @return Num
97
     */
98
    public function hours()
99
    {
100
        return Num::factory($this->db, $this->dateFormat('%H'));
101
    }
102
103
    /**
104
     * ISO-8601 compatible datetime string, offset `Z` (UTC/Zulu)
105
     *
106
     * https://en.wikipedia.org/wiki/ISO_8601
107
     *
108
     * @return Str
109
     */
110
    public function iso8601()
111
    {
112
        return $this->dateFormat([
113
            'mysql' => '%Y-%m-%dT%H:%i:%SZ',
114
            'sqlite' => '%Y-%m-%dT%H:%M:%SZ',
115
        ]);
116
    }
117
118
    /**
119
     * Julian day number (fractional).
120
     *
121
     * @return Num
122
     */
123
    public function julian()
124
    {
125
        return Num::fromFormat($this->db, [
126
            // mysql: julian "year zero" offset, plus number of fractional days since "year zero".
127
            'mysql' => "(1721059.5 + (TO_SECONDS(%s) / 86400))",
128
            'sqlite' => "JULIANDAY(%s)"
129
        ], $this);
130
    }
131
132
    /**
133
     * `00` to `59`
134
     *
135
     * @return Num
136
     */
137
    public function minutes()
138
    {
139
        return Num::factory($this->db, $this->dateFormat([
140
            'mysql' => '%i',
141
            'sqlite' => '%M'
142
        ]));
143
    }
144
145
    /**
146
     * `01` to `12`
147
     *
148
     * @return Num
149
     */
150
    public function month()
151
    {
152
        return Num::factory($this->db, $this->dateFormat('%m'));
153
    }
154
155
    /**
156
     * `00` to `59`
157
     *
158
     * @return Num
159
     */
160
    public function seconds()
161
    {
162
        return Num::factory($this->db, $this->dateFormat('%S'));
163
    }
164
165
    /**
166
     * `00:00:00` to `23:59:59`
167
     *
168
     * @return Str
169
     */
170
    public function time()
171
    {
172
        return $this->dateFormat([
173
            'mysql' => '%H:%i:%S',
174
            'sqlite' => '%H:%M:%S'
175
        ]);
176
    }
177
178
    /**
179
     * Unix timestamp.
180
     *
181
     * @return Num
182
     */
183
    public function timestamp()
184
    {
185
        return Num::fromFormat($this->db, [
186
            'mysql' => "UNIX_TIMESTAMP(%s)",
187
            'sqlite' => "STRFTIME('%%s',%s)",
188
        ], $this);
189
    }
190
191
    /**
192
     * `00` to `53`
193
     *
194
     * @return Num
195
     */
196
    public function weekOfYear()
197
    {
198
        return Num::factory($this->db, $this->dateFormat([
199
            'mysql' => '%U',
200
            'sqlite' => '%W'
201
        ]));
202
    }
203
204
    /**
205
     * `YYYY`
206
     *
207
     * @return Num
208
     */
209
    public function year()
210
    {
211
        return Num::factory($this->db, $this->dateFormat('%Y'));
212
    }
213
}
214