question

lu.zhang@sectionstudios.com avatar image
lu.zhang@sectionstudios.com asked

How to get the current timestamp of the Playfab server?

I want to do some time verification for player's building progress. Is there anyway to get the timestamp of the server? So I can record the start time that the player start building something and also check whether the building is complete after a while.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

[Edit: Since someone just asked me about this post in reference to clock drift, I've updated it to have the more recent info on that (which you can find here: Title-Wide Data Management - Get Time (PlayFab Client) | Microsoft Docs)

There are a couple of options for this:

1. Use the datetime returned from the server. In the response, it's a Header value named "Date".

2. Use Cloud Script to validate the update progress, using the standard getTime in Date().

In either case, it's important to bear in mind that there will be a small amount of clock drift between servers, so the time reflected by either call could differ somewhat (up to 5 seconds).

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Dean Loades avatar image Dean Loades commented ·

@Brendan I am using the unity plugin and for the life of me I can't find anywhere where a timestamp is returned from the server. It may be easy to get with a web response, but in Unity I am really struggling.

Can you point it out for me?

Else I am going to have to write a CloudScript function for this one task, which seems a bit daft.

0 Likes 0 ·
brendan avatar image brendan Dean Loades commented ·

You should be able this from the response headers in Unity as responseHeaders["Date"];

To make this easier though, we are planning on updating with a specific "GetTime" API call in an upcoming release.

0 Likes 0 ·
Josh Winter avatar image
Josh Winter answered

I would also like to know if this is possible - thanks.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

usachovdailymagic avatar image
usachovdailymagic answered

Here is the method. Add it to your cloudscript and use everywhere you want

//use_server_timestamps_in_seconds - bool
function getServerTimestamp(use_server_timestamps_in_seconds) { var now = new Date(); var time = now.getTime(); // miliseconds // Get timestamp in seconds if ( use_server_timestamps_in_seconds ) { time = Math.floor(time / 1000); // cast to seconds } return time; }

...
...
...

var tmstmp_seconds = getServerTimestamp(true);
var tmstmp_miliseconds = getServerTimestamp(false);
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.