Reputation: 95
In the Firestore project, I have documents in a collection containing data for shops, having fields like shopName
, shopAddress
, startTime
(eg. 10 AM) and closeTime
(eg. 10 PM) . (all strings for now)
When the user is browsing the app, i have retrieved the data from Firestore of the shops displayed in the app, now i wanna show that the shop is closed when the device's time is not between the startTime
and closeTime
of the shop. How do i achieve this?
So far I can detect the device's current time using dart package intl
using this code:
print("${DateFormat('j').format(DateTime.now())}");
It gives output as follows:
I/flutter (14877): 6 PM
This is in DateFormat
, and the data types stored in Firestore are strings.. I dont know how to compare them.. Do let me know if i have to change the data types in Firestore too.
Thank You
Upvotes: 1
Views: 718
Reputation: 823
Maybe you can create a hash map like this:
hashMap=['12 AM', '1 AM', '2 AM', ... , '11 PM', '12 AM'];
After that you can get the positions of startTime
, closeTime
and actualTime
, and see if the actualTime
is between start and close times positions.
Let me know if you want to give you a code example.
Upvotes: 2
Reputation: 265
I think if you use 24 Hour Time Format
and convert startTime
, closeTime
and actualTime
to int
or double
( if the shop close at 20:30/8:30pm), then you can easily compare them with if
. On your firebase server string format is perfect.
For example you make a map and iterate it, and check if the actualTime
is higher than startTime
and lower than closeTime
.
I have never tried this code, but i think it is going to work.
Map map = {'1am': 1, '2am': 2, '3am': 3, ... , '11pm': 23};
map.entries.forEach((e) {
if(e.key == actualTime) {
if(e.value >= startTime && e.value < closeTime) {
print('Open');
}
else{
print('Closed');
}
}
});
By the way, I think you should use UTC
, because if you change the time-zone on your device, your app is going to show that the shop is closed, but in fact the shop is open, just you are in a different time-zone. You can easily implement this with this code.
var now = DateTime.now().toUtc();
Upvotes: 2