Set timeZone UTC

 

Since it seems it is not possible to convert to UTC time zone, I thought of putting a C++ method in a .mqh file. But apparently there is a compilation problem. Can anyone give me some help?


    #include <iostream>
    #include <ctime>
    #include <string>

    std::string now()
    {
      std::time_t now= std::time(0);
      std::tm* now_tm= std::gmtime(&now);
      char buf[42];
      std::strftime(buf, 42, "%Y%m%d %X", now_tm);
      return buf;
    }}  
 
rom:

Since it seems it is not possible to convert to UTC time zone, I thought of putting a C++ method in a .mqh file. But apparently there is a compilation problem. Can anyone give me some help?


In the tester ? 

 
rom: Since it seems it is not possible to convert to UTC time zone, I thought of putting a C++ method in a .mqh file. But apparently there is a compilation problem. Can anyone give me some help?

The language is called "MQL". It is not called "C++". Just because MQL is based on C/C++, does not mean that you can use pure C++ code.

If you wan to convert or use zone based time functions, then use the MQL functions for it — Documentation on MQL5: Date and Time

Please note that some of these functions are not supported in the Strategy Tester.

Function

Action

TimeCurrent

Returns the last known server time (time of the last quote receipt) in the datetime format

TimeTradeServer

Returns the current calculation time of the trade server

TimeLocal

Returns the local computer time in datetime format

TimeGMT

Returns GMT in datetime format with the Daylight Saving Time by local time of the computer, where the client terminal is running

TimeDaylightSavings

Returns the sign of Daylight Saving Time switch

TimeGMTOffset

Returns the current difference between GMT time and the local computer time in seconds, taking into account DST switch

TimeToStruct

Converts a datetime value into a variable of MqlDateTime structure type

StructToTime

Converts a variable of MqlDateTime structure type into a datetime value

Documentation on MQL5: Date and Time
Documentation on MQL5: Date and Time
  • www.mql5.com
Date and Time - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Lorentzos Roussos #:

In the tester ? 

Hi Lorentzos, thank you for your interest! I don't use tester. In real time, I would like to print such an output "14/12/2022 14:12:38.207", but I need to set the UTC time zone.
 
rom #:
Hi Lorentzos, thank you for your interest! I don't use tester. In real time, I would like to print such an output "14/12/2022 14:12:38.207", but I need to set the UTC time zone.

Then they have such a provision already

TimeGMT()

It returns the local time , and , in mt5 if you want the estimated server time (of the broker) they have also provided 

TimeTradeServer()

Which does not stop on friday unlike 

TimeCurrent()

In Mt4 some ninja stuff must be done to find the live broker time .

 

With:

https://www.mql5.com/en/articles/9926
https://www.mql5.com/en/articles/9929

you can detect even in the tester GMT (=UTC) as well as the time in EU and USA and then you can use these time stamps instead of TimeCurrent():

void OnTick() {
...   
   tC = TimeCurrent();
   checkTimeOffset( tC );
   tGMT  = tC + OffsetBroker.actOffset;   // GMT
   tNY   = tGMT - (NYShift+DST_USD);      // time in New York
   tLon  = tGMT - (LondonShift+DST_EUR);  // time in London
   tEU   = tGMT - (FfmShift+DST_EUR);     // time in FFm
...
Dealing with Time (Part 2): The Functions
Dealing with Time (Part 2): The Functions
  • www.mql5.com
Determing the broker offset and GMT automatically. Instead of asking the support of your broker, from whom you will probably receive an insufficient answer (who would be willing to explain a missing hour), we simply look ourselves how they time their prices in the weeks of the time changes — but not cumbersome by hand, we let a program do it — why do we have a PC after all.
 
Carl Schreiber #:

With:

https://www.mql5.com/en/articles/9926
https://www.mql5.com/en/articles/9929

you can detect even in the tester GMT (=UTC) as well as the time in EU and USA and then you can use these time stamps instead of TimeCurrent():

Excellent work @Carl Schreiber 👏 👏

Reason: