Skip to content

Commit 86b40bd

Browse files
committed
task: Cleanup and improve datetime helper class
1 parent 14e71ad commit 86b40bd

File tree

1 file changed

+46
-53
lines changed

1 file changed

+46
-53
lines changed

app/Core/Support/DateTimeHelper.php

+46-53
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
class DateTimeHelper extends CarbonImmutable
2525
{
26-
private Language $language;
27-
2826
private string $userTimezone;
2927

3028
private string $userLanguage;
@@ -33,13 +31,11 @@ class DateTimeHelper extends CarbonImmutable
3331

3432
private string $userTimeFormat;
3533

36-
private Environment $config;
37-
3834
private readonly string $dbTimezone;
3935

4036
private readonly string $dbFormat;
4137

42-
private CarbonImmutable $datetime;
38+
private ?CarbonImmutable $datetime;
4339

4440
/**
4541
* Constructs a new instance of the class.
@@ -53,19 +49,19 @@ public function __construct($time = null, $tz = null)
5349
{
5450
parent::__construct($time, $tz);
5551

56-
$this->language = app()->make(Language::class);
57-
$this->config = app()->make(Environment::class);
52+
$language = app()->make(Language::class);
53+
$config = app()->make(Environment::class);
5854

5955
// These are read only for a reason
6056
$this->dbFormat = 'Y-m-d H:i:s';
6157
$this->dbTimezone = 'UTC';
6258

6359
// Session is set in middleware, unlikely to not be set but just in case set defaults.
64-
$this->userTimezone = session('usersettings.timezone') ?? $this->config->defaultTimezone;
65-
$this->userLanguage = str_replace('-', '_', (session('usersettings.language') ?? $this->config->language));
60+
$this->userTimezone = session('usersettings.timezone') ?? $config->defaultTimezone;
61+
$this->userLanguage = str_replace('-', '_', (session('usersettings.language') ?? $config->language));
6662

67-
$this->userDateFormat = session('usersettings.date_format') ?? $this->language->__('language.dateformat');
68-
$this->userTimeFormat = session('usersettings.time_format') ?? $this->language->__('language.timeformat');
63+
$this->userDateFormat = session('usersettings.date_format') ?? $language->__('language.dateformat');
64+
$this->userTimeFormat = session('usersettings.time_format') ?? $language->__('language.timeformat');
6965
}
7066

7167
/**
@@ -76,51 +72,54 @@ public function __construct($time = null, $tz = null)
7672
* Defaults to an empty string. Can also be one of start|end to denote start or end time of
7773
* day
7874
* @return CarbonImmutable The parsed date and time in user timezone as a CarbonImmutable object.
75+
*
76+
* @throws InvalidDateException
7977
*/
8078
public function parseUserDateTime(string $userDate, string $userTime = ''): CarbonImmutable
8179
{
8280

81+
// Initialize result variable to null
82+
$this->datetime = null;
83+
84+
// Validate input string
8385
if (! $this->isValidDateString($userDate)) {
8486
throw new InvalidDateException('The string is not a valid date time string to parse as user datetime string', $userDate);
8587
}
8688

87-
// Check if provided date is iso8601 (from API)
88-
try {
89-
$this->datetime = CarbonImmutable::createFromFormat(DateTime::ISO8601, $userDate);
90-
91-
return $this->datetime;
92-
} catch (\Exception $e) {
93-
// Do nothing
94-
}
95-
96-
// Also try
97-
try {
98-
$this->datetime = CarbonImmutable::createFromFormat(DateTime::ISO8601_EXPANDED, $userDate);
99-
100-
return $this->datetime;
101-
} catch (\Exception $e) {
102-
// Do nothing
89+
// Define standard formats to try first
90+
$standardFormats = [
91+
DateTime::ATOM,
92+
DateTime::ISO8601,
93+
DateTime::ISO8601_EXPANDED,
94+
DateTime::W3C,
95+
];
96+
97+
// Try standard formats first
98+
foreach ($standardFormats as $format) {
99+
try {
100+
$this->datetime = CarbonImmutable::createFromFormat($format, $userDate);
101+
if ($this->datetime !== false && $this->datetime !== null) {
102+
return $this->datetime;
103+
}
104+
} catch (\Exception $e) {
105+
// Continue to next format
106+
}
103107
}
104108

105-
// Lastly try w3c
106-
try {
107-
$this->datetime = CarbonImmutable::createFromFormat(DateTime::W3C, $userDate);
108-
109-
return $this->datetime;
110-
} catch (\Exception $e) {
111-
// Do nothing
112-
}
109+
// If no standard format worked, handle user format cases
110+
$locale = substr($this->userLanguage, 0, 2);
111+
$trimmedDate = trim($userDate);
113112

114-
if ($userTime == 'start') {
115-
$this->datetime = CarbonImmutable::createFromLocaleFormat('!'.$this->userDateFormat, substr($this->userLanguage, 0, 2), trim($userDate), $this->userTimezone)
113+
if ($userTime === 'start') {
114+
$this->datetime = CarbonImmutable::createFromLocaleFormat('!'.$this->userDateFormat, $locale, $trimmedDate, $this->userTimezone)
116115
->startOfDay();
117-
} elseif ($userTime == 'end') {
118-
$this->datetime = CarbonImmutable::createFromLocaleFormat('!'.$this->userDateFormat, substr($this->userLanguage, 0, 2), trim($userDate), $this->userTimezone)
116+
} elseif ($userTime === 'end') {
117+
$this->datetime = CarbonImmutable::createFromLocaleFormat('!'.$this->userDateFormat, $locale, $trimmedDate, $this->userTimezone)
119118
->endOfDay();
120-
} elseif ($userTime == '') {
121-
$this->datetime = CarbonImmutable::createFromLocaleFormat('!'.$this->userDateFormat, substr($this->userLanguage, 0, 2), trim($userDate), $this->userTimezone);
119+
} elseif ($userTime === '') {
120+
$this->datetime = CarbonImmutable::createFromLocaleFormat('!'.$this->userDateFormat, $locale, $trimmedDate, $this->userTimezone);
122121
} else {
123-
$this->datetime = CarbonImmutable::createFromLocaleFormat('!'.$this->userDateFormat.' '.$this->userTimeFormat, substr($this->userLanguage, 0, 2), trim($userDate.' '.$userTime), $this->userTimezone);
122+
$this->datetime = CarbonImmutable::createFromLocaleFormat('!'.$this->userDateFormat.' '.$this->userTimeFormat, $locale, trim($trimmedDate.' '.$userTime), $this->userTimezone);
124123
}
125124

126125
return $this->datetime;
@@ -190,19 +189,13 @@ public function setCarbonDate(CarbonImmutable|Carbon $date): string|CarbonImmuta
190189
/**
191190
* isValidDateString - checks if a given string is a valid date and time string
192191
*
193-
* @param string|null $dateTimeString The date and time string to be validated
194-
* @return bool Returns true if the string is a valid date and time string, false otherwise
192+
* @param ?string $dateTimeString The date and time string to be validated
193+
* @return bool Returns true if the string is a valid string that is worth sending to a parser, false otherwise
195194
*/
196195
public function isValidDateString(?string $dateTimeString): bool
197196
{
198-
if (
199-
empty($dateTimeString) === false
200-
&& $dateTimeString != '1969-12-31 00:00:00'
201-
&& $dateTimeString != '0000-00-00 00:00:00'
202-
) {
203-
return true;
204-
}
205-
206-
return false;
197+
return empty($dateTimeString) === false
198+
&& $dateTimeString !== '1969-12-31 00:00:00'
199+
&& $dateTimeString !== '0000-00-00 00:00:00';
207200
}
208201
}

0 commit comments

Comments
 (0)