问题
I am building a flutter/firebase chat app and storing timestamps of messages in UTC. I am using this line of code to convert local time to UTC. 'time': DateTime.now().toUtc().toString(),
When I tested this with two users one is in Virginia, US, and the other one in Sri Lanka. The timestamp of the user in Sri Lanka is converted correctly to UTC, but for user in Virginia time is correct but the date as of yesterday.
Sri Lanka Time: 2020-06-21 20:50:48
UTC converted: 2020-06-21 15:20:48.027446Z
Virginia Time: 2020-06-21 11:24:38
UTC converted: 2020-06-20 15:24:38.594194Z
My whole app malfunctioned because of this and order of messages got weird. How to solve this issue?
回答1:
You don't need to store the timestamp by yourself. Firestore offers timestamp. See https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/FieldValue/serverTimestamp.html
来源:https://stackoverflow.com/questions/62502699/dart-flutter-not-converting-local-date-time-to-utc-correctly