Returns the current server time in hours and minutes
hours,minutes = GetGameTime();
- Number - The current hour (0-23).
- Number - The minutes passed in the current hour (0-59).
local hour,minute = GetGameTime(); message(hour .. ":" .. minute);
A message with the current server time appears.
This function can unexpectedly return results inconsistant 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 particualry noticable 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);
So for example you log into US Ysera EST at 18:00, and enter Ulduar runing 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 important functionally, such as diffing times or using as part of functional historical data.
For getting consistant 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 mintue accuracy. Client time is good because you have second precision, but everyones 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 tiemzone, and lots of variance. The "instance server not same time as login server" problem suddenly makes the server time much less valuable for consistancy. 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 mintues, though not typicaly the case. Using local time for most apps where a univeral time isnt important is the way to go.
A third choice for consistant 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 acruate 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; its a bit contrived to get it right. What you end up with however is as near second accruacy universal server time across all clients. This depends on how accurate minute rollovers form server time are and using the cients seconds counter should not drift any perceptable amount inside a mintute. 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 dont know how often or what triggers actual server time updates to the client. But in practice ive 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 loging 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 natuarally be corrected within a minute or so of entering realm world server.
This is alot of trouble to get simple consistant server time, adn ... may seem crazy to worry about. For some apps is more than nice to have, ala player joins and leaves for say raid tracker for example and dealing wiht non-trival things for users to fix after the fact like wtf dkp upload results, its worth it. The core issue isnt having accruacy like an atomic clock, but not haivng time values jump in a way that makes the data absurd or inconsistant or impossible to process like dkp uploads, and a close second is all users having the basic same time of the day in a consistant format and time zone, which is really the same thing for multi user uploads in my example. All blizz cound do to fix this would be to make a hole where to continue 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 ther is such a thing, and is all probably not a trivial thing to change.
And.. while im on such a roll.. I suspect the server time updates are one of two things. They are 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 its an out of band update to the clients every so often where there would be no real seconds value to begin with. A third possiblity 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 is out of band and special the last example would be the least bandwidth by far. Dun make me re-synthesize what would need extra return value pls pls. :)