Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Database Rule that compares timestamp to 00:00:00 of today?

I'm trying to create a Firebase Database Rule that forbids users to write to any node which has a timestamp created before 00:00:00 of today.

Example of database:

{
    "messageID": { 
        "message": "blabla",
        "timestamp": 1481744636
    }
}

Rules:

{
    "rules": {
        "$messageID": {
            ".write": "data.child('timestamp').val() > ???"
        }
    }
}

I know about the 'now' keyword, but I don't think it helps me. I need a way to compare the timestamp with a value for 00:00:00 of today.

I could create and store a table with all the unix epoch times for midnight of each day in the futurem but isn't there a way to calculate it on the fly?

like image 425
Dan Flict Avatar asked Feb 05 '23 12:02

Dan Flict


1 Answers

This should do it:

{
    "rules": {
        "$messageID": {
            ".write": "data.child('timestamp').val() > (now - (now % 86400000))"
        }
    }
}

86,400,000 is the number of milliseconds in 24 hours, so now % 86400000 should give you the number of milliseconds since 00:00:00 UTC.

console.log(new Date(Date.now() - (Date.now() % 86400000)))
like image 89
cartant Avatar answered Feb 08 '23 15:02

cartant