-
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.
Add support for converting to a DateTime and returning the active tim…
…e zone (#2)
- Loading branch information
1 parent
04517ce
commit 196ed4b
Showing
2 changed files
with
40 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -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 | ||
*/ | ||
|
@@ -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 | ||
*/ | ||
|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -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 | ||
*/ | ||
|
@@ -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 | ||
*/ | ||
|
@@ -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()); | ||
} | ||
} |