Skip to content

Commit

Permalink
fix: improve am/pm cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gbassisp committed May 21, 2024
1 parent fa077a3 commit ca6c57e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/src/param_cleanup_rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ final _exprs = [...idealTimePatterns]..removeLast();
final _betterTimeComponent = CleanupRule((params) {
String padLeft(String? original) => (original ?? '').padLeft(2, '0');
String padRight(String? original) => (original ?? '').padRight(3, '0');
String cleanAmpm(String? original) =>
original?.replaceAll(RegExp(r'(\.|-)'), '').toLowerCase() ?? '';

for (final e in _exprs) {
const ampm = r'\s*(?<ampm>(a|p)\.?m\.?\W)?';
const ampm = r'\s*(?<ampm>(a|p)(\.|-)?m(\.|-)?\W)?';
final re = RegExp(e + ampm, caseSensitive: false);
final s = '${params.formattedString} ';
final matches = re.allMatches(s);
Expand All @@ -174,7 +176,7 @@ final _betterTimeComponent = CleanupRule((params) {
'${padLeft(m.tryNamedGroup('second'))}'
'${m.tryNamedGroup('microsecond') != null ? '.'
'${padRight(m.tryNamedGroup('microsecond'))}' : ''} '
'${m.tryNamedGroup('ampm') ?? ''}';
'${cleanAmpm(m.tryNamedGroup('ampm'))}';
final newString = s.replaceAllMapped(re, (_) => '') + newTime;

params
Expand Down
40 changes: 40 additions & 0 deletions test/nonsense_formats_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,45 @@ void main() {

expect(parser.tryParse(formatted), equals(expected));
});
for (final am in [
'am',
'AM',
'a.m.',
'a.m',
'A.M',
' am',
' AM',
' a.m.',
' a.m',
' A.M',
]) {
test('git default nonsense 3 - "$am"', () {
final formatted = 'Thu, May 16 10:18:07$am 2024 -0930';
final expected =
DateTime.utc(2024, 5, 16, 10, 18, 7).copyWithOffset('-0930');

expect(parser.tryParse(formatted), equals(expected));
});
}
for (final pm in [
'pm',
'PM',
'p.m.',
'p.m',
'P.M',
' pm',
' PM',
' p.m.',
' p.m',
' P.M',
]) {
test('git default nonsense 4 - "$pm"', () {
final formatted = 'Thu, May 16 10:18:07$pm 2024 -0930';
final expected =
DateTime.utc(2024, 5, 16, 22, 18, 7).copyWithOffset('-0930');

expect(parser.tryParse(formatted), equals(expected));
});
}
});
}

0 comments on commit ca6c57e

Please sign in to comment.