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

incorrect timezone handling for ingredient logs, causes consumed items to show up for yesterday instead of today #576

Open
Dieterbe opened this issue May 8, 2024 · 3 comments

Comments

@Dieterbe
Copy link
Contributor

Dieterbe commented May 8, 2024

Steps to Reproduce

be in a timezone that is not UTC. I am in UTC+2

  1. create a nutritional plan
  2. today, at some time, e.g. 4 PM, log an ingredient to the plan (using the mobile app)
  3. the POST request to https://wger.de/api/v2/nutritiondiary/ uses datetime: <today>T00:00:00.000 , that same api call has a response including datetime: <today>T00:00:00+02:00
  4. in nutrition.dart, fetchAndSetLogs(),
  • the api returns json such as {id: 13499, plan: 92895, meal: null, ingredient: 146034, weight_unit: null, datetime: <today>T00:00:00+02:00, amount: 100.00}
  • the decoded datetime (after Log.fromJson(logData); ) is <yesterday> 22:00:00.000Z

Expected results:

i would expect accurate time to get logged, and my logged items to show up for the correct day

Actual results:

both time and timezone get altered, logged items are classified under the wrong day

@Dieterbe
Copy link
Contributor Author

Dieterbe commented May 8, 2024

i will do some more digging and experimentation. hopefully this just needs some small tweaks in dart, and not in django.
i hope the timezone handling is django is solid.

@Dieterbe
Copy link
Contributor Author

Dieterbe commented May 8, 2024

i just checked how it works on web (wger.de)

  • nutrition entry has a datetime field (on flutter, you only chose a date, not time)
  • POST request has <today>T15:47:01.701Z
  • POST response has <today>T17:47:01.701000+02:00 (different representation, but identical time)
  • nutritiondiary api call returns <today>T17:47:01.701000+02:00

so this is all pretty correct.

with flutter, we encode (to submit to the api) using
_$LogToJson(Log instance) uses instance.datetime.toIso8601String() which says:

The format is yyyy-MM-ddTHH:mm:ss.mmmuuuZ for UTC time, and yyyy-MM-ddTHH:mm:ss.mmmuuu (no trailing "Z") for local/non-UTC time

and we decode what we got from the api in _$LogFromJson using DateTime.parse which supports localtime and zulu time properly.

it seems with flutter, we have these issues:

  • the posted time is nonsensical, and doesn't allow for tweaking the time
  • while the part of encoding/decoding actually seems to work fine.
    However, the flutter app acts as if we are in UTC, effectively ignoring the user's timezone (even when the data is technically correct, just represented differently), thereby attributing events to the wrong days

i will have a look if i can fix most or all of these.

@Dieterbe
Copy link
Contributor Author

Dieterbe commented May 9, 2024

for the first one, i have a fix in #566

for the 2nd one, not sure yet what's the best way to fix it. but at least for EU users the first fix alone solves the problem in practice (unless you log foods shortly after midnight :-)

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

1 participant