-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDT.php
More file actions
327 lines (294 loc) · 10.4 KB
/
Copy pathDT.php
File metadata and controls
327 lines (294 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
namespace App\Common;
use App\Exceptions\ServerException;
use DateTimeImmutable;
use Exception;
use Carbon\Carbon;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use function config;
use function now;
use function today;
/**
* Date/Time helper class
*/
class DT
{
const DATE_TIME_FORMAT = 'Y-m-d\TH:i:s.u\Z';
const LEGACY_DATE_TIME_FORMAT = 'Y-m-d\TH:i:s\Z';
const DATE_FORMAT = 'Y-m-d';
const LOCAL_TIME_ZONE = "Europe/Amsterdam";
const TIME_FORMAT = "H:i";
/**
* Returns the current date and time in the application's default timezone (e.g. from config/app.timezone).
*
* @return Carbon
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function now(): Carbon
{
return now(config('app.timezone'));
}
/**
* Returns the current date in the application's default timezone (e.g. from config/app.timezone).
* @return Carbon
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function today(): Carbon
{
return today(config('app.timezone'));
}
/**
* Creates a copy of the $date parameter and adds the number of days given by the $days parameter.
*
* @param Carbon $date The date to initialize the new Carbon object from.
* @param int $days The number of days to add.
* @return Carbon Returns a new Carbon instance.
*/
public static function addDaysToCopy(Carbon $date, int $days): Carbon
{
return $date->copy()->addDays($days);
}
/**
* Creates a new Carbon instance from a timestamp.
*
* @param int $timestamp The timestamp.
* @return Carbon Returns a new Carbon instance.
*/
public static function fromTimestamp(int $timestamp): Carbon
{
return Carbon::createFromTimestamp($timestamp);
}
/**
* Parses a date string in the format Y-m-d and returns a new Carbon object in the application's timezone.
* @param string $string The string to parse.
* @return Carbon
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public static function fromDateString(string $string): Carbon
{
$date = self::fromString($string, self::DATE_FORMAT);
if ($date) {
return self::fromString($string, self::DATE_FORMAT)->startOfDay();
}
throw new Exception("Invalid date/time string value. Can not parse date string.");
}
/**
* Parses a date-time string given in the format Y-m-d\TH:i:s.u\Z in the application's timezone.
*
* @param string $string The string to parse.
* @return Carbon|false
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @noinspection PhpUnused
*/
public static function fromISOString(string $string): bool|Carbon
{
return self::fromString($string, self::DATE_TIME_FORMAT);
}
/**
* @param string $string
* @return false|Carbon
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function fromLegacyISOString(string $string): bool|Carbon
{
return self::fromString($string, self::LEGACY_DATE_TIME_FORMAT);
}
/**
* Parses a date-time string given in the format Y-m-d\TH:i:s.u\Z in the local timezone defined by the
* LOCAL_TIME_ZONE constant. Then converts the value to the application's timezone.
*
* @param string $string The string to parse.
* @return Carbon|false
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function fromLocalISOString(string $string): bool|Carbon
{
return Carbon::createFromFormat(self::DATE_TIME_FORMAT, $string, self::LOCAL_TIME_ZONE)->timezone(config('app.timezone'));
}
/**
* Creates a Carbon object from a string. If the string contains no timezone information this method assumes
* local timezone.
*
* @param string $time The string representation of the date/time value
* @return Carbon
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function fromLocalString(string $time): Carbon
{
$result = new Carbon($time, self::LOCAL_TIME_ZONE);
$result->setTimezone(config('app.timezone'));
return $result;
}
/**
* Parses a localized string, e.g. Feb 02, 2022 CET 00:00 and returns a Carbon object in the application's time zone (UTC).
*
* @param string $string The string to be parsed.
* @return Carbon Returns a Carbon object if the parsing was successful.
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function parseLocalString(string $string): Carbon
{
return Carbon::parseFromLocale($string)->timezone(config('app.timezone'));
}
/**
* Create a new Carbon instance from an existing Carbon instance
* @param Carbon $dateTime
* @return Carbon
*/
public static function createFromCarbon(Carbon $dateTime): Carbon
{
$result = new Carbon(null, $dateTime->getTimezone());
$result->setTimestamp($dateTime->getTimestamp());
return $result;
}
/**
* Creates a copy in the local timezone, takes the start of the day and returns it in the application's timezone.
*
* @param Carbon $value A Carbon object (may include time info) indicating a date in the local timezone and convert
* it to the application's timezone.
*
* @return Carbon Returns a new Carbon instance in the application's timezone.
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function startOfDayUtc(Carbon $value): Carbon
{
return $value->copy()->timezone(self::LOCAL_TIME_ZONE)->startOfDay()->timezone(config('app.timezone'));
}
/**
* Creates a DateTime object in the INTERNAL_TIME_ZONE.
*
* @param string $time A string defining the time for initialization. Could
* be a formatted date/time string or any of the string literals supported
* by the Carbon/DateTime constructor. Must be provided in UTC. If the timezone is
* specified in the string it should be either "Z", "+00:00" or "UTC".
* Otherwise, an exception is thrown.
*
* @return Carbon
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ServerException Throws an exception if the $time parameter
* contains the timezone and if it differs from "Z", "+00:00" or "UTC".
*/
public static function fromUTCString(string $time): Carbon
{
$result = new Carbon($time, config('app.timezone'));
$timezone = $result->getTimezone()->getName();
if (!empty($timezone) && ($timezone !== "Z" && $timezone !== "UTC" && $timezone !== "+00:00")) {
throw new ServerException("The date/time provided must be in the UTC but is in '$timezone' timezone.");
}
return $result;
}
/**
* @return int
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function timestampNowInMilliseconds(): int
{
$now = new Carbon("now", config('app.timezone'));
$time = $now->setDate(1970, 01, 01);
return $time->getTimestamp() * 1000;
}
/**
* @return int
*/
public static function localTimestampNowInMilliseconds(): int
{
$now = new Carbon("now", self::LOCAL_TIME_ZONE);
$time = $now->setDate(1970, 01, 01);
return $time->getTimestamp() * 1000;
}
/**
* Parses string date-time info.
*
* @param string $string The string to parse.
* @param string $format The date/time format that is expected.
* @return Carbon|false
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function fromString(string $string, string $format): bool|Carbon
{
return Carbon::createFromFormat($format, $string, config('app.timezone'));
}
/**
* Formats a Carbon instance based on the DATE_TIME_FORMAT.
*
* @param Carbon $dateTime The object to be formatted.
* @return string
*/
public static function toString(Carbon $dateTime): string
{
return $dateTime->format(self::DATE_TIME_FORMAT);
}
/**
* Formats a Carbon instance based on the LEGACY_DATE_TIME_FORMAT.
*
* @param Carbon $dateTime The object to be formatted.
* @return string
*/
public static function toLegacyString(Carbon $dateTime): string
{
return $dateTime->format(self::LEGACY_DATE_TIME_FORMAT);
}
/**
* Formats a date according to the DATE_FORMAT format. Does not include the time in the output string.
* @param Carbon $dateTime The date/time to be formatted.
* @return string Returns a string containing the date in the DATE_FORMAT format ("Y-m-d").
*/
public static function toDateString(Carbon $dateTime): string
{
return $dateTime->format(self::DATE_FORMAT);
}
/**
* Formats a date/time Carbon object to a local time string as 'H:i' (e.g. '10:23').
* @param Carbon $dateTime The date/time to be formatted.
* @return string
*/
public static function toLocalTimeString(Carbon $dateTime): string
{
return self::formatToLocal( $dateTime, self::TIME_FORMAT);
}
public static function toStringForPlanon(Carbon $dateTime): string
{
return self::formatToLocal($dateTime, "c");
}
public static function isToday(Carbon $value): bool
{
return $value->isToday();
}
/**
* @param Carbon $value
* @param int $atLeastDays
* @return bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function isFuture(Carbon $value, int $atLeastDays): bool
{
$d = DateTimeImmutable::createFromMutable($value)->setTime(0, 0);
$now = self::now()->setTime(0, 0);
if ($value->isPast()) {
return false;
}
return $d->diff($now)->days > $atLeastDays;
}
private static function formatToLocal(Carbon $dateTime, string $format): string
{
$c = new Carbon($dateTime);
$c->setTimezone(self::LOCAL_TIME_ZONE);
return $c->format($format);
}
}