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

JIFFY[257] - Fix: Use NumberFormat for locale-aware numeral parsing #299

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
8 changes: 6 additions & 2 deletions lib/src/getter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class Getter {
int month(DateTime dateTime) => dateTime.month;

int quarterOfYear(DateTime dateTime, Locale locale) {
return int.parse(DateFormat('Q', locale.code).format(dateTime));
return _toInt(DateFormat('Q', locale.code).format(dateTime), locale);
}

int dayOfYear(DateTime dateTime, Locale locale) {
return int.parse(DateFormat('D', locale.code).format(dateTime));
return _toInt(DateFormat('D', locale.code).format(dateTime), locale);
}

int year(DateTime dateTime) => dateTime.year;
Expand All @@ -74,6 +74,10 @@ class Getter {
return result;
}

int _toInt(String localeNumber, Locale locale) {
return NumberFormat.decimalPattern(locale.code).parse(localeNumber).toInt();
}

static final List<int> daysInMonthArray = List.from(
[0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
growable: false);
Expand Down
28 changes: 28 additions & 0 deletions test/src/getter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ void main() {
});
}

test('Should successfully get day of year for non-standard number symbols',
() async {
// Setup
await Jiffy.setLocale('bn');
final newLocale = Jiffy.now().locale;

// Execute
final actualDayOfYear =
underTest.dayOfYear(DateTime(1997, 9, 23), newLocale);

// Verify
expect(actualDayOfYear, 266);
});

for (var testData in weekOfYearTestData()) {
test('Should successfully get week of year', () {
// Setup
Expand All @@ -190,6 +204,20 @@ void main() {
expect(actualQuarterOfYear, testData['expectedQuarterOfYear']);
});
}

test('Should successfully get quarter for non-standard number symbols',
() async {
// Setup
await Jiffy.setLocale('bn');
final newLocale = Jiffy.now().locale;

// Execute
final actualDayOfYear =
underTest.quarterOfYear(DateTime(2022, 4, 1), newLocale);

// Verify
expect(actualDayOfYear, 2);
});
});

for (var testData in daysInMonthTestData()) {
Expand Down