Last updated

Timestamps

AgoraTimeStamp and UTCDateTime are the two methods for consistent handling of timestamps. The AgoraTimeStamp is defined as the number of milliseconds since Jan. 1, 1970 (UTC) as a double . It is expected that all times used between applications use AgoraTimeStamps to avoid miscommunication.

AgoraTimeStamp is not affected by timezone. When passing in a DateTime object to AgoraTimeStamp it converts the time correctly to UTC time and delivers the correct corresponding timestamp. Additionally, the DateTime returned by UTCDateTime is set to the UTC timezone.

For clarity, the code for AgoraTimeStamp and UTCDateTime is provided below:

public static class Time
{
    public static double AgoraTimeStamp(DateTime? tm = null)
    {
        if (tm == null)
            return (DateTime.UtcNow - DateTimeOffset.UnixEpoch).TotalMilliseconds;
        return (tm.Value.ToUniversalTime() - DateTimeOffset.UnixEpoch).TotalMilliseconds;
    }

    public static DateTime UTCDateTime(double tm) => 
        new(DateTimeOffset.UnixEpoch.Ticks + (long)(tm * 10000), DateTimeKind.Utc);
}
from datetime import datetime, timezone

def AgoraTimeStamp(tm=datetime.utcnow()) -> float:
    dt_utc = datetime(tm.year, tm.month, tm.day,
                      tm.hour, tm.minute, tm.second, tm. microsecond,
                      tzinfo=timezone.utc)
    delta = dt_utc.timestamp() - tm.timestamp()
    return (tm.timestamp() + delta) * 1000

def UTCDateTime(tm: float) -> datetime:
    dt = datetime.utcfromtimestamp(tm/1000).replace(tzinfo=timezone.utc)
    return dt

now_time_stamp = AgoraTimeStamp()
then_time_stamp = AgoraTimeStamp() - 10000

now_date_time = UTCDateTime(now_time_stamp)
then_date_time = UTCDateTime(then_time_stamp)