Skip to content

Commit

Permalink
fix: prevent timezone uninitialized error getting date
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanveen committed Jul 21, 2024
1 parent 66b0949 commit 6149806
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
35 changes: 20 additions & 15 deletions bloembraaden/help/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,33 @@
class Date
{
public static function getDate( string $value ): ?\DateTimeImmutable {
$timezone = new \DateTimeZone( Setup::$timezone );

// parse the date, it should be YYYY-MM-DD HH:MM:SS.milliseconds+timezone diff compared to UTC
if ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s.u O', $value, $timezone ) ) ) {
return $dt;
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d\TH:i:s.u O', $value, $timezone ) ) ) { // official, used by eg Instagram
return $dt;
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s O', $value, $timezone ) ) ) {
// ‘O’ means timezone, dates in Bloembraaden generally have the timestamp part present,
// only user input probably has not
if ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s.u O', $value ) ) ) {
return $dt;
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d\TH:i:s O', $value, $timezone ) ) ) { // official, used by eg Instagram
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d\TH:i:s.u O', $value ) ) ) { // official, used by eg Instagram
return $dt;
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s.u', $value, $timezone ) ) ) {
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s O', $value ) ) ) {
return $dt;
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s', $value, $timezone ) ) ) {
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d\TH:i:s O', $value ) ) ) { // official, used by eg Instagram
return $dt;
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d H:i', $value, $timezone ) ) ) {
return $dt;
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d', $value, $timezone ) ) ) {
return $dt->setTime( 12, 0 ); // when no time is given, set in the middle
} else {
// when no timestamp, this is user input, an instance should be loaded and its timestamp used
$timezone = new \DateTimeZone( Setup::$timezone );

if ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s.u', $value, $timezone ) ) ) {
return $dt;
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s', $value, $timezone ) ) ) {
return $dt;
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d H:i', $value, $timezone ) ) ) {
return $dt;
} elseif ( ( $dt = \DateTimeImmutable::createFromFormat( 'Y-m-d', $value, $timezone ) ) ) {
return $dt->setTime( 12, 0 ); // when no time is given, set in the middle
}
}

//if ($dt === false or array_sum($dt::getLastErrors())) {}
//if ($dt === false || array_sum($dt::getLastErrors())) {}
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion bloembraaden/help/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public static function loadInstanceSettings(Instance $I): void
self::$timezone = 'Europe/Amsterdam'; // this is a correct timezone and for now the default
}
if (false === self::getMainDatabaseConnection()->exec(sprintf('SET timezone TO \'%s\';', self::$timezone))) {
Help::addError(new Exception('failed to set timezone'));
Help::addError(new Exception('Failed to set timezone'));
} else {
date_default_timezone_set(self::$timezone);
}
Expand Down

0 comments on commit 6149806

Please sign in to comment.