I have a cron job that runs @12PM everyday. It's supposed to grab all the documents in a collection that were added between 6AM and 12PM on that day. I'm using a field called dateOrderAdded
to run the query on. This field is in the timezone GMT -4 so when I'm running the query I have to cater for that.
"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.sendTwelvePmOrderSummary = functions.pubsub.schedule("0 12 * * *")
.timeZone("America/Caracas")
.onRun(async context => {
//Get current date
const today = new Date();
//Set time to 6AM
const todaySixAm = new Date(today.setHours(6,0,0,0));
//Set time to 12PM
const todayTwelvePm = new Date(today.setHours(12,0,0,0));
console.log(todaySixAm.valueOf()); //browser - 1609149600000 | cloud fn - 1609135200000
console.log(todayTwelvePm.valueOf()); // browser - 1609171200000 | cloud fn - 1609156800000
//Get all orders in-between today at 6AM and today at 12PM
const orderCol = await admin.firestore().collection('orders').where('dateOrderAdded','>=',todaySixAm).where('dateOrderAdded','<=',todayTwelvePm).get();
const orderDocs = orderCol.docs.map(doc=>doc.data())
if(orderCol.size === 0 ){
//No orders placed - TODO: Send no orders placed email
console.log('No orders placed');
}else{
//orders! - TODO: Send order placement summary
console.log('Orders placed');
}
return null;
}
The browser seems to set the hours to 6 and 12 appropriately but on the cloud function it remains at 2 and 8.
Since dates are UTC in cloud functions the hours 2 and 8 make sense but I explicitly set the hours to 6 and 12 and I'm not sure why I'm not seeing the change being set.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…