Firebase.ServerValue.TIMESTAMP returns object literal (JSON) instead of number
The global variable Firebase.ServerValue.TIMESTAMP returns an object literal ({.sv: "timestamp"}) that make both arithmetic and logical operation not possible to work.
Logical: https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L214
if (notification.notificationType !== 'suspension' || notification.data.suspendedUntil < Firebase.ServerValue.TIMESTAMP) {
notification.data.suspendedUntil < Firebase.ServerValue.TIMESTAMP will always be false.
(Note: notification.data.suspendedUntil will return as a string, see next error below).
Arithmetic: https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L457
suspendedUntil = Firebase.ServerValue.TIMESTAMP + 1000*timeLengthSeconds;
Let's say timeLengthSeconds is 10, the result of suspendedUntil will be "[object Object]10000".
Screenshot from my Firebase Forge:

I haven't checked for these line codes below, but they seem work fine: https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L295 https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L391 https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L406 https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L441
Thanks.
Great catch - I'll get a fix on it right away.
Is this fixed now? I'm still getting {.sv: "timestamp"} when retrieving a date created by Firebase.ServerValue.TIMESTAMP.
Fixed! Closing out.
So you just replaced ServerValue.TIMESTAMP with new Date().getTime()?
What if I want to get the actual Date from the object's property saved as ServerValue.TIMESTAMP:
$scope.authUserRef.transaction(function(user) {
user.SERVER_TIME = Firebase.ServerValue.TIMESTAMP;
return user;
}, function(error, committed, snapshot) {
if (error) {
console.wrror("Error getting the SERVER_TIME");
}
if (committed) {
var serv = snapshot.val();
var serverDate = new Date(snapshot.val().SERVER_TIME * 1000);
console.log("Server date: " + serverDate);
}
});
snapshot.val().SERVER_TIME = {.sv: "timestamp"}
How do I get the actual Date? Or maybe there is another way to get Firebase server's time?
@oburakevych When you pass around Firebase.ServerValue.TIMESTAMP, the { '.sv': 'timestamp' } value is used as a placeholder until actually written to Firebase via .set(), .push(), etc.
When one of those methods is called with the TIMESTAMP in it, the client will immediately fire any local events for listeners on that data using the client's local timestamp offset by the difference between client and server time as determined when you first connected to Firebase. This value is approximate, and becomes updated when the actual server-side write in Firebase completes.
To get the actual server time from Firebase for a write of some kind, you would first write to Firebase (i.e. ref.child('updated_at').set(Firebase.ServerValue.TIMESTAMP); and have listeners for that data (ref.on('value', function(snapshot) { // snapshot.val().updated_at });). There is no way to get the server time synchronously from Firebase, but the local time + server offset time (provided immediately whenever you write to Firebase using the ServerValue) is a good approximation.
Can't we get the timestamp value directly like in android instead of getting { '.sv': 'timestamp' }. Or can we parse it someway to get value? I am also stuck on this.
As Rob mentioned, you use server offset time. See Clock Skew in the docs for more details.
This issue is back: seeing the same value when saving: {.sv: "timestamp"},
for code:
data.updated_on = firebase.database.ServerValue.TIMESTAMP;
The firebase.database.ServerValue.TIMESTAMP value cannot be accessed directly. It is only resolved once it is used in a write, i.e. ref.set(...) or ref.update(...), in which case any functions reacting to changes at those references will be invoked with the replaced value.
@oburakevych When you pass around Firebase.ServerValue.TIMESTAMP, the
{ '.sv': 'timestamp' }value is used as a placeholder until actually written to Firebase via.set(),.push(), etc.When one of those methods is called with the TIMESTAMP in it, the client will immediately fire any local events for listeners on that data using the client's local timestamp offset by the difference between client and server time as determined when you first connected to Firebase. This value is approximate, and becomes updated when the actual server-side write in Firebase completes.
To get the actual server time from Firebase for a write of some kind, you would first write to Firebase (i.e.
ref.child('updated_at').set(Firebase.ServerValue.TIMESTAMP);and have listeners for that data (ref.on('value', function(snapshot) { // snapshot.val().updated_at });). There is no way to get the server time synchronously from Firebase, but the local time + server offset time (provided immediately whenever you write to Firebase using the ServerValue) is a good approximation.
@robertdimarco Is there any way I can get the server time offset without writing anything to firebase?