-
Notifications
You must be signed in to change notification settings - Fork 87
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
[Bug]: DateTime & TimeZone, invalid date returned #901
Comments
Hi, |
Hi @tanukijs, |
Here is an example of how you can handle timezone data: import 'package:realm_dart/realm.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
part 'realm_datetime_with_timezone.g.dart';
@RealmModel()
class _Data {
late DateTime dateTimeUtc;
late String locationName; // tz database/Olson name
tz.TZDateTime get dateTime => tz.TZDateTime.from(
dateTimeUtc,
tz.getLocation(locationName),
);
}
Data createData(tz.TZDateTime value) => Data(
DateTime.fromMillisecondsSinceEpoch(value.millisecondsSinceEpoch), // value.toUtc() won't work
value.location.name,
);
void main(List<String> arguments) {
tz.initializeTimeZones();
final paris = tz.getLocation('Europe/Paris');
var year = 1789;
final r = Realm(Configuration.inMemory([Data.schema]));
r.write(() {
while (year < 2023) {
final bastilleDay = tz.TZDateTime(paris, year++, 7, 14);
r.add(createData(bastilleDay));
}
});
for (final data in r.all<Data>()) {
final x = data.dateTime;
print(
'iso6801: ${x.toIso8601String()}, '
'weekday: ${x.weekday}, ' // 1 based, ie. 1 is monday
'location: ${x.location}, '
'timezone: ${x.timeZone}, '
'timezoneOffset: ${x.timeZoneOffset}',
);
}
Realm.shutdown();
} |
Hi @tanukijs, |
Hello, this is way clearer to me. Thank you for your fast replies ! |
Hi @tanukijs , |
➤ Desislava Stefanova commented: Updated documentation. |
What happened?
Since i migrated to Realm, i saw a problem with my dates everywhere. It seems that all the date have been shifted to another TimeZone.
My phone has TimeZone set to Paris, it's 07:30 AM and realm return me 05:30 AM.
I found out that i can convert the date
.toUtc()
before inserting it to realm & convert it back to.toLocal()
but as i didn't see any informations anywhere else than #569 i didn't expect DateTime field to work like that.Repro steps
DateTime
fieldDateTime.now()
& the value returned by our newly created entityVersion
3.3.0
What Realm SDK flavor are you using?
Local Database only
What type of application is this?
Flutter Application
Client OS and version
Realm Dart 0.4.0+beta
Code snippets
Here is a snippet, we only need an entity with a
DateTime
field to reproduce the problem.Stacktrace of the exception/crash you're getting
No response
Relevant log output
The text was updated successfully, but these errors were encountered: