• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

johnykvsky / dummygenerator / 16881746921

11 Aug 2025 01:39PM UTC coverage: 98.554% (-0.1%) from 98.665%
16881746921

Pull #19

github

web-flow
Merge efc0eed0a into efc07aa39
Pull Request #19: Updated enum extension

886 of 899 relevant lines covered (98.55%)

14.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.45
/src/Core/DateTime.php
1
<?php
2

3
declare(strict_types = 1);
4

5
namespace DummyGenerator\Core;
6

7
use DateInterval;
8
use DateTimeImmutable;
9
use DateTimeInterface;
10
use DateTimeZone;
11
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionInterface;
12
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionTrait;
13
use DummyGenerator\Definitions\Extension\DateTimeExtensionInterface;
14
use DummyGenerator\Definitions\Extension\Exception\ExtensionArgumentException;
15

16
class DateTime implements DateTimeExtensionInterface, RandomizerAwareExtensionInterface
17
{
18
    use RandomizerAwareExtensionTrait;
19

20
    /**
21
     * @var string[]
22
     * TODO move to interface const
23
     */
24
    protected array $centuries = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII', 'XIX', 'XX', 'XXI'];
25

26
    public function dateTime(DateTimeInterface|string $until = 'now', ?string $timezone = null): \DateTimeInterface
27
    {
28
        return $this->setTimezone(
10✔
29
            $this->getTimestampDateTime($this->unixTime($until)),
10✔
30
            $timezone,
10✔
31
        );
10✔
32
    }
33

34
    public function dateTimeAD(DateTimeInterface|string $until = 'now', ?string $timezone = null): \DateTimeInterface
35
    {
36
        $min = (PHP_INT_SIZE > 4) ? -62135597361 : -PHP_INT_MAX;
1✔
37

38
        return $this->setTimezone(
1✔
39
            $this->getTimestampDateTime($this->randomizer->getInt($min, $this->getTimestamp($until))),
1✔
40
            $timezone,
1✔
41
        );
1✔
42
    }
43

44
    public function dateTimeBetween(DateTimeInterface|string $from = '-30 years', DateTimeInterface|string $until = 'now', ?string $timezone = null): \DateTimeInterface
45
    {
46
        $start = $this->getTimestamp($from);
12✔
47
        $end = $this->getTimestamp($until);
12✔
48

49
        if ($start > $end) {
12✔
50
            throw new ExtensionArgumentException('"$from" must be anterior to "$until".');
1✔
51
        }
52

53
        $timestamp = $this->randomizer->getInt($start, $end);
11✔
54

55
        return $this->setTimezone(
11✔
56
            $this->getTimestampDateTime($timestamp),
11✔
57
            $timezone,
11✔
58
        );
11✔
59
    }
60

61
    public function dateTimeInInterval(
62
        DateTimeInterface|string $from = '-30 years',
63
        DateInterval|string $interval = '+5 days',
64
        ?string $timezone = null
65
    ): \DateTimeInterface {
66
        $intervalObject = $interval instanceof DateInterval ? $interval : DateInterval::createFromDateString($interval);
2✔
67
        $datetime = $from instanceof \DateTimeInterface ? $from : new \DateTimeImmutable((string) $from);
2✔
68

69
        // @phpstan-ignore-next-line
70
        $other = $datetime->add($intervalObject);
2✔
71

72
        $begin = min($datetime, $other);
2✔
73
        $end = $datetime === $begin ? $other : $datetime;
2✔
74

75
        return $this->dateTimeBetween($begin, $end, $timezone);
2✔
76
    }
77

78
    public function dateTimeThisWeek(DateTimeInterface|string $until = 'sunday this week', ?string $timezone = null): \DateTimeInterface
79
    {
80
        $from = new DateTimeImmutable();
1✔
81

82
        if ($timezone !== null) {
1✔
83
            $from = $from->setTimezone(new DateTimeZone($timezone));
1✔
84
        }
85

86
        if ($from->format('w') !== '1') {
1✔
87
            $from = $from->modify('last monday');
×
88
        }
89

90
        $from = $from->setTime(0, 0, 0);
1✔
91

92
        return $this->dateTimeBetween($from, $until, $timezone);
1✔
93
    }
94

95
    public function dateTimeThisMonth(DateTimeInterface|string $until = 'last day of this month', ?string $timezone = null): \DateTimeInterface
96
    {
97
        return $this->dateTimeBetween('first day of this month', $until, $timezone);
1✔
98
    }
99

100
    public function dateTimeThisYear(DateTimeInterface|string $until = 'last day of december', ?string $timezone = null): \DateTimeInterface
101
    {
102
        return $this->dateTimeBetween('first day of january', $until, $timezone);
1✔
103
    }
104

105
    public function dateTimeThisDecade(DateTimeInterface|string $until = 'now', ?string $timezone = null): \DateTimeInterface
106
    {
107
        $year = floor(date('Y') / 10) * 10;
1✔
108

109
        return $this->dateTimeBetween("first day of january $year", $until, $timezone);
1✔
110
    }
111

112
    public function dateTimeThisCentury(DateTimeInterface|string $until = 'now', ?string $timezone = null): \DateTimeInterface
113
    {
114
        $year = floor(date('Y') / 100) * 100;
1✔
115

116
        return $this->dateTimeBetween("first day of january $year", $until, $timezone);
1✔
117
    }
118

119
    public function date(string $format = 'Y-m-d', DateTimeInterface|string $until = 'now'): string
120
    {
121
        return $this->dateTime($until)->format($format);
9✔
122
    }
123

124
    public function time(string $format = 'H:i:s', DateTimeInterface|string $until = 'now'): string
125
    {
126
        return $this->date($format, $until);
1✔
127
    }
128

129
    public function unixTime(DateTimeInterface|string $until = 'now'): int
130
    {
131
        return $this->randomizer->getInt(0, $this->getTimestamp($until));
11✔
132
    }
133

134
    public function iso8601(DateTimeInterface|string $until = 'now'): string
135
    {
136
        return $this->date(DateTimeInterface::ATOM, $until);
1✔
137
    }
138

139
    public function amPm(DateTimeInterface|string $until = 'now'): string
140
    {
141
        return $this->date('a', $until);
1✔
142
    }
143

144
    public function dayOfMonth(DateTimeInterface|string $until = 'now'): string
145
    {
146
        return $this->date('d', $until);
1✔
147
    }
148

149
    public function dayOfWeek(DateTimeInterface|string $until = 'now'): string
150
    {
151
        return $this->date('l', $until);
1✔
152
    }
153

154
    public function month(DateTimeInterface|string $until = 'now'): string
155
    {
156
        return $this->date('m', $until);
1✔
157
    }
158

159
    public function monthName(DateTimeInterface|string $until = 'now'): string
160
    {
161
        return $this->date('F', $until);
1✔
162
    }
163

164
    public function year(DateTimeInterface|string $until = 'now'): string
165
    {
166
        return $this->date('Y', $until);
1✔
167
    }
168

169
    public function century(): string
170
    {
171
        return $this->randomizer->randomElement($this->centuries);
1✔
172
    }
173

174
    public function timezone(): string
175
    {
176
        return $this->randomizer->randomElement(\DateTimeZone::listIdentifiers());
1✔
177
    }
178

179
    /**
180
     * Get the POSIX-timestamp of a DateTime or string (can be epoch as "1733785884").
181
     */
182
    protected function getTimestamp(\DateTimeInterface|string $until = 'now'): int
183
    {
184
        if (is_numeric($until)) {
24✔
185
            return (int) $until;
1✔
186
        }
187

188
        if ($until instanceof \DateTimeInterface) {
24✔
189
            return $until->getTimestamp();
19✔
190
        }
191

192
        return $this->getDateTimeFromString($until)->getTimestamp();
9✔
193
    }
194

195
    /**
196
     * Get a DateTimeImmutable created based on a POSIX-timestamp.
197
     *
198
     * @param int $timestamp the UNIX / POSIX-compatible timestamp
199
     * @throws \DateMalformedStringException
200
     */
201
    protected function getTimestampDateTime(int $timestamp): \DateTimeInterface
202
    {
203
        return new \DateTimeImmutable('@' . $timestamp);
22✔
204
    }
205

206
    protected function resolveTimezone(?string $timezone): string
207
    {
208
        if ($timezone === null) {
22✔
209
            return date_default_timezone_get();
11✔
210
        }
211

212
        $timezones = \DateTimeZone::listIdentifiers();
11✔
213
        if (in_array($timezone, $timezones, true)) {
11✔
214
            return $timezone;
10✔
215
        }
216

217
        throw new ExtensionArgumentException('"$timezone" is not a valid timezone.');
1✔
218
    }
219

220
    /**
221
     * Internal method to set the timezone on a DateTime object.
222
     */
223
    protected function setTimezone(\DateTimeInterface $dateTime, ?string $timezone): \DateTimeInterface
224
    {
225
        $timezone = $this->resolveTimezone($timezone);
22✔
226

227
        // @phpstan-ignore-next-line
228
        return $dateTime->setTimezone(new \DateTimeZone($timezone));
21✔
229
    }
230

231
    protected function getDateTimeFromString(string $dateString): DateTimeInterface
232
    {
233
        try {
234
            return new DateTimeImmutable($dateString);
9✔
235
        } catch (\Throwable $e) {
×
236
            throw new ExtensionArgumentException('Invalid datetime string given.', $e->getCode(), $e);
×
237
        }
238
    }
239
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc