Global Variable as datetime - page 2

 
Alberto Tortella:

Hi all, is it possible to save a global variable as datetime format?

I'm trying with the following code, but I see it as a string.

you can use 'union'

void OnInit()
  {    
  string d     = "TIME_BUY_"  ;
  string e     = Symbol()     ;    
  string TIME_BUY_    = d + e ;
  GlobalVariableSet(TIME_BUY_, UlongToDbl(iTime(NULL,PERIOD_CURRENT,0))); 
  
  Print( (datetime)DblToUlong(GlobalVariableGet(TIME_BUY_)) );   
  }


//+------------------------------------------------------------------+
union eightbytes
   {
   double   dbl;
   ulong    ulng;
   } VALUE;
double UlongToDbl(ulong one)
   {
   VALUE.ulng=one;
   return(VALUE.dbl);
   }
ulong DblToUlong(double one)
   {
   VALUE.dbl=one;
   return(VALUE.ulng);
   }
 

https://www.mql5.com/en/forum/432809


Kindly see this thread, you can cast MQL4 datetime into double and later convert back to datetime.

They both use 8 bytes and datetime is most likely just a long under the scene (integer).


The possible data loss:

https://stackoverflow.com/questions/32783923/how-big-is-the-precision-loss-converting-long-to-double


For more details, we might scan thru these insightful discussion.

In the original MQL4, a “datetime” data-type was originally 4 bytes, presumably an “int” data-type with a limit of 2147483647 that would give a maximum date-time of D’2038.01.19 03:14:07’. Later on, with the appearance of MQL5 and update to MQL4+, the “datetime” datatype was updated to 8 bytes, presumably a “long” data-type with a limit of 9223372036854775807. However, according to the documentation, the date-time limit is 31 December, 3000. Its limit is probably D’3000.12.31 23:59:59’ or a value of 32535215999 seconds, which in hexadecimal would be 0x7933FFF7F and would occupy 4.5 bytes.  

https://www.mql5.com/en/forum/427424

Get datetime in epoch/timestamp
Get datetime in epoch/timestamp
  • 2022.09.17
  • eashanm
  • www.mql5.com
Hello, I'm trying to get the opening time of a bar in timestamp/epoch...