ServerTime API
ServerTime API
The tudada server time (epoch ms) to use instead of the device clock when deciding timed rewards (daily/weekly rewards, event periods). Users can change the device clock, so base reward decisions on server time.
Usage has two steps.
- Sync once on entry —
GetServerTime(). After the first success, the SDK re-syncs automatically whenever the app returns from the background. - Read instantly when deciding —
GetServerTimeSync(). Returns the time projected from the last sync without calling the server. If no sync has ever succeeded it returnsnull; do not grant in that case.
Calling GetServerTime() once more when the reward UI opens keeps you safe in environments that send no resume signal (review, build tests).
GetServerTime(onSuccess, onFail)
Fetches the server time. On failure, hold the reward.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
onSuccess | Action<ServerTimeResult> | - | Success callback |
onFail | Action<ServerTimeFailResult> | - | Failure callback |
Success Response (ServerTimeResult):
| Field | Type | Description |
|---|---|---|
now | long | Server time (integer epoch ms, no timezone) |
errMsg | string | Result message |
Failure Response (ServerTimeFailResult):
| Field | Type | Description |
|---|---|---|
reason | string | Failure reason (table below) |
errMsg | string | Error message |
reason | Meaning | What to do |
|---|---|---|
network | Network error | Retry shortly |
timeout | Response timed out | Retry shortly |
bad_response | Malformed response | Retry shortly |
unsupported | The current environment does not provide server time | It fails immediately for 5 minutes; after that, the next GetServerTime() call (or, once a sync has succeeded, the automatic re-sync on returning from the background) tries again. Hold rewards meanwhile. If the WebGL template still loads an old JS SDK bundle (below 0.3.0), it stays unsupported until the bundle is replaced |
hidden | The request was discarded because the app went to the background, or it was called while the page was hidden | Call again after returning to the foreground |
TudadaSDK.Instance.GetServerTime(
onSuccess: (result) => Debug.Log("Server time: " + result.now),
onFail: (result) => Debug.Log("Unavailable: " + result.reason)
);
GetServerTimeSync()
Returns the server time projected from the last sync immediately, without calling the server.
Return Value (ServerTimeSync, or null before the first sync):
| Field | Type | Description |
|---|---|---|
now | long | Server time (integer epoch ms). It may lag behind real time by however long the app was suspended, but it never runs ahead |
stale | bool | true while the re-sync after returning from the background has not yet succeeded |
var t = TudadaSDK.Instance.GetServerTimeSync();
if (t != null && t.now >= nextRewardAt)
{
GrantDailyReward(); // safe even when t.stale — the value never runs ahead
}
The "never runs ahead" guarantee excludes clock skew between server instances (NTP drift). This API does not guarantee absolute clock accuracy.
In the Editor, GetServerTime() responds with the device UTC time, and from then on GetServerTimeSync() also returns the device UTC time (null before the first call).