question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

Do I need to call GetTime API for time-based events?

Hello everyone,

I was using Javascript's Date object to manage time-based events in the Cloudscript. I came across an API called GetTime in the ServerAPI. I was wondering if it is necessary to call that API for time-based events? I would like to work with a time value regardless of server locations.

For example; when players create a group and do something in common (e.g. upgrading their HQ), will the time be different depending on who calls the ExecuteCloudscript API?

Let's say there are 2 players and they are in the same group (clan). One of them is from the USA, the other one is from Germany, and the upgrade process takes 30 minutes. If the player in the USA calls the ExecuteCloudscript API to start the upgrade process, will the player in Germany see that the HQ will be upgraded in 30 minutes? (Let's assume that the player in Germany logs in to their account right after the upgrade process is started and retrieves the time value that is saved in the database)

The player in the USA calls this API:

handlers.UpgradeHQ = function(args, context) {
    // ...

    var now = new Date().getTime();

    // Add 30 minutes to the current time
    var endTime = now + (30 * 60 * 1000);

    // save 'endTime' to the database
};

The player in Germany calls this API after 'UpgradeHQ' is called:

handlers.GetUpgradeTime = function(args, context) {
    // get that 'endTime' value from the database

    var now = new Date().getTime();

    // Return the time difference in miliseconds
    var timeDiff = endTime - now;
    return timeDiff;
};

The player in Germany should get a value ~1800000 (which is 30 minutes in milliseconds). In the other words, will the "now" variables be the same for the players from different counties?

10 |1200

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

1 Answer

·
Rick Chen avatar image
Rick Chen answered

In CloudScript, the GetTime API is not necessary, Date().getTime() should do the job. The GetTime API is designed to help clients or external servers to get the PlayFab server time, in UTC. Please refer to this thread: https://community.playfab.com/questions/47919/dategettime-vs-servergettime.html

The Date().getTime() also uses UTC for time representation, which would be the same for different timezone. Please refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime

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.