Skip to content
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
16 changes: 9 additions & 7 deletions lib/src/types/xsd_date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,19 @@ class XsdDate implements Comparable<XsdDate> {
if (tzStr == null) {
return XsdDate(dt, isFloating: true);
} else {
Duration? offset;
final Duration offset;
if (tzStr == 'Z') {
offset = Duration.zero;
} else {
final tzMatch = _offsetRegex.firstMatch(tzStr);
if (tzMatch != null) {
final sign = tzMatch.group(1) == '+' ? 1 : -1;
final hours = int.parse(tzMatch.group(2)!);
final minutes = int.parse(tzMatch.group(3)!);
offset = Duration(hours: hours, minutes: minutes) * sign;
// _dateRegex ensures tzStr is in [+-]HH:mm format, so a match is guaranteed.
final tzMatch = _offsetRegex.firstMatch(tzStr)!;
final sign = tzMatch.group(1) == '+' ? 1 : -1;
final hours = int.parse(tzMatch.group(2)!);
final minutes = int.parse(tzMatch.group(3)!);
if (hours > 14 || minutes > 59 || (hours == 14 && minutes != 0)) {
throw FormatException('Invalid timezone offset range', input);
}
offset = Duration(hours: hours, minutes: minutes) * sign;
}
return XsdDate(dt, isFloating: false, originalOffset: offset);
}
Expand Down
12 changes: 12 additions & 0 deletions test/types/xsd_date_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ void main() {
() => XsdDate.parse('2002-02-30'),
throwsFormatException,
); // Invalid day
expect(
() => XsdDate.parse('2002-10-10+15:00'), // Hour > 14
throwsFormatException,
);
expect(
() => XsdDate.parse('2002-10-10-14:01'), // Minute > 00 when hour is 14
throwsFormatException,
);
expect(
() => XsdDate.parse('2002-10-10+05:60'), // Minute > 59
throwsFormatException,
);
});

test('equality', () {
Expand Down