Retrieving Twitter Server time

Nov 26, 2013 at 5:54 PM
Edited Nov 26, 2013 at 5:59 PM
Hi,

I'm looking to retrieve the server time from twitter. It should be included in the response's header. Does the API support this?

In the meantime, I have made a local change to the IToken interface to include a property for the server's datetime:
    public DateTime ServerTime { get; set; }
And inside private Dictionary<string, object>[] ExecuteQuery I changed this:
if (_lastHeadersResult != null && _lastHeadersResult["X-RateLimit-Remaining"] != null)
{
    XRateLimitRemaining = Int32.Parse(_lastHeadersResult["X-RateLimit-Remaining"]);
}
To this:
if (_lastHeadersResult != null)
{
    if (_lastHeadersResult["X-RateLimit-Remaining"] != null)
    {
        XRateLimitRemaining = Int32.Parse(_lastHeadersResult["X-RateLimit-Remaining"]);
    }
    if (_lastHeadersResult["Date"] != null)
    {
        DateTime serverTime;
        if (DateTime.TryParse(_lastHeadersResult["Date"], out serverTime))
        {
            ServerTime = serverTime;
        }
    }
}
_kurt
Coordinator
Nov 27, 2013 at 8:41 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Nov 27, 2013 at 8:47 AM
Hi,

I am not happy with the current way to retrieve the X-RateLimit with the current Release.
As a result I checked a solution allowing you to retrieve the DateTime of the previous request.
        public DateTime ServerDateTimeOfPreviousRequest
        {
            get
            {
                if (_lastHeadersResult == null)
                {
                    throw new Exception("You cannot access the server datetime before performing any HttpRequest");
                }

                if (_lastHeadersResult["Date"] == null)
                {
                    throw new Exception("Twitter didn't provide the server time information during the previous HttpRequest");
                }

                return DateTime.Parse(_lastHeadersResult["Date"]);
            }
        }
Kind Regards,
Linvi
Nov 27, 2013 at 5:23 PM
In my particular case, I am trying to calculate the difference in time (local vs. twitter). After giving it some thought, I ended up adding a second property that does just that.
public TimeSpan LocalToServerTimeOffset { get; set; }
...
                    if (_lastHeadersResult["Date"] != null)
                    {
                        DateTime serverTime;
                        if (DateTime.TryParse(_lastHeadersResult["Date"], out serverTime))
                        {
                            LocalToServerTimeOffset = DateTime.Now - serverTime;
                            ServerTime = serverTime;
                        }
                    }
I think others may find this property useful as well.

Cheers
_kurt
Coordinator
Nov 27, 2013 at 5:29 PM
Hi,

Just being curious, why do you need this information?

Linvi
Nov 28, 2013 at 7:34 PM
I can see why you'd want this: the timestamp of the tweet is relative to the time of the server, if the client's local-time is not perfectly in-sync the current messages would appear to either be in the past or future. Especially useful when using the streaming API.