Skip to content

Commit b9e9869

Browse files
authored
Allow to parse TimeZoneOffset with ±[hh] only (#123)
1 parent cedfa74 commit b9e9869

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/Parser/IsoParsers.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public static function monthDay(): PatternParser
206206
}
207207

208208
/**
209-
* Returns a parser for a time-zone offset such as `Z` or `+01:00`.
209+
* Returns a parser for a time-zone offset such as `Z`, `+01`, `+01:00`, `+01:00:00`.
210210
*/
211211
public static function timeZoneOffset(): PatternParser
212212
{
@@ -220,8 +220,10 @@ public static function timeZoneOffset(): PatternParser
220220
->startGroup()
221221
->appendCapturePattern('[\-\+]', TimeZoneOffsetSign::NAME)
222222
->appendCapturePattern(TimeZoneOffsetHour::PATTERN, TimeZoneOffsetHour::NAME)
223+
->startOptional()
223224
->appendLiteral(':')
224225
->appendCapturePattern(TimeZoneOffsetMinute::PATTERN, TimeZoneOffsetMinute::NAME)
226+
->endOptional()
225227
->startOptional()
226228
->appendLiteral(':')
227229
->appendCapturePattern(TimeZoneOffsetSecond::PATTERN, TimeZoneOffsetSecond::NAME)

src/TimeZoneOffset.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function from(DateTimeParseResult $result): TimeZoneOffset
106106
}
107107

108108
$hour = $result->getField(Field\TimeZoneOffsetHour::NAME);
109-
$minute = $result->getField(Field\TimeZoneOffsetMinute::NAME);
109+
$minute = $result->getOptionalField(Field\TimeZoneOffsetMinute::NAME);
110110
$second = $result->getOptionalField(Field\TimeZoneOffsetSecond::NAME);
111111

112112
$hour = (int) $hour;
@@ -128,6 +128,7 @@ public static function from(DateTimeParseResult $result): TimeZoneOffset
128128
* The following ISO 8601 formats are accepted:
129129
*
130130
* * `Z` - for UTC
131+
* * `±hh`
131132
* * `±hh:mm`
132133
* * `±hh:mm:ss`
133134
*

tests/TimeZoneOffsetTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ public function testParse(string $text, int $totalSeconds): void
152152
public static function providerParse(): iterable
153153
{
154154
yield from [
155+
['+00', 0],
156+
['-00', 0],
157+
['+01', 3600],
158+
['-01', -3600],
159+
['+18', 64800],
160+
['-18', -64800],
161+
155162
['+00:00', 0],
156163
['-00:00', 0],
157164
['+01:00', 3600],
@@ -182,7 +189,6 @@ public static function providerParseInvalidStringThrowsException(): array
182189
return [
183190
[''],
184191
['00:00'],
185-
['+00'],
186192
['+00:'],
187193
['+00:00:'],
188194
['+1:00'],

0 commit comments

Comments
 (0)