![見出し画像](https://assets.st-note.com/production/uploads/images/117883949/rectangle_large_type_2_df0e83d495670a4c0d60140c88e356b3.jpeg?width=1200)
日本時間を返す(夏時間/冬時間)(制御)
//<<<<<>>>>><<<<<>>>>><< 時間変数 >><<<<<>>>>><<<<<>>>>>
datetime nowtime_jpn;
MqlDateTime nowtime_str;
OnInit()
特になし
OnTick()
nowtime_jpn = getJapanTime();//MQL5の時刻を日本時間に変更(夏/冬時間も計算)
TimeToStruct(nowtime_jpn, nowtime_str);
Print(nowtime_str.hour);//日本時間(時間)を出力
Print(nowtime_str.min);//日本時間(分)を出力
getJapanTime()
//+------------------------------------------------------------------+
//| 日本時間の取得 |
//+------------------------------------------------------------------+
datetime getJapanTime()
{
datetime present = TimeCurrent();
datetime summer = present + 21600; //業者により変更が必要
datetime winter = present + 25200; //業者により変更が必要
MqlDateTime present_str;
TimeToStruct(present, present_str);
switch(present_str.year)
{
case 2000:
if(StringToTime("2000.3.12")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2000.11.5"))
return summer;
break;
case 2001:
if(StringToTime("2001.3.11")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2001.11.4"))
return summer;
break;
case 2002:
if(StringToTime("2002.3.10")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2002.11.3"))
return summer;
break;
case 2003:
if(StringToTime("2003.3.9") <=TimeCurrent()&&TimeCurrent()<=StringToTime("2003.11.2"))
return summer;
break;
case 2004:
if(StringToTime("2004.3.14")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2004.11.7"))
return summer;
break;
case 2005:
if(StringToTime("2005.3.13")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2005.11.6"))
return summer;
break;
case 2006:
if(StringToTime("2006.3.12")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2006.11.5"))
return summer;
break;
case 2007:
if(StringToTime("2007.3.11")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2007.11.4"))
return summer;
break;
case 2008:
if(StringToTime("2008.3.9") <=TimeCurrent()&&TimeCurrent()<=StringToTime("2008.11.2"))
return summer;
break;
case 2009:
if(StringToTime("2009.3.8") <=TimeCurrent()&&TimeCurrent()<=StringToTime("2009.11.1"))
return summer;
break;
case 2010:
if(StringToTime("2010.3.14")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2010.11.7"))
return summer;
break;
case 2011:
if(StringToTime("2011.3.13")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2011.11.6"))
return summer;
break;
case 2012:
if(StringToTime("2012.3.11")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2012.11.4"))
return summer;
break;
case 2013:
if(StringToTime("2013.3.10")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2013.11.3"))
return summer;
break;
case 2014:
if(StringToTime("2014.3.9") <=TimeCurrent()&&TimeCurrent()<=StringToTime("2014.11.2"))
return summer;
break;
case 2015:
if(StringToTime("2015.3.8") <=TimeCurrent()&&TimeCurrent()<=StringToTime("2015.11.1"))
return summer;
break;
case 2016:
if(StringToTime("2016.3.13")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2016.11.6"))
return summer;
break;
case 2017:
if(StringToTime("2017.3.12")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2017.11.5"))
return summer;
break;
case 2018:
if(StringToTime("2018.3.11")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2018.11.4"))
return summer;
break;
case 2019:
if(StringToTime("2019.3.10")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2019.11.3"))
return summer;
break;
case 2020:
if(StringToTime("2020.3.8") <=TimeCurrent()&&TimeCurrent()<=StringToTime("2020.11.1"))
return summer;
break;
case 2021:
if(StringToTime("2021.3.14")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2021.11.7"))
return summer;
break;
case 2022:
if(StringToTime("2022.3.13")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2022.11.6"))
return summer;
break;
case 2023:
if(StringToTime("2023.3.12")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2023.11.5"))
return summer;
break;
case 2024:
if(StringToTime("2024.3.10")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2024.11.3"))
return summer;
break;
case 2025:
if(StringToTime("2025.3.9") <=TimeCurrent()&&TimeCurrent()<=StringToTime("2025.11.2"))
return summer;
break;
case 2026:
if(StringToTime("2026.3.8") <=TimeCurrent()&&TimeCurrent()<=StringToTime("2026.11.1"))
return summer;
break;
case 2027:
if(StringToTime("2027.3.14")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2027.11.7"))
return summer;
break;
case 2028:
if(StringToTime("2028.3.12")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2028.11.5"))
return summer;
break;
case 2029:
if(StringToTime("2029.3.11")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2029.11.4"))
return summer;
break;
case 2030:
if(StringToTime("2030.3.10")<=TimeCurrent()&&TimeCurrent()<=StringToTime("2030.11.3"))
return summer;
break;
}
return winter;
}
処理のイメージ
①getJapanTime()は、指定した日時でsummer、それ以外ではwinterを返すようにしてある
②以下で、nowtime_jpnにgetJapanTime()の時間を格納(1)した後、
nowtime_strに変換(2)
(1)nowtime_jpn = getJapanTime();
(2)TimeToStruct(nowtime_jpn, nowtime_str);
以降、nowtime_str.XXXで日本時間を呼び出すことが可能
![](https://assets.st-note.com/img/1696211552189-wCiKd3noeg.png)
MQL5コード一覧はコチラ
この記事が気に入ったらサポートをしてみませんか?