Files
ClassicAPI/src/time/Server.h
T
Brues aacace1645 fix(time): take the server clock from the engine's own time sync
`GetServerTime` had two separate problems.

The global is not ours to keep. Registration happens inside
FUN_LOAD_SCRIPT_FUNCTIONS and FrameXML loads after that, so a client
whose GameTime.lua declares its own `function GetServerTime()` replaces
it on every login and every /reload, and no addon can reach the C
function again. Turtle's version returns (serverHour, serverMinute) --
which is itself just the player's own machine clock, since it only
diverges from date() if the realm answers a TW_TIME broadcast, and
nothing in the public core does. Add C_DateAndTime.GetServerTime: the
same C function under a name FrameXML never touches.

The value was also the wrong clock. It was rebuilt from the broadcast
gametime struct, which carries minute granularity, no timezone, and
whatever per-map time offset the realm folded in before sending. The
engine already has a better source: FUN_004DE430 (quest-log enter-world
setup) sends CMSG_QUERY_TIME unconditionally, and the handler caches
localEpoch - serverEpoch at VAR_SERVER_TIME_DELTA, refreshed hourly. The
reply is a raw time(nullptr), so the skew yields a true UTC epoch with
real seconds, free of both the timezone and the map offset. The sign is
confirmed by the engine's own use of it -- the timed-quest expiry check
in FUN_004DE510 converts a server timestamp into local-clock space by
ADDING the delta.

The two clocks answer different questions, so Time::Server exposes both
and each function takes the one its contract names:

  CurrentEpoch()     the instant  GetServerTime, GetSecondsUntilDailyReset
  RealmClockEpoch()  the display  GetCurrentCalendarTime, GetServerTimeLocal

GetServerTimeLocal is "the epoch offset by the server's timezone", which
RealmClockEpoch already is by construction, so its round-trip through
the *player's* timezone is gone. GetSecondsUntilDailyReset moves the
other way: a realm buckets its day over a raw time(nullptr), so the
boundary is UTC midnight rather than the displayed midnight.

Verified in-game across an Azeroth -> Kalimdor port on two realms, one
on UTC and one on UTC-5. The instant stays continuous (+51 s across the
port, matching real elapsed time) while GetGameTime jumps 12 hours;
GetServerTimeLocal tracks the realm's displayed clock and rolls the date
with it; GetCurrentCalendarTime's hour matches GetGameTime in both zones.

Fixes #50
2026-09-11 12:17:37 -05:00

51 lines
2.3 KiB
C++

// This file is part of ClassicAPI.
//
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <cstdint>
namespace Time::Server {
// The client receives TWO different clocks, and they answer two different
// questions. Both accessors return 0 when their source has no data yet
// (before login). Pick by what the caller's contract says, not by
// convenience — on a realm that is not on UTC, or in a zone the realm time
// -shifts, these return very different values.
// The instant: a true Unix epoch, seconds since 1970-01-01 UTC. Backed by
// the engine's own `CMSG_QUERY_TIME` sync, so it has real seconds and is
// free of both the realm's timezone and any per-map time offset. Use it for
// anything that compares, sorts, stores, or counts down — `GetServerTime`
// and the daily reset.
//
// Falls back to `RealmClockEpoch` for the ~1 RTT after enter-world before
// the sync lands, and on a server that never answers the query. That
// fallback is the wrong clock by the realm's UTC offset, so a caller can
// see the value jump once during the first moments of a session.
int64_t CurrentEpoch();
// The realm's wall clock, expressed as the epoch that renders back to those
// same digits under UTC. Built from the gametime the server broadcasts, so
// it matches `GetGameTime()` exactly — including a per-map time offset where
// the realm applies one. Minute granularity, with `GetTickCount` filling the
// sub-minute part.
//
// This is not an instant and must not be compared against a real timestamp.
// Use it only to DISPLAY realm time: the calendar's date and time fields,
// and `GetServerTimeLocal`, whose contract is "the epoch offset by the
// server's timezone" — which is precisely this value.
int64_t RealmClockEpoch();
} // namespace Time::Server