-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put in a workaround for the DateTime::modify() bug and moved the mini…
…mum PHP version back to 5.3.0. See SF Support Requests #667. git-svn-id: http://svn.code.sf.net/p/mrbs/code/mrbs/trunk@2973 25f7e783-145e-4f2d-a9f8-dd7182bfe9c7
- Loading branch information
cimorrison
committed
Jan 21, 2015
1 parent
1ec9be4
commit cb12673
Showing
7 changed files
with
105 additions
and
8 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
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
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,75 @@ | ||
<?php | ||
namespace MRBS; | ||
|
||
class DateTime extends \DateTime | ||
{ | ||
// Workaround for a bug that was fixed in PHP 5.3.6 | ||
// Only supports a limited range of $modify strings for PHP < 5.3.6. Throws | ||
// an exception if passed a string that it can't handle (maybe it should just | ||
// generate a warning? - that's what the global DateTime does) | ||
public function modify($modify) | ||
{ | ||
if (version_compare(PHP_VERSION, '5.3.6') >= 0) | ||
{ | ||
return parent::modify($modify); | ||
} | ||
|
||
$date = getdate($this->getTimestamp()); | ||
$modification = self::parse($modify); | ||
|
||
foreach ($modification as $unit => $amount) | ||
{ | ||
switch($amount['mode']) | ||
{ | ||
case 'absolute': | ||
$date[$unit] = $amount['quantity']; | ||
break; | ||
case 'relative': | ||
$date[$unit] = $date[$unit] + $amount['quantity']; | ||
break; | ||
default: | ||
throw new Exception ("Unknown mode '" . $amount['mode'] . "'"); | ||
break; | ||
} | ||
} | ||
|
||
$modified_timestamp = mktime($date['hours'], $date['minutes'], $date['seconds'], | ||
$date['mon'], $date['mday'], $date['year']); | ||
|
||
return $this->setTimestamp($modified_timestamp); | ||
} | ||
|
||
|
||
// Parse the $modify string and return an array of any modifications that are necessary. | ||
// The array is indexed at the top level by 'hours', 'minutes', 'seconds', 'mon', 'mday' and | ||
// 'year' - ie the same keys that the output of getdate() uses. Each value is itself an array, | ||
// indexed by 'mode' (can be 'relative' or 'absolute') and then 'quantity'. If the mode is | ||
// relative then the quantity is added to the original, if absolute then it replaces the original. | ||
private static function parse($modify) | ||
{ | ||
$modify = self::map($modify); | ||
|
||
// Test for a simple hh:mm pattern (or hhmm or hh.mm) | ||
$pattern = '/([01][0-9]|[2][0-3])[.:]?([0-5][0-9])/'; | ||
if (preg_match($pattern, $modify, $matches)) | ||
{ | ||
return array('hours' => array('mode' => 'absolute', | ||
'quantity' => $matches[1]), | ||
'minutes' => array('mode' => 'absolute', | ||
'quantity' => $matches[2])); | ||
} | ||
|
||
// Could add more tests later if need be. | ||
throw new Exception("Modify string '$modify' not supported by MRBS"); | ||
} | ||
|
||
|
||
// Replace some simple modify strings with their numeric alternatives. | ||
private static function map($modify) | ||
{ | ||
$mappings = array('midnight' => '00:00', | ||
'noon' => '12:00'); | ||
|
||
return (isset($mappings[$modify])) ? $mappings[$modify] : $modify; | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
// $Id$ | ||
|
||
spl_autoload_register(function ($class) { | ||
|
||
$base_dir = __DIR__ . '/'; | ||
|
||
// Replace namespace separators with directory separators. | ||
// Append '.php' | ||
$file = $base_dir . str_replace('\\', '/', $class) . '.php'; | ||
|
||
if (file_exists($file)) | ||
{ | ||
require $file; | ||
} | ||
}); |
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