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

Date parsing #19

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Date format: DD/MM/YYYY

## [next]

- Use `DateTime.tryParse` to parse the date ([#18](https://github.com/bdlukaa/books_finder/issues/18))

## 4.3.0 - [01/05/2022]

- **MINOR BREAKING** Renamed `BookInfo.industryIdentifier` to `BookInfo.industryIdentifiers`
Expand Down
64 changes: 35 additions & 29 deletions lib/src/scripts/books.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,35 +213,7 @@ class BookInfo {
Map<String, dynamic> json, {
bool reschemeImageLinks = false,
}) {
final publishedDateArray =
((json['publishedDate'] as String?) ?? '0000-00-00').split('-');

// initialize datetime variable
DateTime? publishedDate;
if (publishedDateArray.isNotEmpty) {
// initialize date
int year = int.parse(publishedDateArray[0]);
int month = 1;
int day = 1;

// now test the date string
if (publishedDateArray.length == 1) {
// assume we have only the year
year = int.parse(publishedDateArray[0]);
}
if (publishedDateArray.length == 2) {
// assume we have the year and maybe the month (this could be just a speculative case)
year = int.parse(publishedDateArray[0]);
month = int.parse(publishedDateArray[1]);
}
if (publishedDateArray.length == 3) {
// assume we have year-month-day
year = int.parse(publishedDateArray[0]);
month = int.parse(publishedDateArray[1]);
day = int.parse(publishedDateArray[2]);
}
publishedDate = DateTime(year, month, day);
}
DateTime? publishedDate = _getDateTime(json['publishedDate']);

final imageLinks = <String, Uri>{};
(json['imageLinks'] as Map<String, dynamic>?)?.forEach((key, value) {
Expand Down Expand Up @@ -357,4 +329,38 @@ class BookInfo {
infoLink.hashCode ^
canonicalVolumeLink.hashCode;
}

static DateTime _getDateTime(String? encoded) {
encoded ??= '0000-00-00';
DateTime? publishedDate = DateTime.tryParse(encoded);

if (publishedDate != null) return publishedDate;

final publishedDateArray = encoded.split('-');
if (publishedDateArray.isNotEmpty) {
// initialize date
int year = int.parse(publishedDateArray[0]);
int month = 1;
int day = 1;
// now test the date string
if (publishedDateArray.length == 1) {
// assume we have only the year
year = int.parse(publishedDateArray[0]);
}
if (publishedDateArray.length == 2) {
// assume we have the year and maybe the month (this could be just a speculative case)
year = int.parse(publishedDateArray[0]);
month = int.parse(publishedDateArray[1]);
}
if (publishedDateArray.length == 3) {
// assume we have year-month-day
year = int.parse(publishedDateArray[0]);
month = int.parse(publishedDateArray[1]);
day = int.parse(publishedDateArray[2]);
}
publishedDate = DateTime(year, month, day);
}

return publishedDate ?? DateTime(0);
}
}
3 changes: 2 additions & 1 deletion test/book_finder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ void main() {
/// .inauthor
books = await queryBooks(
'J. K. Rowling',
langRestrict: 'en',
queryType: QueryType.inauthor,
maxResults: 1,
printType: PrintType.books,
Expand All @@ -141,7 +142,7 @@ void main() {
/// .inpublisher
books = await queryBooks(
'Scholastic inc',
queryType: QueryType.inauthor,
queryType: QueryType.inpublisher,
maxResults: 1,
printType: PrintType.books,
orderBy: OrderBy.relevance,
Expand Down