3

I'm pretty much new to Firebase Cloud Functions, so I don't know whether the question I'm going to ask is valid/not.

So basically I'm working on an android fantasy game (which uses firebase database) which has several contests to be started and ended based on the contest's date and time validity. So I want to get the current date and time so that I can calculate the time remaining to participate in the contest and save that in the database (using CLoud functions)

So to get the current time, should I use APIs like 'https://timezonedb.com/api' or is there anything else I can do to get the time.

Also, how can I calculate the remaining time given the deadline and the current time (in TypeScript language)?

I went through a lot of videos and docs, but couldn't figure out any approach. A short explanation and some useful links would be of great help.

Vivek Thakkar
  • 131
  • 3
  • 11
  • You haven't really said anything about the code you've written, what kind of function you want to trigger, and how the whole thing is supposed to work, so my answer below assumes that you simply want to make a call at some point to know the current point in time. – Doug Stevenson Sep 12 '18 at 07:02

2 Answers2

10

With code running in Cloud Functions, you can trust that the server's sense of time is accurate. For JavaScript running in a node environment, simply run Date.now() to return a timestamp in milliseconds since unix epoch. There is nothing more to do.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
2

You can get the current time from the Firebase by using ServerValue.TIMESTAMP without the need to use Cloud Functions since you have already use Realtime db.

Example code snippet:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
String key = ref.push().getKey(); // this will create a new unique key
Map<String, Object> value = new HashMap<>();
value.put("name", "shesh");
value.put("address", "lucknow");
value.put("timestamp", ServerValue.TIMESTAMP);
ref.child(key).setValue(value);
Angus
  • 3,680
  • 1
  • 12
  • 27
  • This is only relevant if the client is invoking a Cloud Function by writing to the database, which was not explicitly stated in the question. – Doug Stevenson Sep 12 '18 at 07:01
  • Angus Tay Hey I dont want the code to run on the app. Instead I want to get the current time using Firebase Cloud Function and store the time in Firebase Database. Can you please check the updated version of my question? I'm facing some issues – Vivek Thakkar Sep 12 '18 at 11:02
  • It is not practical for you to calculate and return all the value from Cloud Function where it will incur especially if you got thousands and thousands of users keep on querying the time remaining. The best practice is to use the client app to calculate it. – Angus Sep 12 '18 at 13:09
  • @DougStevenson yes, I know, but I just suggesting an alternative of using client side as Cloud Function has plenty of limitation and incur cost. From the doc: A function times out after 1 minute by default, but you can extend this period up to 9 minutes. So the function cannot be used in this application in my opinion. – Angus Sep 12 '18 at 13:11
  • @AngusTay Yeah.. I think using a cloud function would not serve better in my case. I'll try to include it in client app.. Thanks – Vivek Thakkar Sep 13 '18 at 09:08