Skip to content

Commit

Permalink
Added timezone argument to GraphQL FormattableDate type
Browse files Browse the repository at this point in the history
  • Loading branch information
bryannielsen committed Aug 9, 2024
1 parent 70a57bf commit 7953531
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- Timezone argument for GraphQL FormattableDate types

### Fixed

- Bug where integer dates were not handled properly when used as a GraphQL FormattableDate
Expand Down
15 changes: 15 additions & 0 deletions src/Api/Graph/Fields/FormattableDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function args(): array
'type' => Type::boolean(),
'defaultValue' => false,
],
'timezone' => [
'type' => Type::string(),
'defaultValue' => null,
'description' => 'Timezone defaults to UTC. Use "SYSTEM" for the value set in ExpressionEngine',
],
];
}

Expand All @@ -48,6 +53,16 @@ protected function resolve($root, array $args): ?string
return null;
}

if ($args['timezone']) {
if ($args['timezone'] === 'SYSTEM') {
$args['timezone'] = ee()->config->item('default_site_timezone') ?: date_default_timezone_get();
}

if (in_array($args['timezone'], \DateTimeZone::listIdentifiers())) {
$date->setTimezone($args['timezone']);
}
}

if ($args['relative']) {
return $date->diffForHumans();
}
Expand Down

0 comments on commit 7953531

Please sign in to comment.