Home > Enterprise >  How to set the date of messages sent and received for exact time zone date time?
How to set the date of messages sent and received for exact time zone date time?

Time:06-09

I'm trying to create an app of messages but the problem is to order the messages if one of the user don't have the time correctly set ( for example with 1 less minute ). So how can I set a default time zone that I can set the date of the messages according to this time zone date time and not the local ( device ) time? I tried using Timestamp.now(), NTP ( idk if i used it correctly but it was getting local time too) FieldValue.serverTimeStamp(); ( that was returning instance and the date was not working ). How can I solve this problem?

CodePudding user response:

Whenever you save time take time in UTC like the following

DateTime.now().toUtc()

There can be one more issue in this approach. When a user has disabled automatic time setting and had manually adjusted time in the settings then it may result in inconsistent data too. To solve this you may try some api like worldclockapi.org. But there will be a small network delay to fetch time first then to save in your database. It's basically a trade off unless you have a server and save it in db taking time from the servers..

CodePudding user response:

Use dart ntp package to do this.

DateTime sendTimestamp = await NTP.now();
print('Send timestamp: ${sendTimestamp}');

Every time we need to send timestamp from device, just get it from NTP.now().

Then to sync timezone between user app, use timezone packages.

final detroit = tz.getLocation('America/Detroit');
final timeZone = detroit.timeZone(sendTimestamp.millisecondsSinceEpoch);
print(timeZone);
  • Related