1

I have written a TimeValidityCheckUtil which determines if the snapshot from a particular listener is within a given time-frame of the current time of the device. This has this method, checkIfTImeValid

  public static boolean checkIfTimeValid(Timestamp dbTimestamp)
    {    
        Timestamp currentTimestamp = Timestamp.now();    
        Long seconds = currentTimestamp.getSeconds() - dbTimestamp.getSeconds();  
        if(seconds> 10)
        {
            return false;
        }    
        return true;    
    }

The database structure is in Firestore and is as follows:

"ABC"-|
      |
      |-"documentId"-|
                     |
                     |-"some_key" - "string"
                     |-"timestamp" - timestamp

This is what happens, device A creates a documentId and the object with the timestamp.

Device B listens to this documentId and invokes the checkIfTimeValid to check if the the operation by document A was within 10s of the current Timestamp (to check if it's recent)

Even if this process is happening instantly, the device shows the difference between the timestamps as ~57-62s which according to me should not be more than 1-5s.

Why this is happening?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Maverick7
  • 1,087
  • 12
  • 22

1 Answers1

1

Timestamp.now() is calculated using the local device's time. If the device clock is out of sync with Firebase, that difference will be reflected here.

I am not aware of the Firestore equivalent, but in the RTDB, you can use special location /.info/serverTimeOffset to estimate the difference between clocks in milliseconds.

firebase.database().ref('/.info/serverTimeOffset').once('value')
  .then((snap) => {
    var offset = snap.val();
    console.log('Server time delta in ms: ', offset);
    // var estimatedServerTimeMs = new Date().getTime() + offset;
  })
  .catch(console.error)

RTDB Docs

samthecodingman
  • 23,122
  • 4
  • 30
  • 54
  • 1
    You can also "ping" time.google.com, the timeserver used by GCP/Functions using this [SNTP Client](https://github.com/aslamanver/sntp-client-android) to manually estimate the client device time offset. – samthecodingman Jan 25 '20 at 10:12
  • Thanks @samthecodingman. Is there a way I always fetch the Timestamp from the server instead of the local device's time so that I do not have to thrive on offset estimates? – Maverick7 Jan 25 '20 at 10:18
  • That's what the SNTP client is for. It will return the server timestamp – samthecodingman Jan 25 '20 at 10:23
  • 1
    There is no equivalent for Firestore. – Doug Stevenson Jan 25 '20 at 18:37