wger-project / flutter

Flutter fitness/workout app for wger
GNU Affero General Public License v3.0
573 stars 245 forks source link

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

Open Dieterbe opened 6 months ago

Dieterbe commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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

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:

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

Dieterbe commented 6 months ago

for the first one, i have a fix in https://github.com/wger-project/flutter/pull/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 :-)