Skip to content

Commit

Permalink
feat: added long month rule that can work for other languages
Browse files Browse the repository at this point in the history
  • Loading branch information
gbassisp committed Nov 8, 2023
1 parent 6572343 commit bd504d9
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 8 deletions.
111 changes: 108 additions & 3 deletions lib/src/any_date_rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,25 @@ final DateParsingRule ambiguousCase = SimpleRule((params) {
final DateParsingRule ymd = MultipleRules([
maybeDateTimeParse,
ymdTextMonthRegex,
ymdNonLatinTextMonth,
ymdRegex,
]);

final DateParsingRule ydm = MultipleRules([
ydmTextMonthRegex,
ydmNonLatinTextMonth,
ydmRegex,
]);

final DateParsingRule dmy = MultipleRules([
dmyTextMonthRegex,
dmyNonLatinTextMonth,
dmyRegex,
]);

final DateParsingRule mdy = MultipleRules([
mdyTextMonthRegex,
mdyNonLatinTextMonth,
mdyRegex,
]);

Expand All @@ -268,6 +272,32 @@ DateParsingRule maybeDateTimeParse = SimpleRule((params) {
return null;
});

DateParsingRule ymdNonLatinTextMonth = SimpleRule((params) {
final seps = params.parserInfo.allowedSeparators;
// ignore: prefer_interpolation_to_compose_strings
final textMonthPattern = '(?<month>' r'[^\d' + seps.join() + ']+)';

final base = '^'
'$_yearPattern'
'$_s'
'$textMonthPattern'
'$_s'
'$_dayPattern';
DateTime? res;
for (final timePattern in _timePatterns) {
final re = RegExp(base + _s + timePattern);
res = _tryTextMonth(re, params.formattedString, params.parserInfo.months);
if (res != null) {
return res;
}
}
return _tryTextMonth(
RegExp(base),
params.formattedString,
params.parserInfo.months,
);
});

DateParsingRule ymdTextMonthRegex = SimpleRule((params) {
final base = '^'
'$_yearPattern'
Expand Down Expand Up @@ -332,8 +362,33 @@ final DateParsingRule ydmRegex = SimpleRule((params) {
);
});

DateParsingRule ydmNonLatinTextMonth = SimpleRule((params) {
final seps = params.parserInfo.allowedSeparators;
// ignore: prefer_interpolation_to_compose_strings
final textMonthPattern = '(?<month>' r'[^\d' + seps.join() + ']+)';

final base = '^'
'$_yearPattern'
'$_s'
'$_dayPattern'
'$_s'
'$textMonthPattern';
DateTime? res;
for (final timePattern in _timePatterns) {
final re = RegExp(base + _s + timePattern);
res = _tryTextMonth(re, params.formattedString, params.parserInfo.months);
if (res != null) {
return res;
}
}
return _tryTextMonth(
RegExp(base),
params.formattedString,
params.parserInfo.months,
);
});

DateParsingRule ydmTextMonthRegex = SimpleRule((params) {
// final s = separatorPattern(params.parserInfo.allowedSeparators);
final base = '^'
'$_yearPattern'
'$_s'
Expand Down Expand Up @@ -376,8 +431,33 @@ final DateParsingRule mdyRegex = SimpleRule((params) {
);
});

DateParsingRule mdyNonLatinTextMonth = SimpleRule((params) {
final seps = params.parserInfo.allowedSeparators;
// ignore: prefer_interpolation_to_compose_strings
final textMonthPattern = '(?<month>' r'[^\d' + seps.join() + ']+)';

final base = '^'
'$textMonthPattern'
'$_s'
'$_dayPattern'
'$_s'
'$_yearPattern';
DateTime? res;
for (final timePattern in _timePatterns) {
final re = RegExp(base + _s + timePattern);
res = _tryTextMonth(re, params.formattedString, params.parserInfo.months);
if (res != null) {
return res;
}
}
return _tryTextMonth(
RegExp(base),
params.formattedString,
params.parserInfo.months,
);
});

DateParsingRule mdyTextMonthRegex = SimpleRule((params) {
// final s = separatorPattern(params.parserInfo.allowedSeparators);
final base = '^'
'$_textMonthPattern'
'$_s'
Expand Down Expand Up @@ -420,8 +500,33 @@ final DateParsingRule dmyRegex = SimpleRule((params) {
);
});

DateParsingRule dmyNonLatinTextMonth = SimpleRule((params) {
final seps = params.parserInfo.allowedSeparators;
// ignore: prefer_interpolation_to_compose_strings
final textMonthPattern = '(?<month>' r'[^\d' + seps.join() + ']+)';

final base = '^'
'$_dayPattern'
'$_s'
'$textMonthPattern'
'$_s'
'$_yearPattern';
DateTime? res;
for (final timePattern in _timePatterns) {
final re = RegExp(base + _s + timePattern);
res = _tryTextMonth(re, params.formattedString, params.parserInfo.months);
if (res != null) {
return res;
}
}
return _tryTextMonth(
RegExp(base),
params.formattedString,
params.parserInfo.months,
);
});

DateParsingRule dmyTextMonthRegex = SimpleRule((params) {
// final s = separatorPattern(params.parserInfo.allowedSeparators);
final base = '^'
'$_dayPattern'
'$_s'
Expand Down
27 changes: 22 additions & 5 deletions test/locale_based_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ import 'test_values.dart' as values;
final _locales = availableLocalesForDateFormatting;

Iterable<DateFormat> formats(String locale) sync* {
// unsuported for lack of information (no year)
// yield DateFormat.y(locale);
// yield DateFormat.yM(locale);
// yield DateFormat.yMMMM(locale);
// yield DateFormat.yMMM(locale);

// in progress
// yield DateFormat.yMEd(locale);
// yield DateFormat.yMMMEd(locale);
// yield DateFormat.yMMMMEEEEd(locale);

// basic
// yield DateFormat.yMMMMd(locale);
// yield DateFormat.yMMMd(locale);

yield DateFormat.yMd(locale);
}

Expand Down Expand Up @@ -117,5 +113,26 @@ Future<void> main() async {
});
}
}

test('non-latin character month is identified', () {
final l = Locale.parse('am');
final info = l.parserInfo;
final longMonths = info.months;

bool containsMonth() {
for (final m in longMonths) {
if (m.name == 'ጁላይ') {
return true;
}
}
return false;
}

expect(
containsMonth(),
isTrue,
reason: 'expected "am" locale to include "ጁላይ" on month list',
);
});
});
}

0 comments on commit bd504d9

Please sign in to comment.