How to controll time indepedently from time on users phone in my firebase app? - flutter

I have a Flutter app where users can predict the outcome of a soccer match. The problem is that if the user changes the time on their phone they can still predict even after it has ended. How do I prevent this so that it's independent of the time on each user's phone?
Thank you

the solution is to not get the date from the local device, but get it from the firebase timestamp that it offers, as example if we say that you want to do it from now until tomorrow at the same time, you can save it to your DB, using:
Timestamp(Timestamp.now().millisecondsSinceEpoch , 24*60*60*1000)
in your case, you need to compare the Timestamp we set in the database with:
FieldValue.serverTimestamp()
also, you can get a DateTime from a Timestamp with this:
final firestoreTimestamp = /* the Timestamp object */;
firestoreTimestamp.toDate();

The safest and most recommended approach for this is to use database rules.
Patiently study this https://fireship.io/snippets/firestore-rules-recipes

Related

Timestamp.now() auto date time for messenger

I'm trying to create an app that allows users to chat with each other and to order the messages i'm using Timestamp.now() ( that works fine ) but the problem is that if the user dont have auto date time turned on on the phone and the hour is different ( like 1 minute ) the messages order is totally messy. I've tried to use NTP.now() but it not worked, even if i set an offset and add to datetime. Any sugestions or solution for this problem? In this case, i used offset using time.google.com as server ( maybe that's my error?) because after the first message the date time is the same for all messages from user
Use any api service like
worldtimeapi.org
And get a unified time based on one server.

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

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?
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..
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);

How to handle date and time in application when it is used from different geographical locations?

Lets suppose, if a user from India enters some data into the database with its own local timestamp at 5:30 a.m and at the same time a user from U.S.A want to see the data, at that time he will feel very weird because that time which is entered in the database has not arrived yet. How to handle this problem ?
Store all dates and times as UTC date and time values and convert them to the local date and time whenever they are displayed.

How to change the system date and time into correct time zone

I have two fields,
CreatedDate
UpdatedDate
It takes from the system date and time.. My sql server is located at UK. So when i see in site it is showing wrong time.. how to change this in mvc?
My details page, i am showing like following,
<strong>Date Posted:</strong>
<%:Model.CreatedDate%>
Please give me the ideas?
I would store your dates using UTC and then use that as a baseline for converting to the appropriate timezone for your region. This makes it flexible since you have a constant reference point.

MongoDB: how to generate local date?

I generate a date in MongoDB shell:
var d = new Date();
d
but the date result doesn't match the time in my location
However, the same code in javascript, the console.log(d) can output the correct time in my location
Why? How can I generate my local time in MongoDB?
This will give you the timezone (which you should store separately inside your application).
var myDate = new Date();
document.write(myDate.getTimezoneOffset());
MongoDB (including the console) will by default always generate and stores in UTC, however ISODates do support a timezone offset ( http://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC ) however you would need to manage the creation of that offset from your application.
As #CRUSADER mentions it is normally better to store the users offset within the row or even not at all, particularly if your user could be accessing from many locations with many different timezones. In this case it might actually be better to modify the dates within your client JavaScript to take care of the difference in timezone from where they are currently accessing the page.