This repository has been archived by the owner on Sep 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SDPAP-8095] Added date and time conversion for news and grant conten…
…t types. (#214) * [SDPAP-8095] Added date and time conversion for news and grant content types.
- Loading branch information
1 parent
e22b2a0
commit a234d27
Showing
6 changed files
with
87 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,7 @@ workflows: | |
branches: | ||
ignore: | ||
- reference | ||
- phpunit_tests | ||
|
||
mergetoreference: | ||
jobs: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Drupal\tide_landing_page\Helper; | ||
|
||
use Drupal\Core\Datetime\DrupalDateTime; | ||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; | ||
|
||
/** | ||
* Helper functions. | ||
*/ | ||
class TideLandingPageHelper { | ||
|
||
/** | ||
* Convert given date to local time format. | ||
* | ||
* @param string $date | ||
* The date element to process. | ||
* | ||
* @return string | ||
* The converted date. | ||
*/ | ||
public static function localDateAndTimeFormatter($date) { | ||
// Parse date with GMT timezone. | ||
$storage_tz = DateTimeItemInterface::STORAGE_TIMEZONE; | ||
$drupal_date_time = new DrupalDateTime($date, $storage_tz); | ||
// Convert to local timezone. | ||
$system_tz = \Drupal::service('config.factory')->get('system.date')->get('timezone.default'); | ||
$date_formatter = \Drupal::service('date.formatter'); | ||
$converted_date = $date_formatter->format($drupal_date_time->getTimeStamp(), 'custom', 'Y-m-d H:i:s', $system_tz); | ||
return $converted_date; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use Drupal\test_helpers\TestHelpers; | ||
use Drupal\Tests\UnitTestCase; | ||
use Drupal\tide_landing_page\Helper\TideLandingPageHelper; | ||
|
||
/** | ||
* Tests the TideLandingPageHelper class. | ||
* | ||
* @group tide_landing_page | ||
*/ | ||
class TideLandingPageHelperTest extends UnitTestCase { | ||
|
||
/** | ||
* Tests the localDateAndTimeFormatter method. | ||
*/ | ||
public function testLocalDateAndTimeFormatter() { | ||
TestHelpers::service('date.formatter')->stubSetFormat('medium', 'Medium', 'd.m.Y'); | ||
TestHelpers::service('config.factory')->stubSetConfig('system.date', ['timezone.default' => 'Australia/Melbourne']); | ||
// Instantiate the TideLandingPageHelper class with the mocked objects. | ||
$helper = new TideLandingPageHelper(); | ||
// Call the method to be tested. | ||
$result = $helper->localDateAndTimeFormatter('2023-09-29T13:59:00'); | ||
// Assert that the result matches the expected output. | ||
$this->assertEquals('2023-09-29 23:59:00', $result); | ||
} | ||
|
||
} |