【问题标题】:Cloud functions not offsetting date for firestore query云功能不偏移 Firestore 查询的日期
【发布时间】:2021-04-05 09:03:33
【问题描述】:

我有一个每天下午 12 点运行的 cron 作业。它应该抓取当天早上 6 点到中午 12 点之间添加的集合中的所有文档。我正在使用一个名为dateOrderAdded 的字段来运行查询。该字段位于 GMT -4 时区,因此当我运行查询时,我必须满足这一要求。

"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;
}

浏览器似乎适当地将小时数设置为 6 点和 12 点,但在云功能上它仍然是 2 点和 8 点。

由于云函数中的日期是 UTC,因此 2 小时和 8 小时有意义,但我明确将小时数设置为 6 和 12,我不确定为什么我没有看到设置的更改。

【问题讨论】:

  • 使用 setHours 时,您设置的是本地时间。为了与 Cloud 保持一致,您似乎需要使用 setUTCHours 设置 UTC 时间。

标签: javascript date google-cloud-firestore google-cloud-functions


【解决方案1】:

建议您自己创建 unix 时间戳,而不是依赖 Cloud Functions 的 Date

根据 UTC-4 时区,下一行将帮助生成适当的时间:

编辑:对于 Nodejs 10 运行时,格式会有所不同,因此我必须调整日期以避免生成不正确的日期,如果格式因您选择的运行时而异,只需将日期调整为有效

var date = new Date( ).toLocaleString("es-VE", { "timeZone": "America/Caracas" }).split(", ")[0].split("/");
let today = date[2] + "-" +  date[0] + "-" + date[1];

let todaySixAm = new Date(`${today}T06:00:00.000-04:00`);
let todayTwelvePm = new Date(`${today}T12:00:00.000-04:00`);

console.log("Today 6:00 am GMT-4 " + todaySixAm.valueOf()); //1609322400000
console.log("Today 12:00 pm GMT-4:" + todayTwelvePm.valueOf()); //1609344000000

这样您就不必依赖托管代码的服务器的本地时间。

【讨论】:

    【解决方案2】:

    在 cmets 中接受 @RobG 的建议,我让它在 setUTC 小时数下工作。

    "use strict";
    const functions = require("firebase-functions");
    const admin = require("firebase-admin");
    
    
    exports.sendTwelvePmOrderSummary = functions.pubsub
    .schedule("0 12 * * *")
    .timeZone("America/Caracas")
    .onRun(async context => {
    
        //Timezone of stored data is GMT -4 
        //For query to be run correctly use UTC hours
        //6AM = 6+ 4 = 10 UTC hours
    
        const todaySixAm = new Date(new Date().setUTCHours(10,0,0,0));  
    
        //Set time to 11:59:59:999AM - 12 + 4 = 16
        //Set it to just before 12PM so results from other CRONs won't be duplicated
        const todayTwelvePm = new Date(new Date().setUTCHours(15, 59, 59,999));  
    
    
        //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)
            return { message: "No orders placed - TWELVE PM Summary" };
      
        console.log('Orders placed!');
        return null;
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 2023-03-27
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      相关资源
      最近更新 更多