1

I need unchanging server time value. I am using Firebase and Unity3d.

I want to get timestamp value as string using Unity Firebase.

I updated ServerValue.Timestamp. I changed local Time on my mobile. And then I got the value that I updated. My app returned local timestamp value instead ServerValue.Timestamp that firebase database showed.

How can I use ServerValue.Timestamp in Firebase Unity?

Hemant
  • 1,961
  • 2
  • 17
  • 27
maduckhoo
  • 11
  • 1
  • 2
  • Timestamps are a corner of RTDB that I haven't yet fully explored. What I would do to test this out is to attach a ValueChanged listener (https://firebase.google.com/docs/database/unity/retrieve-data#value-events) to a node then write ServerValue.Timestamp into that node (https://firebase.google.com/docs/reference/unity/class/firebase/database/server-value#timestamp) and print out what you see in the event handler. I suspect that when it hits the local cache, you'll get local value. Then eventually the server. See this video for ValueChanged @~10:13: https://youtu.be/MbIH4QT3xF8?t=613 – Patrick Martin Dec 23 '19 at 16:48

2 Answers2

1

To get an accurate server time, you need to get it from the server somehow. From the docs ServerValue.Timestamp is: "A placeholder value for auto-populating the current timestamp (time since the Unix epoch, in milliseconds) by the FirebaseDatabase servers."

If you push some data to your realtime database that contains that placeholder as it's value, then read it back you should have the server time of that write. Eg.

database.update({ startedAt: Firebase.ServerValue.TIMESTAMP });

(Check this post out)

You can also estimate clock skew, or request the server time driectly from the server using Cloud Functions.

NSJacob1
  • 509
  • 3
  • 12
1
  1. Successfully Firebase Initialized in script(Initialized in Awake())

  2. Call method PostToFirebase() one time. in any place(like start())

       private void PostToFirebase()
       {
         FirebaseDatabase.DefaultInstance.RootReference.Child("TimeStampTest")
        .Child("TimeStampValue").SetValueAsync(GetCurrentTimeStampForFirebaseServer());
    
        }
        public static object GetCurrentTimeStampForFirebaseServer()
        {
           return ServerValue.Timestamp;
        }
    
Atul Patel
  • 71
  • 2