Server Time
This guide outlines the steps to migrate from the deprecated LionGameInterfaces.Time.GetServerTime or NakamaConnection.GetServerTime to the new GlobalConfigHandler approach for retrieving server time.
Overview
The previous methods for fetching server time via LionGameInterfaces or direct NakamaConnection calls have been replaced. The new standard uses GlobalConfigHandler located in the WhoAmI service to fetch the time configuration directly.
❌ Old Way (Deprecated)
Previously, you might have accessed server time using LionGameInterfaces or a custom interface implementation:
var time = await LionGameInterfaces.Time.GetCurrentTimeUTC();✅ New Way (Recommended)
You should now retrieve the server time string from GlobalConfigHandler, parse it as a Unix timestamp (milliseconds), and convert it to a DateTime object.
Implementation Steps
Fetch the string using
GlobalConfigHandler.GetInfo<string>("serverTime").Parse the string into a
long(Unix timestamp in milliseconds).Convert the timestamp to a UTC
DateTime.
Code Example
using LionStudios.Suite.Core;
// Ensure you have this namespace
using System;
using System.Threading.Tasks;
public async Task<DateTime> GetCurrentServerTime()
{
// 1. Get the server time string from Global Config
string serverTimeStr = await GlobalConfigHandler.GetInfo<string>("serverTime");
// 2. Parse string to long (Unix Milliseconds)
if (long.TryParse(serverTimeStr, out long unixMs))
{
// 3. Convert to DateTime UTC
DateTime utcTime = DateTimeOffset.FromUnixTimeMilliseconds(unixMs).UtcDateTime;
return utcTime;
}
// Fallback if parsing fails (e.g., return local UTC or throw)
return DateTime.UtcNow;
}One-Liner (If you are confident in the data)
DateTime utcTime = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(
await GlobalConfigHandler.GetInfo<string>("serverTime"))).UtcDateTime;Summary of Changes
Access Point
LionGameInterfaces.Time / NakamaConnection
GlobalConfigHandler
Method
GetCurrentTimeUTC() / custom WebRequest
GetInfo<string>("serverTime")
Return Type
DateTime (often hidden implementation)
string (Unix Timestamp MS)
Namespace
LionStudios.Suite.Core
LionStudios.Suite.Core
Troubleshooting
Missing Namespace: Ensure you are using
LionStudios.Suite.Core.Parsing Errors: The
serverTimekey returns a string representation of along. Always uselong.Parseorlong.TryParse.Async/Await:
GlobalConfigHandler.GetInfois asynchronous. Ensure your calling method isasyncand valid for use (e.g., not in a constructor).
Last updated
Was this helpful?