I have a cloud function that is scheduled to run at 8pm PDT every day to check if a certain condition is met for each user in my firestore database. If the condition is met, the function will add a document to a collection called ff_push_notifications. My problem is with the scheduling time.
Here is the function I wrote in C# to convert a DateTime object to UTC-7 for the Firestore timestamp property. It should create a timestamp for the current day, set to the provided hour/minute/second arguments.
public static Timestamp GetTimestamp(int hour, int minute, int second)
{
// Step 1: Get the current date and time
DateTime currentDateTime = DateTime.Now;
// Step 2: Set the time to hour/minute/second
DateTime targetTimeUtcMinus7 = new DateTime(currentDateTime.Year, currentDateTime.Month, currentDateTime.Day, hour, minute, second);
// Step 3: Convert the time to UTC
TimeZoneInfo timeZoneUtcMinus7 = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
DateTime targetTimeUtc = TimeZoneInfo.ConvertTimeToUtc(targetTimeUtcMinus7, timeZoneUtcMinus7);
// Step 4: Create a Firestore Timestamp object
Timestamp firestoreTimestamp = Timestamp.FromDateTime(targetTimeUtc);
return firestoreTimestamp;
}
When I execute this function with the arguments (20, 0, 0) locally, I get the current day at 8pm UTC-7. When this function executes server side at its scheduled time (7:00pm), the timestamp set in the document is tomorrow at 8:00pm.
So as you can see in this screenshot, the timestamp for the cloud function execution is May 27 at 7:00pm, but the scheduled time is being set to May 28th at 8:00pm instead of May 27th.
Now, since this GetTimestamp()
method runs just fine locally, I suspect the issue is with the server side time being different than my local machine. How might I overcome this issue?