Server Time And DST Helpers
MQL4 has no function that returns a broker's own GMT offset directly. TimeGMT() computes GMT from the local computer's time zone and daylight-saving settings, not the broker's - it knows nothing about which country the broker's servers observe DST rules for. The offset every published session or GMT-based indicator actually uses is the gap between TimeCurrent() (server time) and TimeGMT() (GMT as derived from the local machine). That is what this header computes too. It is the standard technique, not a shortcut, and its one real dependency - the local computer's clock and time zone being set correctly - is stated plainly rather than glossed over, because it is the one thing that silently breaks this on a misconfigured VPS. Once that offset exists, converting any timestamp between server time, GMT and local time is straightforward arithmetic, provided both directions are offered - this header keeps ServerToGmt/GmtToServer and ServerToLocal/LocalToServer as matched pairs so a value can always be converted back and checked. The DST heuristic is deliberately conservative about what it claims. There is no broker-agnostic way to know a server's DST calendar from inside MQL4 - brokers differ, and some do not observe DST at all. IsLikelyServerDst takes the broker's known non-DST offset as an input the caller must supply once, and reports only whether the current offset has moved away from it. It is a detector, not an oracle, and the .txt says so rather than implying more precision than exists. Notes and limitations - ServerGmtOffsetSeconds depends on the local computer's system clock and time zone being correct. On a VPS with the wrong time zone or an unsynced clock, every value this header derives from it is wrong by the same amount - check ServerTimeInfoToString once at startup and sanity-check it against a known reference before trusting downstream logic. - IsLikelyServerDst cannot tell DST apart from a broker simply changing its offset for an unrelated reason, and it says nothing about which direction a transition is heading - only that the offset no longer matches the baseline. - Rounding the offset to the nearest hour in the DST check assumes standard whole-hour shifts. A small number of regions use half-hour offsets; the heuristic will not be reliable there. - All conversions are pure arithmetic on the current offset at the moment of the call. They do not look up historical offsets, so converting an old timestamp assumes the current offset also applied then - true most of the time, false right around a DST transition.
Script · MT4 · v1.0 · Free
About Server Time And DST Helpers
Server Time And DST Helpers is a script for MetaTrader 4. It has 1 setting, such as Assumed winter offset hours. MQL source code included.
Files in the download: ServerTime.mqh, ServerTime.txt, ServerTimeDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Assumed winter offset hours | 2 | Broker's known non-DST GMT offset, for the DST test |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Trading Sessions · MT4 Script · MQL4 Source Code