Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/prep time cook time not correctly imported #1871

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- Add Android client to README
[#1767](https://github.com/nextcloud/cookbook/pull/1767) @lneugebauer
- Show info for empty cookbook or categories in recipe overview
[#1866](https://github.com/nextcloud/cookbook/pull/1767) @seyfeb
[#1866](https://github.com/nextcloud/cookbook/pull/1866) @seyfeb

### Fixed
- Fix translation string to not contain quotes
Expand All @@ -29,6 +29,8 @@
[#1834](https://github.com/nextcloud/cookbook/pull/1834) @christianlupus
- Hode the button to copy ingredients unless there are some ingredients to copy
[#1844](https://github.com/nextcloud/cookbook/pull/1844) @christianlupus
- Allow parsing more ISO 8601 duration strings. See issue [#1749](https://github.com/nextcloud/cookbook/issues/1749)
[#XX](https://github.com/nextcloud/cookbook/pull/XX) @seyfeb

### Maintenance
- Fix URL of Transifex after upstream subdomain change
Expand Down
24 changes: 20 additions & 4 deletions lib/Helper/ISO8601DurationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,30 @@ public function parseDuration(string $duration): string {
throw new InvalidDurationException($this->l->t('Could not parse duration {duration}', ['duration' => $duration]));
}

/**
* Parses the string $duration and checks if it has a valid ISO 8601 duration format. Otherwise throws
* InvalidDurationException.
*
* For reference of ISO 8601, see <a href="https://en.wikipedia.org/wiki/ISO_8601">Wikipedia</a>.
* @param string $duration
* @return string
* @throws InvalidDurationException if $duration does not comply to ISO 8601.
*/
private function parseIsoFormat(string $duration): string {
$pattern = '/^PT(\d+)H(?:(\d+)M(?:(\d+)S)?)?$/';
// P (for period) denotes the duration and must be at the start.
// The single parts are optional, but the lookahead (?=\d) ensures that there is some time (digit) given.
// Years, months, and days are improbable for recipes but so what.. ;) That being said: It's not supported.
$pattern = '/^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?$/';
$ret = preg_match($pattern, trim($duration), $matches);

if ($ret === 1) {
$hours = (int)$matches[1];
$minutes = (int) ($matches[2] ?? 0);
$seconds = (int) ($matches[3] ?? 0);
// $matches[0] is the complete string (like P1Y2M3DT4H5M6S)
// $matches[1] to $matches[3] is years to days (like 1Y 2M 3D)
// $matches[5] is the tome part of the string (like T4H5M6S)
// $matches[6] to $matches[8] is hours to seconds (like 4H 5M 6S)
$hours = (int)$matches[6];
$minutes = (int) ($matches[7] ?? 0);
$seconds = (int) ($matches[8] ?? 0);

while ($seconds >= 60) {
$seconds -= 60;
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/Helper/ISO8601DurationHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public function dpIso() {
['PT3H', 'PT3H0M0S'],
['PT13H0M', 'PT13H0M0S'],
['PT0H60M61S', 'PT1H1M1S'],
['PT10M', 'PT0H10M0S'],
['PT1M', 'PT0H1M0S'],
['PT10S', 'PT0H0M10S'],
['PT1S', 'PT0H0M1S'],
['PT20M1S', 'PT0H20M1S'],
];
}

Expand Down