Dart/Flutter无法将本地日期时间正确转换为UTC [英] Dart/Flutter Not converting local date time to UTC correctly

查看:531
本文介绍了Dart/Flutter无法将本地日期时间正确转换为UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个flutter/firebase聊天应用程序,并将消息的时间戳存储在UTC中.我正在使用此行代码将本地时间转换为UTC.'time':DateTime.now().toUtc().toString(),当我与两个用户进行测试时,一个在美国弗吉尼亚州,另一个在斯里兰卡.斯里兰卡用户的时间戳已正确转换为UTC,但弗吉尼亚州用户的时间戳正确,但日期为昨天.

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.

斯里兰卡时间: 2020-06-21 20:50:48

UTC转换为: 2020-06-21 15:20:48.027446Z

弗吉尼亚州时间: 2020-06-21 11:24:38

UTC转换为: 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?

推荐答案

您不需要自己存储时间戳.Firestore提供时间戳.请参见 https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/FieldValue/serverTimestamp.html

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

这篇关于Dart/Flutter无法将本地日期时间正确转换为UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆