diff --git a/src/Converter.php b/src/Converter.php index 8763df4..875451e 100644 --- a/src/Converter.php +++ b/src/Converter.php @@ -23,6 +23,7 @@ * @package Date * @author Demian Katz * @author Luke O'Sullivan + * @author Ere Maijala * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link https://vufind.org/wiki/development Wiki */ @@ -38,6 +39,7 @@ * @package Date * @author Demian Katz * @author Luke O'Sullivan + * @author Ere Maijala * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link https://vufind.org/wiki/development Wiki */ @@ -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 @@ -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)); } @@ -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; + } } diff --git a/tests/ConverterTest.php b/tests/ConverterTest.php index 41165ba..f05d4d1 100644 --- a/tests/ConverterTest.php +++ b/tests/ConverterTest.php @@ -22,6 +22,7 @@ * @category VuFind * @package Tests * @author Demian Katz + * @author Ere Maijala * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link https://vufind.org Main Page */ @@ -36,6 +37,7 @@ * @category VuFind * @package Tests * @author Demian Katz + * @author Ere Maijala * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link https://vufind.org Main Page */ @@ -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 { @@ -153,5 +164,6 @@ protected function runTests() '29-11-1973--23:34:39', $date2->convertToDisplayDateAndTime('U', 123456879, '--') ); + $this->assertEquals('Europe/Helsinki', $date2->getTimeZone()->getName()); } }