Time zone differences

 

I have been drilling every where to find a solution to this...

Scenario: I want to have user input that will provide a start time and end time for a range of time. Then get the GMT for both. Next I need to get the server time and get the GMT for this (already done). Finally, I need specify my actual start time and end time for a trade routine to occur within this ramge adjust as an offset of the sever time.

Example:

UserStartHour: 21

UserStartMin: 00

UserEndTime: 00

UserEndHout: 00

UserTimeZone: EST

ServerTime: 08:05 PST

GMT Time: 15:05

// Get GMT of User Start time

HERE IS THE PROBLEM.... I can use the server or locals time values to get the date, all I really need to know is the offset between GMT and user provided information.

// Get GMT of User End time

Again here is the problem, same as other.

I got the server time, I got the GMT time, I got the local time, I just cannot figure out how to get the User specified time.

Here is the code I have so far:

=======================================================

//+------------------------------------------------------------------+
//| Time2Run.mq4 |
//| Copyright © 2009, Investors Haven |
//| Description: |
//| At specific times of the day, the market will run up or down. |
//| This EA ;ets upi tell it when this happens, then evaluates the |
//| time block and seeks out the max and min vales of the price |
//| high/low then waits for the market to break out of this range. |
//| When this range is broken, a trade is opened in that direction |
//| The trade remains open for the same size as double the difference|
//| of range high/low. If trade continues, then trailing stop loss |
//| is set at 10 pips. Trade closes at camdle reverse or TS |
//| or stop loss. Stop loss is set at high/low difference below entry|
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Investors Haven"
#property link ""


#import "kernel32.dll"
void GetLocalTime(int& TimeArray[]);
void GetSystemTime(int& TimeArray[]);
int GetTimeZoneInformation(int& TZInfoArray[]);
#import
#define TIME_ZONE_ID_UNKNOWN 0
#define TIME_ZONE_ID_STANDARD 1
#define TIME_ZONE_ID_DAYLIGHT 2


//---- input parameters
extern int StartHour=21;
extern int StartMinute=00;
extern int EndHour=0;
extern int EndMinute=0;
extern string TimeZone="EST";

//globals
static int oldTime;
static double MaxRange; // This is the highest value during the range
static double MinRange; // This is the lowest value during the range

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

oldTime = Time[0];
MaxRange = -1; // set to low value for comparison
MinRange = 1000000000000; // set to really high value for comparison

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

int TimeArray[4];
int TZInfoArray[43];
int nYear,nMonth,nDay,nHour,nMin,nSec,nMilliSec;
string sMilliSec;

if(oldTime != Time[0]) // Check for new candle
{
oldTime = Time[0]; // Set current time to old time for next check

// +---------------------------------------------------------------------
// All work here
// +---------------------------------------------------------------------




//----
GetSystemTime(TimeArray);

//---- parse date and time from array
nYear=TimeArray[0]&0x0000FFFF;
nMonth=TimeArray[0]>>16;
nDay=TimeArray[1]>>16;
nHour=TimeArray[2]&0x0000FFFF;
nMin=TimeArray[2]>>16;
nSec=TimeArray[3]&0x0000FFFF;


//---- format date and time items
string time_string = FormatDateTime(nYear, nMonth, nDay, nHour, nMin, nSec);
Print("System time is: ",time_string);


//---- format date and time items
string UserTargetStartTime = FormatDateTime(nYear, nMonth, nDay, StartHour, StartMinute, nSec);
Print("User target Start time is: ",UserTargetStartTime);


//---- format date and time items
string UserTargetEndTime = FormatDateTime(nYear, nMonth, nDay, EndHour, EndMinute, nSec);
Print("User target End time is: ",UserTargetEndTime);

//----
GetLocalTime(TimeArray);

//---- parse date and time from array
nYear=TimeArray[0]&0x0000FFFF;
nMonth=TimeArray[0]>>16;
nDay=TimeArray[1]>>16;
nHour=TimeArray[2]&0x0000FFFF;
nMin=TimeArray[2]>>16;
nSec=TimeArray[3]&0x0000FFFF;
nMilliSec=TimeArray[3]>>16;

//---- format date and time items
sMilliSec=1000+nMilliSec;
sMilliSec=StringSubstr(sMilliSec,1);
time_string=FormatDateTime(nYear,nMonth,nDay,nHour,nMin,nSec);
Print("Local time is: ",time_string,":",sMilliSec);


//---- shift with daylight savings
int gmt_shift=0;
int ret=GetTimeZoneInformation(TZInfoArray);
if(ret!=0) gmt_shift=TZInfoArray[0];
Print("Difference between your local time and GMT is: ",(gmt_shift/60)," hours");
if(ret==2) gmt_shift+=TZInfoArray[42];
Print("Current difference between your local time and GMT is: ",(gmt_shift/60)," hours. ", TZInfoArray[42]);

//---- GMT
datetime local_time=StrToTime(time_string);
Print("Greenwich mean time for local is: ",TimeToStr(local_time+gmt_shift*60,TIME_DATE|TIME_MINUTES));

//---- ServerTime difference between GMT Time
datetime dtGmtFromServer = TimeCurrent() - local_time+gmt_shift*60;
Print("GMT from Server: ", TimeToStr(dtGmtFromServer,TIME_DATE|TIME_MINUTES));

//---- GMT
datetime UserS_time=StrToTime(UserTargetStartTime);
Print("Greenwich mean time for User Start time is: ",TimeToStr(UserS_time+gmt_shift*60,TIME_DATE|TIME_MINUTES));

//---- GMT
datetime UserE_time=StrToTime(UserTargetEndTime);
Print("Greenwich mean time for User End is: ",TimeToStr(UserE_time+gmt_shift*60,TIME_DATE|TIME_MINUTES));

//------- Server Time
Print("Server Time: ", TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES));
//---- winter time (nYear remains the current)
nYear=TimeArray[17]&0x0000FFFF;
nMonth=TZInfoArray[17]>>16;
nDay=TZInfoArray[18]>>16;
nHour=TZInfoArray[19]&0x0000FFFF;
nMin=TZInfoArray[19]>>16;
nSec=TZInfoArray[20]&0x0000FFFF;
time_string = FormatDateTime(nYear, nMonth, nDay, nHour, nMin, nSec);
Print("Standard time is: ",time_string);
//---- summer time (nYear remains the current)
nYear=TimeArray[38]&0x0000FFFF;
nMonth=TZInfoArray[38]>>16;
nDay=TZInfoArray[39]>>16;
nHour=TZInfoArray[40]&0x0000FFFF;
nMin=TZInfoArray[40]>>16;
nSec=TZInfoArray[41]&0x0000FFFF;
time_string=FormatDateTime(nYear, nMonth, nDay, nHour, nMin, nSec);
Print("Daylight savings time is: ",time_string);
//----
}
return(0);
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string FormatDateTime(int nYear,int nMonth,int nDay,int nHour,int nMin,int nSec)
{
string sMonth,sDay,sHour,sMin,sSec = "";
//----
sMonth=100+nMonth;
sMonth=StringSubstr(sMonth,1);
sDay=100+nDay;
sDay=StringSubstr(sDay,1);
sHour=100+nHour;
sHour=StringSubstr(sHour,1);
sMin=100+nMin;
sMin=StringSubstr(sMin,1);
sSec=100+nSec;
sSec=StringSubstr(sSec,1);
//----
return(StringConcatenate(nYear,".",sMonth,".",sDay," ",sHour,":",sMin,":",sSec));
}


double TimeZoneServer()
{
int ServerToLocalDiffMinutes = (TimeCurrent()-TimeLocal())/60;
// round to nearest 30 minutes to allow for inaccurate PC clock
int nHalfHourDiff = MathRound(ServerToLocalDiffMinutes/30.0);
ServerToLocalDiffMinutes = nHalfHourDiff*30;
return(TimeZoneLocal() + ServerToLocalDiffMinutes/60.0);
}

double TimeZoneLocal()
{
int TZInfoArray[43];

switch(GetTimeZoneInformation(TZInfoArray))
{
case TIME_ZONE_ID_UNKNOWN:
Print("Error obtaining PC timezone from GetTimeZoneInformation in kernel32.dll. Returning 0");
return(0);
case TIME_ZONE_ID_STANDARD:
return(TZInfoArray[0]/(-60.0));
case TIME_ZONE_ID_DAYLIGHT:
return((TZInfoArray[0]+TZInfoArray[42])/(-60.0));
default:
Print("Unkown return value from GetTimeZoneInformation in kernel32.dll. Returning 0");
return(0);
}
}

//+------------------------------------------------------------------+