datetime construction

 

Hi all, I want to create an automatic expiration datetime instead of my usual method of counters and checks. My question is: given datetime CurrTime = blahblah, how do I create datetime ExpTime = CurrTime + x minutes? I looked at the doc to try and figure out how to create a datetime, but it only lists static examples, which are useless to any programmer. Knowing how to concatenate elements of datetime would be nice. If we knew that, we could simply do TimeMinute(CurrTime), etc., and add the time shift. Unfortunately I also don't know how to do that.

Thanks in advance for help. =)

 
ubuntuboy:

Hi all, I want to create an automatic expiration datetime instead of my usual method of counters and checks. My question is: given datetime CurrTime = blahblah, how do I create datetime ExpTime = CurrTime + x minutes? I looked at the doc to try and figure out how to create a datetime, but it only lists static examples, which are useless to any programmer. Knowing how to concatenate elements of datetime would be nice. If we knew that, we could simply do TimeMinute(CurrTime), etc., and add the time shift. Unfortunately I also don't know how to do that.

MT4 dates are simply a Unix-style number of seconds since 1/1/1970. The datetime type in MT4 is effectively an int, and can be treated as an int. Therefore, adding e.g. 60 to a datetime advances it by one minute. Similarly, TimeCurrent() + 3600 is an hour in advance of the current broker time.

All this makes it very easy to do date arithmetic involving any period up to and including a number of weeks, but makes it relatively hard to do addition or subtraction of months, or to construct the datetime value for a specific day, month, and year.
 

Ah, okay. Thanks. That explains why something like datetime x = 0; is valid. Why not just use an int? Clarity? Or maybe to include the ability to easily specify a date using the D'' format?

I suppose to construct a specific day, month, and year, you would need to identify how many leap years there were between the specified year and 1970. From there, wouldn't it be a simple matter of multiplying 60 * NumDaysFrom1970?

 
ubuntuboy:

I suppose to construct a specific day, month, and year, you would need to identify how many leap years there were between the specified year and 1970. From there, wouldn't it be a simple matter of multiplying 60 * NumDaysFrom1970?

It's a bit crazy, but it's easiest to do it via the StrToTime() function (which is very forgiving about formats and separators). For example:

   int day = 8;
   int month = 7;
   int year = 2010;
   int hour = 11;
   int minute = 38;
   int second = 0;
   string strTime = year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second;
   
   datetime whatever = StrToTime(strTime);
 

I use StrToTime in init() once. And use datetime like integer in iteration loop. See the example indy in this post.  

I suppose to construct a specific day, month, and year, you would need to identify how many leap years there were between the specified year and 1970. From there, wouldn't it be a simple matter of multiplying 60 * NumDaysFrom1970?

The method you mentioned is used by Kim-IV here.  HTH. 

Reason: