问题描述
我正在开发一个应用程序,它根据日期和时间将数据放入和获取到 Firebase,我现在的问题是用户可以在设置中修改它,并获取未来的数据或我尝试使用 dateTime 的旧数据.Now 但是可以修改我也试过 datetime.utcNow 但它也可以修改。所以我的问题是我可以解决这个问题,这样用户就不能修改时间,或者即使他们这样做,应用时间也不会受到影响。?
存储到 Firebase 的代码示例。
await CrossCloudFirestore.Current
.Instance
.Collection(DataPaths.NewsFeed)
.Document(guid)
.SetAsync(new {
ID = ticks + id,ProfileImage = prefrences.profileImage,UserName = usernames,Title = title,Date = DateTime.Now.ToString("dd MMM yyyy"),Time = DateTime.UtcNow.ToString("HH:mm:ss"),Details = details,OrderTime = orderTime
});
我正在尝试的另一个示例。
//All 3 Needs sorting/fixing utc vs local/GMT time
//DateTime date = DateTime.UtcNow;
//var act = TimeZoneInfo.ConvertTime(date,TimeZoneInfo.Local);
//Application.Current.MainPage.displayAlert("",act.ToString(),"OK");
解决方法
解决方案是,您应该从 API 而不是从设备获取当前时间。
这样做的方法是将 Date
& Time
设置为从 API 获取的值。
所以我个人喜欢这个http://worldclockapi.com/api/json/est/now
;将此复制到您想要设置日期和时间的代码中,您应该一切正常。
您可以编辑 API URL 以匹配您的时区;假设您想要中央标准时间,那么这就是您的 API URL http://worldclockapi.com/api/json/cst/now
。
您可以随意使用 Google 搜索其他 API。
现在,如何将变量设置为 API 结果值? ~ 像这样:
var url = "http://worldclockapi.com/api/json/cst/now";
var client = new HttpClient();
var currentDateTime = client.Get(url);
await CrossCloudFirestore.Current
.Instance
.Collection(DataPaths.NewsFeed)
.Document(guid)
.SetAsync(new {
ID = ticks + id,ProfileImage = prefrences.profileImage,UserName = usernames,Title = title,Date = currentDateTime.ToString("dd MMM yyyy"),Time = currentDateTime.ToString("HH:mm:ss"),Details = details,OrderTime = orderTime
});