Losyres
Losyres

Reputation: 92

Firebase Cloud function: Weird timestamp bug

So I have implemented Stories in my Flutter+Firebase app, and wrote a Cloud Function in JS to delete all stories older than 24h. I added a test Story in the Database, with a field 'timestamp' and set it to August 25, 8:00. Now when I am running the function, I print out the document id and the timestamp of that found document. However, the dates that get printed out are all in 1973!?

Here is my function:

// Start delete old Stories
const runtimeOpts = {
  timeoutSeconds: 300,
  memory: '512MB'
}
const MAX_CONCURRENT = 3;
const PromisePool = promisePool.PromisePool;
exports.storiesCleanup = functions.runWith(runtimeOpts).pubsub.schedule('every 1 minutes').onRun( 
  async context => {
    console.log('Cleaning stories...');
    await getOldStories();
    //const promisePool = new PromisePool(() => deleteOldStories(oldStories), MAX_CONCURRENT);
    //await promisePool.start();
    console.log("finished cleaning stories");
  }
);

async function getOldStories() {
    const yesterday = Date.now() - 24*60*60*1000;
    console.log("Yesterday: " + yesterday);
    
    var storyPostsRef = admin.firestore().collection("StoryPosts");
    var query = storyPostsRef.where("timestamp", "<", yesterday);

    storyPostsRef.get().then(
      querySnapshot => {
        querySnapshot.forEach(
          (doc) => {
            // HERE I AM PRINTING OUT!
            console.log("Found document: " + doc.id + ", which was created on: " + doc.data().timestamp);
            //doc.ref.delete();
          }
        )
        return null;
      }
    ).catch(error => {throw error;});
}
//End delete old stories

Here is a picture of a document and its timestamp: enter image description here

And this is a picture of the id and timestamp printed out for that document: enter image description here

Edit: So for printing out, I figured that if I print doc.data().timestamp.seconds I get the correct number of seconds since epoch. But I still don't understand what the number 06373393200.000000 (printed in the picture above) is. And how do I then make a query when I want to get all stories where the timestamp is small than today-24h ?

var query = storyPostsRef.where("timestamp", "<", Timesstamp.fromDate(yesterday)); does not work.

Upvotes: 0

Views: 161

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317342

If you come to the conclusion that the printed timestamp is from a year other than the one shown in the console, then you are misinterpreting the output. Firestore timestamps are represented with two values - an offset in seconds since the unix epoch, and another offset in nanoseconds from that time. This much is evident from the API documentation for Timestamp.

You're probably taking the seconds offset value and interpreting as an offset in milliseconds, which is common in other timestamp systems. You can see how this would cause problems. If you want to take that a Firestore offset in seconds and use a tool to interpret it in a system that uses milliseconds, you will need to first multiply that value by 1,000,000 to convert seconds to milliseconds.

Upvotes: 1

Related Questions