Features of the mql5 language, subtleties and tricks - page 252

 
amrali #:

The function is a smart subset of the StructToTime algorithm in mql5

Unfortunately, the source code of StructToTime is not analysed by the compiler, but is attached as an imformed function. Therefore, StructToTime counts all the fields of the structure at each call.

The MQL5 compiler is able not to count unused fields, but this requires the source code to be analysed.


MQ has repeatedly said that they will include the source code of standard functions at compilation. Unfortunately, this did not happen with StructToTime.

 
amrali #:

mktime in C https://github.com/lattera/glibc/blob/master/time/mktime.c.

Perfectly formatted:

Easy to understand source code.

// https://github.com/NewYaroslav/xtime_cpp/blob/master/src/xtime.hpp
struct MqlDateTime 
  { 
   int year;           // uint32_t get_year(const timestamp_t timestamp = get_timestamp())
   int mon;            // uint32_t get_month(const timestamp_t timestamp = get_timestamp())
   int day;            // uint32_t get_day_month(const timestamp_t timestamp = get_timestamp())
   int hour;           // uint32_t get_hour_day(const timestamp_t timestamp = get_timestamp())
   int min;            // uint32_t get_minute_hour(const timestamp_t timestamp = get_timestamp())
   int sec;            // uint32_t get_second_minute(const timestamp_t timestamp = get_timestamp())
   int day_of_week;    // uint32_t get_weekday(const timestamp_t timestamp = get_timestamp())
   int day_of_year;    // uint32_t get_day_year(const timestamp_t timestamp = get_timestamp())
  };
GitHub - NewYaroslav/xtime_cpp: Простая C++ библиотека для работы с временем и датой
GitHub - NewYaroslav/xtime_cpp: Простая C++ библиотека для работы с временем и датой
  • NewYaroslav
  • github.com
boost::posix_time, std, boost ::chrono... надо учиться эффективно использовать чужие велосипеды, иначе легко завязнуть в своих... Зачем нужна эта библиотека? Данная библиотека поможет в случаях, когда есть необходимость часто использовать метку времени и искать данные, имеющие взаимосвязь с разными промежутками времени. Например, когда нужно...
 
fxsaber #:

Easy to understand source code.

Nice codes although not the fastest.

Forum on trading, automated trading systems and testing trading strategies

Libraries: Local Timezones and Local Session Hours

amrali, 2024.04.16 18:40

Update 16 April 2024 - version 1.85

Replaced the GetNthSunday internal method with the more optimized GetMonthTime method.


 

Forum on trading, automated trading systems and testing trading strategies

Libraries: Local Timezones and Local Session Hours

amrali, 2024.04.17 00:27

Update 17 April 2024 - version 1.87

Replaced the TimeYear internal method with the more optimised GetYear method .


//+------------------------------------------------------------------+
//| Returns year as integer of the specified date.                   |
//| https://www.mql5.com/ru/forum/170952/page251#comment_53071746    |
//+------------------------------------------------------------------+
int CTimeZoneInfo::GetYear(const datetime time)
  {
   return((int)((time / DAY * 4 + 2) / 1461) + 1970);
  }
 

amrali #:

int CTimeZoneInfo::GetYear(const datetime time)
  {
   return((int)((time / DAY * 4 + 2) / 1461) + 1970);
  }


Genius! Thank you!

 
fxsaber #:

Let's start with the year.

Or the same in integer form.

int GetYear3( const datetime time )
{
  return((int)((time / DAY * 4 + 2) / (365 * 4 + 1)) + 1970);
}

Genius! That was cool. Thank you!

 
amrali #:

Good codes, although not the fastest.

Easy to convert to MQL. For example, finding the year.

#define  FIRST_YEAR_UNIX      1970       // Год начала UNIX времени
#define  SECONDS_IN_4_YEAR    126230400  // Количество секунд за 4 года
#define  SECONDS_IN_YEAR      31536000   // Количество секунд за год
#define  SECONDS_IN_LEAP_YEAR 31622400   // Количество секунд за високосный год

// https://github.com/NewYaroslav/xtime_cpp/blob/master/src/xtime.hpp
int GetYear_XTime( const datetime timestamp )
{
  const int year = FIRST_YEAR_UNIX + 4 * (timestamp / SECONDS_IN_4_YEAR); 
  const datetime t = timestamp % SECONDS_IN_4_YEAR;
  if(t < SECONDS_IN_YEAR) return year;
  else if(t < (2*SECONDS_IN_YEAR)) return year + 1;
  else if(t < (2*SECONDS_IN_YEAR + SECONDS_IN_LEAP_YEAR)) return year + 2;
  return year + 3;  
}

I have not tested the speed.

 
fxsaber #:

Easy to convert to MQL. For example, finding the year.

I have not tested the speed.

faster than GetYear3() but not too much 1.5x

 
fxsaber #:

I once said that it's a good idea to start the year on 1 March instead of 1 January. Then the crooked February becomes the last month.
Then we can apply the following logic to find the month:

int m1 = days/30;
if (m1 == days/31) month = m1; // более 80% случаев
else {...} 

where days - number of days since the last 1 March
I would do it myself, but it's time to sleep.


 
amrali #:

faster than GetYear3(), but not too much 1.5x

Most likely, all the functions there are very fast. You can take it as a basis for writing your own TimeToStruct_Custom. Then it will be much faster than the standard function.