From Datetime value get uint or ulong number of seconds elapsed since January 01 1970, and vice versa.

 

We all know that the Datetime type is actually storing the integer number of seconds elapsed since January 01 1970, but it seems there is no function to get access to this particular value, or to get a Datetime value given the number of seconds elapsed since January 01 1970.

Do you know any good way of doing this?

My intention is to store Datetime values as uint type values to save twice memory space.

This would allow me to store Datetime values until year 2106!

I kind of promised myself that if I won’t be a rich forex trader by that year, I will switch to the pizza delivery business.

I don’t understand why MQL5 is going as far as 31 December 3000, making the datetime type as bulky as 8 bytes (rather than the 4 bytes of the uint type)

Fast Dive into MQL5
  • 2012.08.02
  • MetaQuotes Software Corp.
  • www.mql5.com
You have decided to study MQL5 trading strategies' programming language, but you know nothing about it? We have tried to examine MQL5 and MetaTrader 5 terminal from the newcomers' point of view and have written this short introductory article. In this article, you can find a brief idea of the possibilities of the language, as well as some tips on working with MetaEditor 5 and the terminal.
 

You can get the low part of time value.


datetime  t;
uint      my_t=(uint)t;

 

Why do you need to save up the variable space so badly? Space optimization of the EAs/Indicators is the last thing having to do with success in Forex.
 
mql5:

You can get the low part of time value.

datetime  t;
UINT       my_t=(UINT)t;
Could you please give me a fully working example code?
enivid:

Why do you need to save up the variable space so badly? Space optimization of the EAs/Indicators is the last thing having to do with success in Forex.
That's because I'm doing some mass backtesting using scripts. This scripts are recording on files the time of each virtual opened trade and closed trade, which at the end of the calculation are hundreds of thousands.
 
claudio:
Could you please give me a fully working example code?That's because I'm doing some mass backtesting using scripts. This scripts are recording on files the time of each virtual opened trade and closed trade, which at the end of the calculation are hundreds of thousands.
logical design is far better than backtesting from past. Use demo account to test forward ( future is only ahead man)
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Account Properties - Documentation on MQL5
 

ok I figured !

This example code works great

Thanks a lot "Mql5"

datetime TimeArray[1];
CopyTime(_Symbol,PERIOD_M1,0,1,TimeArray);
datetime  t=TimeArray[0];
uint       my_t=(uint)t;
Print(t," is equal to ",my_t," seconds after January 01, 1970");

// Creating the opposite example
my_t-=3600; // 3600 seconds=1 hour JUST MAKING A DIFFERENT NUMBER
t=(datetime)my_t;
Print(my_t," seconds after January 01, 1970 are equal to ",t);
What is this type of operation?
my_t=(uint)t

Could you please point it out on the MQL5 reference.

You light up my day, Thankssssss ;-)

 
claudio:
What is this type of operation?
It is truncation. See typecasting.
Reason: