0

So in my android app, I am using the real-time database to store information about my users. That information should be updated every Monday at 00:00 o'clock. I am using a cloud function to do this but the problem here is the time zones. Right now I have set the time zone to 'Europe/Sofia' for testing purposes. In the documentation, it is said that the time zone for cloud functions must be from the TZ database. So I figured I could ask the user before registering in my app their preferred time zone and save it in the database. My question is after getting the user's prefered time zone is there a way to only write one cloud function and execute it dynamically for each time zone in the TZ database or do I have to create individual functions for each time zone in the TZ database?

Chinez
  • 551
  • 2
  • 6
  • 29

2 Answers2

1

If I correctly understand your question, you could have a scheduled Cloud Function which runs every hour from 00:00 to 23:00 UTC+14:00 on Mondays, and, for every execution (i.e. for every hour within this range), query for the users that should be updated and execute the updates.

I'm not able to enter more into details, based on the info you have provided.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • That is a nice a idea! But then what about daylight saving time. Here's what the docs say: "Note that some time zones include a provision for daylight savings time. The rules for daylight saving time are determined by the chosen tz." If I use UTC and run a function every hour how am I going to account for this? – Veselin Zinkov Nov 28 '20 at 14:51
  • I understand that you are going to "ask the user before registering in my app their preferred time zone and save it in the database". You should use this info to decide if, at the specific UTC time the Cloud Function runs it is Monday 00:00 o'clock for the user. For every execution of the Cloud Function (every hour) there might be some users, in your database, for which it is Monday 00:00 o'clock: it's up to you to find them based on what they have saved as preferred time zone . – Renaud Tarnec Nov 28 '20 at 16:11
  • See the update: after thinking about it a bit more I think you should select the UTC+14:00 timezone to set the scheduled Cloud Function, since it is the earliest time zone on Earth. – Renaud Tarnec Nov 28 '20 at 16:38
  • 1
    Yes I thought about that and was just going to say it to you. Thanks for the suggestions. I will think about the problem a bit more and see what I can do. – Veselin Zinkov Nov 28 '20 at 17:02
1

It's not possible to schedule a Cloud Function using a dynamic timezone. You must know the timezone at the time you write the function and declare it statically in your code.

If you want to schedule something dynamically, read through your options in this other question: https://stackoverflow.com/a/42796988/807126

So, you could schedule a repeating function that runs every hour, and check to see if something should be run for a user at the moment in time that it was invoked. Or, you can schedule a single future invocation of a function with a service like Cloud Run, and keep rescheduling it if needed.

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