Completed
Push — master ( 311580...c99b64 )
by Tim
01:58
created

DateTimeUtility::getUtcTimeZone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * DateTime Utility.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Utility;
9
10
use TYPO3\CMS\Core\Utility\MathUtility;
11
12
/**
13
 * DateTime Utility.
14
 */
15
class DateTimeUtility
16
{
17
    /**
18
     * One second.
19
     */
20
    const SECONDS_SECOND = 1;
21
22
    /**
23
     * One minute in seconds.
24
     */
25
    const SECONDS_MINUTE = 60;
26
27
    /**
28
     * One hour in seconds.
29
     */
30
    const SECONDS_HOUR = 3600;
31
32
    /**
33
     * One day in seconds.
34
     */
35
    const SECONDS_DAY = 86400;
36
37
    /**
38
     * One week in seconds.
39
     */
40
    const SECONDS_WEEK = 604800;
41
42
    /**
43
     * One quartar in seconds (90 days).
44
     */
45
    const SECONDS_QUARTER = 7776000;
46
47
    /**
48
     * One year in seconds (365 days).
49
     */
50
    const SECONDS_YEAR = 31536000;
51
52
    /**
53
     * One decade in seconds (base on a 365 days year).
54
     */
55
    const SECONDS_DECADE = 315360000;
56
57
    /**
58
     * Format date (Backend).
59
     */
60
    const FORMAT_DATE_BACKEND = '%a %d.%m.%Y';
61
62
    /**
63
     * Convert a Week/Year combination to a DateTime of the first day of week.
64
     *
65
     * @param int $week
66
     * @param int $year
67
     * @param int $startDay
68
     *
69
     * @return \DateTime
70
     */
71
    public static function convertWeekYear2DayMonthYear($week, $year, $startDay = 1): \DateTime
72
    {
73
        $week = \str_pad((string)$week, 2, '0', STR_PAD_LEFT);
74 2
75
        return self::normalizeDateTimeSingle(\strtotime($year . '-W' . $week . '-' . $startDay));
76 2
    }
77
78
    /**
79
     * Time zone is set by the TYPO3 core.
80
     *
81
     * @return \DateTimeZone
82
     *
83
     * @see \TYPO3\CMS\Core\Core\Bootstrap->setDefaultTimezone()
84
     */
85
    public static function getTimeZone()
86
    {
87
        return new \DateTimeZone(\date_default_timezone_get());
88
    }
89
90
    /**
91
     * Time zone that is always UTC.
92
     *
93
     * @return \DateTimeZone
94
     */
95
    public static function getUtcTimeZone()
96
    {
97
        return new \DateTimeZone('UTC');
98
    }
99
100
    /**
101
     * Get the time seconds of the given date (TYPO3 Backend style).
102
     *
103
     * @param \DateTime $dateTime
104
     *
105
     * @return int
106
     */
107
    public static function getDaySecondsOfDateTime(\DateTime $dateTime): int
108
    {
109
        $hours = (int)$dateTime->format('G');
110
        $minutes = $hours * self::SECONDS_MINUTE + (int)$dateTime->format('i');
111
112
        return $minutes * self::SECONDS_MINUTE + (int)$dateTime->format('s');
113
    }
114
115
    /**
116
     * Get a normalize date time object.
117
     *
118
     * @param int|null $day
119
     * @param int|null $month
120
     * @param int|null $year
121
     *
122
     * @return \DateTime
123
     */
124
    public static function normalizeDateTime($day = null, $month = null, $year = null): \DateTime
125
    {
126
        $date = self::getNow();
127
        // Check if this date should handle always in UTC
128
        // $date->setTimezone(self::getUtcTimeZone());
129
        if (!MathUtility::canBeInterpretedAsInteger($year)) {
130
            $year = $date->format('Y');
131
        }
132
        if (!MathUtility::canBeInterpretedAsInteger($month)) {
133
            $month = $date->format('m');
134
        }
135
        if (!MathUtility::canBeInterpretedAsInteger($day)) {
136
            $day = $date->format('d');
137
        }
138
        $date->setDate((int)$year, (int)$month, (int)$day);
139
        $date->setTime(0, 0, 0);
140
        if ($date->format('m') > $month) {
141
            $date->modify('last day of last month');
142
        } elseif ($date->format('m') < $month) {
143
            $date->modify('first day of next month');
144
        }
145 2
146
        return $date;
147 2
    }
148 2
149 2
    /**
150
     * Normalize quartar.
151 2
     *
152
     * @param int|null $quarter
153
     *
154 2
     * @return int
155
     */
156
    public static function normalizeQuarter(int $quarter = null): int
157
    {
158
        if (null === $quarter) {
159
            $quarter = self::getQuartar(self::getNow());
160
        }
161
162
        return MathUtility::forceIntegerInRange((int)$quarter, 1, 4);
163
    }
164
165
    /**
166
     * Normalize quartar.
167
     *
168
     * @param int|null $quarter
0 ignored issues
show
Bug introduced by
There is no parameter named $quarter. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
169
     *
170
     * @return int
171
     */
172
    public static function getQuartar(\DateTimeInterface $date): int
173
    {
174
        $month = (int)$date->format('n');
175
176
        return (int)\ceil($month / 3);
177
    }
178
179
    /**
180
     * Reset the DateTime.
181
     *
182
     * @param \DateTime $dateTime
183
     *
184
     * @return \DateTime
185
     */
186
    public static function resetTime($dateTime = null): \DateTime
187
    {
188
        $dateTime = self::normalizeDateTimeSingle($dateTime);
189
        $dateTime->setTime(0, 0, 0);
190
191
        return $dateTime;
192
    }
193
194
    /**
195
     * Get a normalized date time object. The timezone of the returned object is
196
     * UTC for integer parameters and server timezone for everything else.
197
     *
198
     * @param int|string|\DateTime|null $dateInformation
199
     *
200
     * @return \DateTime
201
     */
202
    public static function normalizeDateTimeSingle($dateInformation): \DateTime
203
    {
204
        if ($dateInformation instanceof \DateTimeInterface) {
205
            return $dateInformation;
206
        }
207
        if (MathUtility::canBeInterpretedAsInteger($dateInformation)) {
208
            // http://php.net/manual/en/datetime.construct#refsect1-datetime.construct-parameters :
209
            // The $timezone parameter and the current timezone are ignored [ie. set to UTC] when the $time parameter [...] is a UNIX timestamp (e.g. @946684800) [...]
210
            return new \DateTime("@$dateInformation");
211
        }
212
        if (\is_string($dateInformation)) {
213
            return new \DateTime($dateInformation);
214
        }
215
216
        return self::getNow();
217
    }
218
219
    /**
220
     * Get the current date (normalized optimized for queries, because SIM_ACCESS_TIME is rounded to minutes)
221
     * in the current timezone.
222
     *
223
     * @return \DateTime
224
     */
225
    public static function getNow(): \DateTime
226
    {
227
        // NOTE that new \DateTime('@timestamp') does NOT work - @see comment in normalizeDateTimeSingle()
228
        // So we create a date string with timezone information first, and a \DateTime in the current server timezone then.
229
        return new \DateTime(\date(\DateTime::ATOM, (int)$GLOBALS['SIM_ACCESS_TIME']));
230
    }
231
232
    /**
233
     * Alias for resetTime.
234
     *
235
     * @see resetTime()
236
     *
237
     * @param int|string|\DateTime|null $dateInformation
238
     *
239
     * @return \DateTime
240
     */
241
    public static function getDayStart($dateInformation): \DateTime
242
    {
243
        return self::resetTime($dateInformation);
244
    }
245
246
    /**
247
     * Get the End of the given day.
248
     *
249
     * @param int|string|\DateTime|null $dateInformation
250
     *
251
     * @return \DateTime
252
     */
253
    public static function getDayEnd($dateInformation): \DateTime
254
    {
255
        $dateTime = self::getDayStart($dateInformation);
256
        $dateTime->setTime(23, 59, 59);
257
258
        return $dateTime;
259
    }
260
}
261