Cyprus DST shifts from 2000 till 2028

29 June 2023, 13:56
Lorentzos Roussos
0
118

If your broker is in Cyprus 

If your brokers server time is in Cyprus's Time Zone

You can use the following structures to turn historical bar times or historical ticks to gmt time 

#property copyright "Source"
#property link      "https://www.mql5.com/en/blogs/post/753335"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
#define TSHIFT_BACK_TO_BASE_TIME -1
#define TSHIFT_DAYLIGHT_SAVINGS_TIME 1

int TimeYear(datetime _time){MqlDateTime mqt;TimeToStruct(_time,mqt);return(mqt.year);}

class time_shift_moment{
         public:
datetime happens_at;
     int shift_in_hours;
         time_shift_moment(void){reset();}
        ~time_shift_moment(void){reset();}
    void reset(){
         happens_at=0;
         shift_in_hours=0;
         }
    void set(datetime _at,int _shift){
         happens_at=_at;
         shift_in_hours=_shift;
         }
};
class time_shift_log{
                  public:
time_shift_moment shifts[];
              int base_gmt_offset,dst_gmt_offset;
                  time_shift_log(void){reset();}
                 ~time_shift_log(void){reset();}
             void reset(){
                  base_gmt_offset=0;
                  dst_gmt_offset=0;
                  ArrayFree(shifts);
                  }
             void setup(int base_offset_in_hours_from_gmt,int dst_offset_in_hours_from_gmt){
                  base_gmt_offset=base_offset_in_hours_from_gmt;
                  dst_gmt_offset=dst_offset_in_hours_from_gmt;
                  }
             void add(datetime _time,int _shift){
                  int ns=ArraySize(shifts)+1;
                  ArrayResize(shifts,ns,0);
                  shifts[ns-1].set(_time,_shift);
                  }
             bool turn_time_to_gmt(datetime _time,datetime &_result){
                  int hours_offset=0;
                  if(find_time_offset(_time,hours_offset)){
                    hours_offset*=60*60;//to seconds
                    _result=(datetime)((int)_time-hours_offset);
                    return(true);
                    }
                  return(false);
                  }
             bool turn_time_to_gmt(long _time_ms,long &_result_ms){
                  datetime totime=(datetime)(_time_ms/1000);
                  if(turn_time_to_gmt(totime,totime)){
                    _result_ms=((long)totime*1000);
                    return(true);
                    }
                  return(false);
                  }
             bool find_time_offset(datetime of_this_date,int &result_hours){
                  //so we are looking for a shift before on the same year
                    int search=find_shift_date_before(of_this_date);
                    if(search!=-1){
                    //if in base 
                      if(shifts[search].shift_in_hours==TSHIFT_BACK_TO_BASE_TIME){
                        result_hours=base_gmt_offset;
                        return(true);
                        }
                      else{
                        result_hours=dst_gmt_offset;
                        return(true);
                        }
                    }
                  return(false);
                  }
              int find_shift_date_before(datetime before_this){
                  //start from the end 
                    for(int i=ArraySize(shifts)-1;i>=0;i--){
                       //if this time is earlier than this time 
                         if(shifts[i].happens_at<=before_this){
                           if(TimeYear(shifts[i].happens_at)==TimeYear(before_this)){
                             return(i);
                             }
                           }
                       }
                  return(-1);
                  }
};

time_shift_log CyprusDST;

void prepare_Cyprus(time_shift_log &shift_log){
shift_log.reset();

shift_log.setup(2,3);

shift_log.add(D'2000.03.26 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2000.10.29 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2001.03.25 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2001.10.28 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2002.03.31 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2002.10.27 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2003.03.30 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2003.10.26 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2004.03.28 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2004.10.31 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2005.03.27 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2005.10.30 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2006.03.26 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2006.10.29 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2007.03.25 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2007.10.28 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2008.03.30 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2008.10.26 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2009.03.29 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2009.10.25 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2010.03.28 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2010.10.31 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2011.03.27 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2011.10.30 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2012.03.25 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2012.10.28 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2013.03.31 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2013.10.27 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2014.03.30 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2014.10.26 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2015.03.29 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2015.10.25 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2016.03.27 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2016.10.30 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2017.03.26 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2017.10.29 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2018.03.25 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2018.10.28 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2019.03.31 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2019.10.27 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2020.03.29 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2020.10.25 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2021.03.28 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2021.10.31 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2022.03.27 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2022.10.30 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2023.03.26 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2023.10.29 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2024.03.31 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2024.10.27 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2025.03.30 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2025.10.26 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2026.03.29 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2026.10.25 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2027.03.28 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2027.10.31 04:00',TSHIFT_BACK_TO_BASE_TIME);

shift_log.add(D'2028.03.26 03:00',TSHIFT_DAYLIGHT_SAVINGS_TIME);
shift_log.add(D'2028.10.29 04:00',TSHIFT_BACK_TO_BASE_TIME);
}

int OnInit()
  {
//---
  //call this oninit !
  prepare_Cyprus(CyprusDST);
  //call this to turn a datetime to gmt 
    datetime past=iTime(_Symbol,_Period,10);
             past=D'01.11.2001 00:00';
    datetime past_to_gmt=0;
    if(CyprusDST.turn_time_to_gmt(past,past_to_gmt)){
      Print(TimeToString(past,TIME_DATE|TIME_MINUTES|TIME_SECONDS)+" -> "+TimeToString(past_to_gmt,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
      }else{
      Print("Error");
      }
    //theres a function for ms too 
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  CyprusDST.reset(); 
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+


Share it with friends: