Improve parsing of RFC5545 durations

The previous regex failed to parse PT15M which is a valid duration.

This version more correctly implements:
  https://tools.ietf.org/html/rfc5545#section-3.3.6

It isn't totally correct as it incorrectly allows:

  PT10H20S

Which is invalid due to no minutes being present. Oh well.
This commit is contained in:
Andrew Ruthven 2017-05-17 11:06:29 +12:00
parent 38788acc5c
commit 22e0e4aa38

View File

@ -72,7 +72,9 @@ class RepeatRuleTimeZone extends DateTimeZone {
/**
* Provide a useful way of dealing with RFC5545 duration strings of the form
* ^-?P(\dW)|((\dD)?(T(\dH)?(\dM)?(\dS)?)?)$
* ^(-?)P(?:(\d+W)|(?:(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S?)?)?))$
* This doesn't fully implement the RFC, as it allows PT10H10S - which should
* be invalid as no minutes are present.
*/
class Rfc5545Duration {
private $epoch_seconds = null;
@ -115,7 +117,7 @@ class Rfc5545Duration {
*/
function asSeconds() {
if ( !isset($this->epoch_seconds) ) {
if ( preg_match('{^(-?)P(\d+W)|(?:(\d+)D?(?:T(\d+)H?(\d+)M?(\d+)S?)?)$}i', $this->as_text, $matches) ) {
if ( preg_match('{^(-?)P(?:(\d+W)|(?:(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S?)?)?))$}i', $this->as_text, $matches) ) {
// @printf("%s - %s - %s - %s - %s - %s\n", $matches[1], $matches[2], $matches[3], $matches[4], $matches[5], $matches[6]);
$this->secs = 0;
if ( !empty($matches[2]) ) {