Completed
Push — master ( f30a93...2b3d30 )
by Tim
02:07
created

DateTimeUtility   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 21.62%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 0
loc 234
rs 10
c 0
b 0
f 0
ccs 8
cts 37
cp 0.2162

11 Methods

Rating   Name   Duplication   Size   Complexity  
A convertWeekYear2DayMonthYear() 0 6 1
A getTimeZone() 0 4 1
A getDaySecondsOfDateTime() 0 7 1
B normalizeDateTime() 0 22 6
A normalizeQuarter() 0 8 2
A getQuartar() 0 6 1
A resetTime() 0 7 1
A normalizeDateTimeSingle() 0 16 4
A getNow() 0 6 1
A getDayStart() 0 4 1
A getDayEnd() 0 7 1
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
     * Get the time seconds of the given date (TYPO3 Backend style).
92
     *
93
     * @param \DateTime $dateTime
94
     *
95
     * @return int
96
     */
97
    public static function getDaySecondsOfDateTime(\DateTime $dateTime): int
98
    {
99
        $hours = (int) $dateTime->format('G');
100
        $minutes = $hours * self::SECONDS_MINUTE + (int) $dateTime->format('i');
101
102
        return $minutes * self::SECONDS_MINUTE + (int) $dateTime->format('s');
103
    }
104
105
    /**
106
     * Get a normalize date time object.
107
     *
108
     * @param int|null $day
109
     * @param int|null $month
110
     * @param int|null $year
111
     *
112
     * @return \DateTime
113
     */
114
    public static function normalizeDateTime($day = null, $month = null, $year = null): \DateTime
115
    {
116
        $date = self::getNow();
117
        if (!MathUtility::canBeInterpretedAsInteger($year)) {
118
            $year = $date->format('Y');
119
        }
120
        if (!MathUtility::canBeInterpretedAsInteger($month)) {
121
            $month = $date->format('m');
122
        }
123
        if (!MathUtility::canBeInterpretedAsInteger($day)) {
124
            $day = $date->format('d');
125
        }
126
        $date->setDate((int) $year, (int) $month, (int) $day);
127
        $date->setTime(0, 0, 0);
128
        if ($date->format('m') > $month) {
129
            $date->modify('last day of last month');
130
        } elseif ($date->format('m') < $month) {
131
            $date->modify('first day of next month');
132
        }
133
134
        return $date;
135
    }
136
137
    /**
138
     * Normalize quartar.
139
     *
140
     * @param int|null $quarter
141
     *
142
     * @return int
143
     */
144
    public static function normalizeQuarter(int $quarter = null): int
145 2
    {
146
        if (null === $quarter) {
147 2
            $quarter = self::getQuartar(self::getNow());
148 2
        }
149 2
150
        return MathUtility::forceIntegerInRange((int) $quarter, 1, 4);
151 2
    }
152
153
    /**
154 2
     * Normalize quartar.
155
     *
156
     * @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...
157
     *
158
     * @return int
159
     */
160
    public static function getQuartar(\DateTimeInterface $date): int
161
    {
162
        $month = (int) $date->format('n');
163
164
        return (int) \ceil($month / 3);
165
    }
166
167
    /**
168
     * Reset the DateTime.
169
     *
170
     * @param \DateTime $dateTime
171
     *
172
     * @return \DateTime
173
     */
174
    public static function resetTime($dateTime = null): \DateTime
175
    {
176
        $dateTime = self::normalizeDateTimeSingle($dateTime);
177
        $dateTime->setTime(0, 0, 0);
178
179
        return $dateTime;
180
    }
181
182
    /**
183
     * Get a normalized date time object. The timezone of the returned object is
184
     * UTC for integer parameters and server timezone for everything else.
185
     *
186
     * @param int|string|\DateTime|null $dateInformation
187
     *
188
     * @return \DateTime
189
     */
190
    public static function normalizeDateTimeSingle($dateInformation): \DateTime
191
    {
192
        if ($dateInformation instanceof \DateTimeInterface) {
193
            return $dateInformation;
194
        }
195
        if (MathUtility::canBeInterpretedAsInteger($dateInformation)) {
196
            // http://php.net/manual/en/datetime.construct#refsect1-datetime.construct-parameters :
197
            // The $timezone parameter and the current timezone are ignored [ie. set to UTC] when the $time parameter [...] is a UNIX timestamp (e.g. @946684800) [...]
198
            return new \DateTime("@$dateInformation");
199
        }
200
        if (\is_string($dateInformation)) {
201
            return new \DateTime($dateInformation);
202
        }
203
204
        return self::getNow();
205
    }
206
207
    /**
208
     * Get the current date (normalized optimized for queries, because SIM_ACCESS_TIME is rounded to minutes)
209
     * in the current timezone.
210
     *
211
     * @return \DateTime
212
     */
213
    public static function getNow(): \DateTime
214
    {
215
        // NOTE that new \DateTime('@timestamp') does NOT work - @see comment in normalizeDateTimeSingle()
216
        // So we create a date string with timezone information first, and a \DateTime in the current server timezone then.
217
        return new \DateTime(\date(\DateTime::ATOM, (int) $GLOBALS['SIM_ACCESS_TIME']));
218
    }
219
220
    /**
221
     * Alias for resetTime.
222
     *
223
     * @see resetTime()
224
     *
225
     * @param int|string|\DateTime|null $dateInformation
226
     *
227
     * @return \DateTime
228
     */
229
    public static function getDayStart($dateInformation): \DateTime
230
    {
231
        return self::resetTime($dateInformation);
232
    }
233
234
    /**
235
     * Get the End of the given day.
236
     *
237
     * @param int|string|\DateTime|null $dateInformation
238
     *
239
     * @return \DateTime
240
     */
241
    public static function getDayEnd($dateInformation): \DateTime
242
    {
243
        $dateTime = self::getDayStart($dateInformation);
244
        $dateTime->setTime(23, 59, 59);
245
246
        return $dateTime;
247
    }
248
}
249