API GetGameTime

From Wowpedia
Jump to: navigation, search

Returns the current server time in hours and minutes

hours,minutes = GetGameTime();

Parameters

Returns

hours 
Number - The current hour (0-23).
minutes 
Number - The minutes passed in the current hour (0-59).

Example

local hour,minute = GetGameTime();
message(hour .. ":" .. minute);

Result

A message with the current server time appears.

Notes

This function can unexpectedly return results inconsistent with actual realm sever time. The value returned is from the physical instance server you are actually playing on, and not that of the world instance server (realm server) you log into. Servers for instances such as for raids and pvp are often shared between login world servers. And instance servers are not always running using the same timezone as the login realm server. This is particularly noticeable for Oceanic and other low population world servers. See below for more.

To use this in a macro:

/script local hour,minute = GetGameTime(); DEFAULT_CHAT_FRAME:AddMessage(hour .. ":" .. minute);

More notes

As of Warlords of Draenor, you should probably be using GetServerTime() instead because it is more precise.

So for example you log into US Ysera EST at 18:00, and enter Ulduar running on an instance server that is PST. Suddenly the time returned will be 3 hours behind the time you were getting. Confusing but important :) Please keep this in mind if using this time for anything functionally important, such as diffing times or using as part of historical data.

For getting consistent times you have two basic choices: use server or use local client time. Server time is good because all players in a realm will have the same time within more or less a minute accuracy. Client time is good because you have second precision, but everyone's times are different, as not only do people log into realm servers from other timezones, but you also have no idea how correct their local time is, from unset clocks to bad user timezone, and lots of variance. The "instance server not same time as login server" problem suddenly makes the server time much less valuable for consistency. Two reasons: even if the server is in the same timezone as the login server, you might have variance between the two server like 5-10 minutes, though not typically the case. Using local time for most apps where a universal time isn't important is the way to go.

A third choice for consistent times is to mix the two and run a timer correction update event in the background, where you get a base time difference between local and server time as to correct local time to server time, and base seconds (plus diff) purely off of local time. So: seed base -> use local time + base as time value in addon (server time with second precision) -> continue every fraction of second or so to see if server time has rolled over to next minute -> reconcile and update base -> and on and on... The update event basically corrects the base difference over time to keep it accurate with server time. The base correction update is necessary because users' computers can drift dramatically depending on the computer, and because users can choose to change their timezone or clock at any time. Also doing time diffs and adds is not as simple as it sounds; it's a bit contrived to get it right. What you end up with however is as near second accuracy universal server time across all clients. This depends on how accurate minute rollovers from server time are and using the clients' seconds counter should not drift any perceptible amount inside a minute. I'm guessing the WoW client itself is basing the server time minute rollovers on local client time as well, just not shared with us through this function, and i don't know how often or what triggers actual server time updates to the client. But in practice i've observed this working very very well. However... with the newer problem (the original reason for the note) you will need to basically shut off the base correction when a player enters an instance and save the difference value to your saved vars, in case the player restarts or logs into an instance instead of the realm world server. This leaves the problem of a user installing the addon and logging on to an instance server first rather than the realm world server (and people who raid know that can really happen a lot, ala "oh let me go enable or install that addon i need, brb"). In this case you will need to seed the base with the difference of the instance server just once and then follow the rule of shutting it off in an instance, where it will naturally be corrected within a minute or so of entering realm world server.

This is a lot of trouble to get simple consistent server time, and may seem crazy to worry about. For some apps it's more than nice to have, ala player joins and leaves for say raid tracker for example and dealing with non-trivial things for users to fix after the fact like wtf dkp upload results, it's worth it. The core issue isn't having accuracy like an atomic clock, but not having time values jump in a way that makes the data absurd or inconsistent or impossible to process like dkp uploads, and a close second is all users having the basic same time of the day in a consistent format and time zone, which is really the same thing for multi-user uploads in my example. All Blizzard could do to fix this would be to make a way to get server time from the realm server for addons and possibly share the seconds counter as a third return value in this API function if there is such a thing, and this would probably be a trivial thing to change.

And.. while i'm on such a roll.. I suspect the server time updates are one of several things. They could be taken directly from other data for other purposes like timings from one of the standard server game clocks out of packets intended for avatar updates and the like with the seconds clipped off. Or it's an out of band update to the clients every so often where there would be no real seconds value to begin with. A third possibility would be what I originally suspected, is that is a very occasional update from the server out of band, and updated by the client based on local time anyway. If it's out of band and special the last example would be the least bandwidth by far.

See also