Skip to content

Commit

Permalink
Add support for converting to a DateTime and returning the active tim…
Browse files Browse the repository at this point in the history
…e zone (#2)
  • Loading branch information
EreMaijala authored May 16, 2022
1 parent 04517ce commit 196ed4b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @package Date
* @author Demian Katz <[email protected]>
* @author Luke O'Sullivan <[email protected]>
* @author Ere Maijala <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
Expand All @@ -38,6 +39,7 @@
* @package Date
* @author Demian Katz <[email protected]>
* @author Luke O'Sullivan <[email protected]>
* @author Ere Maijala <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
Expand Down Expand Up @@ -93,6 +95,21 @@ public function __construct(array $config = [])
* @return string A re-formatted time string
*/
public function convert($inputFormat, $outputFormat, $dateString)
{
$date = $this->convertToDateTime($inputFormat, $dateString);
return $date->format($outputFormat);
}

/**
* Generic method for conversion of a time / date string to a DateTime
*
* @param string $inputFormat The format of the time string to be changed
* @param string $dateString The date string
*
* @throws DateException
* @return DateTime A DateTime object
*/
public function convertToDateTime($inputFormat, $dateString)
{
// These are date formats that we definitely know how to handle, and some
// benefit from special processing. However, items not found in this list
Expand Down Expand Up @@ -132,7 +149,7 @@ public function convert($inputFormat, $outputFormat, $dateString)

if ($errors['warning_count'] == 0 && $errors['error_count'] == 0 && $date) {
$date->setTimeZone($this->timezone);
return $date->format($outputFormat);
return $date;
}
throw new DateException($this->getDateExceptionMessage($errors));
}
Expand Down Expand Up @@ -238,4 +255,14 @@ public function convertToDisplayTimeAndDate($createFormat, $timeString,
return $this->convertToDisplayTime($createFormat, $timeString)
. $separator . $this->convertToDisplayDate($createFormat, $timeString);
}

/**
* Get the active time zone
*
* @return DateTimeZone
*/
public function getTimeZone(): DateTimeZone
{
return $this->timezone;
}
}
12 changes: 12 additions & 0 deletions tests/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @category VuFind
* @package Tests
* @author Demian Katz <[email protected]>
* @author Ere Maijala <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*/
Expand All @@ -36,6 +37,7 @@
* @category VuFind
* @package Tests
* @author Demian Katz <[email protected]>
* @author Ere Maijala <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*/
Expand Down Expand Up @@ -123,6 +125,15 @@ protected function runTests()
$this->assertEquals(
'01-2001', $date->convertFromDisplayDate('m-Y', '01-02-2001')
);
$usDateTime = new \DateTime('now', new \DateTimeZone('America/New_York'));
$this->assertEquals(
$usDateTime->format('Y-m-d H:i:s'),
$date->convert('U', 'Y-m-d H:i:s', $usDateTime->getTimestamp())
);
$dateTime = $date->convertToDateTime('U', $usDateTime->getTimestamp());
$this->assertInstanceOf(\DateTime::class, $dateTime);
$this->assertEquals('America/New_York', $dateTime->getTimezone()->getName());
$this->assertEquals('America/New_York', $date->getTimeZone()->getName());

// Check for proper handling of known problems:
try {
Expand Down Expand Up @@ -153,5 +164,6 @@ protected function runTests()
'29-11-1973--23:34:39',
$date2->convertToDisplayDateAndTime('U', 123456879, '--')
);
$this->assertEquals('Europe/Helsinki', $date2->getTimeZone()->getName());
}
}

0 comments on commit 196ed4b

Please sign in to comment.