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

[Bug]: DateTime & TimeZone, invalid date returned #901

Closed
tanukijs opened this issue Sep 14, 2022 · 7 comments
Closed

[Bug]: DateTime & TimeZone, invalid date returned #901

tanukijs opened this issue Sep 14, 2022 · 7 comments
Assignees

Comments

@tanukijs
Copy link

tanukijs commented Sep 14, 2022

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

  1. Define an entity that contain a DateTime field
  2. Insert an entity with a DateTime
  3. Compare the value of DateTime.now() & the value returned by our newly created entity

Version

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.

final createdAt = DateTime.now(); // 07:30 AM
realm.write(() {
  entity.createdAt = createdAt;
  print('1 ${message.createdAt}');  // 05:30 AM
  print('2 $createdAt'); // 07:30 AM
});

Stacktrace of the exception/crash you're getting

No response

Relevant log output

[+16808 ms] flutter: 1 2022-09-14 05:51:00.831907Z
[   +1 ms] flutter: 2 2022-09-14 07:51:00.831907
@blagoev
Copy link
Contributor

blagoev commented Sep 14, 2022

Hi,
DateTimes are persisted as UTC DateTimes in Realm. You need to convert it back to what timezone you need or the timezone of the device when you read the values.

@desistefanova
Copy link
Contributor

Hi @tanukijs,
thanks for noticing this!
We will check the documentation.

@nielsenko
Copy link
Contributor

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();
}

@desistefanova
Copy link
Contributor

Hi @tanukijs,
We will add a note to our documentation about storing dates in UTC.
This issue will be resolved once the documentation is ready.
Thank you for your contribution!

@tanukijs
Copy link
Author

tanukijs commented Sep 19, 2022

Hello, this is way clearer to me. Thank you for your fast replies !
Edit: I let you manage the closing of the issue

@tanukijs tanukijs reopened this Sep 19, 2022
@desistefanova
Copy link
Contributor

Hi @tanukijs ,
Here is the section of the updated document in relation to storing UTC DateTame to Realm.
https://www.mongodb.com/docs/realm/sdk/flutter/realm-database/data-types/#datetime
Thanks! I'm closing the issue.

@sync-by-unito
Copy link

sync-by-unito bot commented Sep 20, 2022

➤ Desislava Stefanova commented:

Updated documentation.

@sync-by-unito sync-by-unito bot closed this as completed Sep 20, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants