Skip to content

Commit 70fba34

Browse files
committed
Better handling of time zones
We now send the current time zone to the server when serializing datetime objects. This was causing entries to be saved some hours wrong or depending on the time, on a different day.
1 parent 0fd2154 commit 70fba34

29 files changed

+169
-105
lines changed

lib/helpers/date.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of wger Workout Manager <https://github.com/wger-project>.
3+
* Copyright (C) wger Team
4+
*
5+
* wger Workout Manager is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* wger Workout Manager is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
/// Returns a timezone aware DateTime object from a date and time string.
20+
DateTime getDateTimeFromDateAndTime(String date, String time) {
21+
return DateTime.parse('$date $time');
22+
}
23+
24+
/// Returns a list of [DateTime] objects from [first] to [last], inclusive.
25+
List<DateTime> daysInRange(DateTime first, DateTime last) {
26+
final dayCount = last.difference(first).inDays + 1;
27+
return List.generate(
28+
dayCount,
29+
(index) => DateTime.utc(first.year, first.month, first.day + index),
30+
);
31+
}
32+
33+
extension DateTimeExtension on DateTime {
34+
bool isSameDayAs(DateTime other) {
35+
final thisDay = DateTime(year, month, day);
36+
final otherDay = DateTime(other.year, other.month, other.day);
37+
38+
return thisDay.isAtSameMomentAs(otherDay);
39+
}
40+
}

lib/helpers/json.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ String? dateToYYYYMMDD(DateTime? dateTime) {
5353
return DateFormat('yyyy-MM-dd').format(dateTime);
5454
}
5555

56+
/// Convert a date to UTC and then to an ISO8601 string.
57+
///
58+
/// This makes sure that the serialized data has correct timezone information.
59+
/// Otherwise the django backend will possible treat the date as local time,
60+
/// which will not be correct in most cases.
61+
String dateToUtcIso8601(DateTime dateTime) {
62+
return dateTime.toUtc().toIso8601String();
63+
}
64+
5665
/*
5766
* Converts a time to a date object.
5867
* Needed e.g. when the wger api only sends a time but no date information.

lib/helpers/misc.dart

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -63,38 +63,6 @@ String repText(
6363
return out.join(' ');
6464
}
6565

66-
/// Returns a list of [DateTime] objects from [first] to [last], inclusive.
67-
List<DateTime> daysInRange(DateTime first, DateTime last) {
68-
final dayCount = last.difference(first).inDays + 1;
69-
return List.generate(
70-
dayCount,
71-
(index) => DateTime.utc(first.year, first.month, first.day + index),
72-
);
73-
}
74-
75-
extension TimeOfDayExtension on TimeOfDay {
76-
bool isAfter(TimeOfDay other) {
77-
return toMinutes() > other.toMinutes();
78-
}
79-
80-
bool isBefore(TimeOfDay other) {
81-
return toMinutes() < other.toMinutes();
82-
}
83-
84-
int toMinutes() {
85-
return (hour * 60) + minute;
86-
}
87-
}
88-
89-
extension DateTimeExtension on DateTime {
90-
bool isSameDayAs(DateTime other) {
91-
final thisDay = DateTime(year, month, day);
92-
final otherDay = DateTime(other.year, other.month, other.day);
93-
94-
return thisDay.isAtSameMomentAs(otherDay);
95-
}
96-
}
97-
9866
void launchURL(String url, BuildContext context) async {
9967
final scaffoldMessenger = ScaffoldMessenger.of(context);
10068
final launched = await launchUrl(Uri.parse(url));

lib/models/nutrition/log.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Log {
3636
@JsonKey(required: true, name: 'plan')
3737
int planId;
3838

39-
@JsonKey(required: true)
39+
@JsonKey(required: true, toJson: dateToUtcIso8601)
4040
late DateTime datetime;
4141

4242
String? comment;

lib/models/nutrition/log.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/nutrition/meal.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import 'package:flutter/material.dart';
2020
import 'package:json_annotation/json_annotation.dart';
2121
import 'package:wger/helpers/consts.dart';
22+
import 'package:wger/helpers/date.dart';
2223
import 'package:wger/helpers/json.dart';
23-
import 'package:wger/helpers/misc.dart';
2424
import 'package:wger/models/nutrition/log.dart';
2525
import 'package:wger/models/nutrition/meal_item.dart';
2626
import 'package:wger/models/nutrition/nutritional_values.dart';

lib/models/nutrition/nutritional_plan.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class NutritionalPlan {
3838
@JsonKey(required: true)
3939
late String description;
4040

41-
@JsonKey(required: true, name: 'creation_date', toJson: dateToYYYYMMDD)
41+
@JsonKey(required: true, name: 'creation_date', toJson: dateToUtcIso8601)
4242
late DateTime creationDate;
4343

4444
@JsonKey(required: true, name: 'only_logging')

lib/models/nutrition/nutritional_plan.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/workouts/log.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Log {
8080
@JsonKey(includeFromJson: false, includeToJson: false)
8181
late WeightUnit? weightUnitObj;
8282

83-
@JsonKey(required: true, toJson: dateToYYYYMMDD)
83+
@JsonKey(required: true, toJson: dateToUtcIso8601)
8484
late DateTime date;
8585

8686
Log({

lib/models/workouts/log.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)