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

OpeningHoursPeriodDate.toDateTime() weekday mapping is wrong #113

Closed
janoschp opened this issue Apr 18, 2021 · 2 comments
Closed

OpeningHoursPeriodDate.toDateTime() weekday mapping is wrong #113

janoschp opened this issue Apr 18, 2021 · 2 comments

Comments

@janoschp
Copy link

janoschp commented Apr 18, 2021

The method dayTimeToDateTime in the src/utils.dart expects the given day parameter to have the following mapping:

0 -> Monday
1 -> Tuesday
...
6 -> Sunday

When it actually has the mapping:
0 -> Sunday
1 -> Monday
...
6 -> Saturday

The second mapping is used in the Places API as documented here: https://developers.google.com/maps/documentation/places/web-service/details#PlaceDetailsResults

So currently when running this code:

PlaceDetails placeDetails = ... /* get it from the api */;
OpeningHoursPeriod openingHoursPeriod = placeDetails.openingHours.periods.first;
DateTime wrongDateTime = openingHoursPeriod.open.toDateTime();

Will return a DateTime object which has it's day shifted one day to the future.

@janoschp
Copy link
Author

If you would change this code:

DateTime dayTimeToDateTime(int day, String time) {
  if (time.length < 4) {
    throw ArgumentError(
        "'time' is not a valid string. It must be four integers.");
  }

  final _now = DateTime.now();
  // Maps is 0-index DO^W
  final _weekday = _now.weekday - 1;
  final _mondayOfThisWeek = _now.day - _weekday;
  final _computedWeekday = _mondayOfThisWeek + day;

  final _hour = int.parse(time.substring(0, 2));
  final _minute = int.parse(time.substring(2));

  return DateTime.utc(_now.year, _now.month, _computedWeekday, _hour, _minute);
}

to this:

DateTime dayTimeToDateTime(int day, String time) {
  if (time.length < 4) {
    throw ArgumentError(
        "'time' is not a valid string. It must be four integers.");
  }

  final _now = DateTime.now();
  // Maps is 0-index DO^W
  final _adjustedDay = day == 0 ? 6 : day - 1;
  final _weekday = _now.weekday - 1;
  final _mondayOfThisWeek = _now.day - _weekday;
  final _computedWeekday = _mondayOfThisWeek + _adjustedDay;

  final _hour = int.parse(time.substring(0, 2));
  final _minute = int.parse(time.substring(2));

  return DateTime.utc(_now.year, _now.month, _computedWeekday, _hour, _minute);
}

everything would work fine.

@lejard-h
Copy link
Owner

lejard-h commented May 1, 2021

Thanks for report

Fix landed on 0.0.20-nullsafety.5

@lejard-h lejard-h closed this as completed May 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants