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();

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

  1. Fetch the string using GlobalConfigHandler.GetInfo<string>("serverTime").

  2. Parse the string into a long (Unix timestamp in milliseconds).

  3. 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

Feature
Old Approach
New Approach

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 serverTime key returns a string representation of a long. Always use long.Parse or long.TryParse.

  • Async/Await: GlobalConfigHandler.GetInfo is asynchronous. Ensure your calling method is async and valid for use (e.g., not in a constructor).

Last updated

Was this helpful?