MT5APITime.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //+------------------------------------------------------------------+
  2. //| MetaTrader 5 API |
  3. //| Copyright 2000-2019, MetaQuotes Software Corp. |
  4. //| http://www.metaquotes.net |
  5. //+------------------------------------------------------------------+
  6. #pragma once
  7. #include <sys/types.h>
  8. #include <sys/timeb.h>
  9. //+------------------------------------------------------------------+
  10. //| Seconds macro |
  11. //+------------------------------------------------------------------+
  12. #define SECONDS_IN_MINUTE INT64(60)
  13. #define SECONDS_IN_HOUR INT64(60*SECONDS_IN_MINUTE)
  14. #define SECONDS_IN_DAY INT64(24*SECONDS_IN_HOUR)
  15. #define SECONDS_IN_WEEK INT64(7*SECONDS_IN_DAY)
  16. #define SECONDS_IN_MONTH INT64(30*SECONDS_IN_DAY)
  17. //+------------------------------------------------------------------+
  18. //| Minutes macro |
  19. //+------------------------------------------------------------------+
  20. #define MINUTES_IN_HOUR (60)
  21. #define MINUTES_IN_DAY (24*MINUTES_IN_HOUR)
  22. #define MINUTES_IN_WEEK (7*MINUTES_IN_DAY)
  23. //+------------------------------------------------------------------+
  24. //| Time management functions |
  25. //+------------------------------------------------------------------+
  26. class SMTTime
  27. {
  28. private:
  29. static const INT64 s_max_time64_t;
  30. static const LPCWSTR s_months_names[13];
  31. static const LPCWSTR s_months_short_names[13];
  32. public:
  33. //--- time functions
  34. static bool ParseTime(const INT64 ctm,tm *ttm);
  35. static INT64 MakeTime(tm *ttm);
  36. static LPCWSTR MonthName(const UCHAR month);
  37. static LPCWSTR MonthNameShort(const UCHAR month);
  38. static INT64 WeekBegin(const INT64 ctm);
  39. static INT64 DayBegin(const INT64 ctm);
  40. static INT64 MonthBegin(const INT64 ctm);
  41. static INT64 YearBegin(const INT64 ctm);
  42. //--- SYSTEMTIME
  43. static INT64 STToTime(const SYSTEMTIME &st);
  44. static SYSTEMTIME& TimeToST(INT64 ctm,SYSTEMTIME& st);
  45. //--- year, month & time hour
  46. static UINT Year(const INT64 ctm);
  47. static UINT Month(const INT64 ctm);
  48. static UINT Day(const INT64 ctm);
  49. static UINT Hour(const INT64 ctm);
  50. static UINT Min(const INT64 ctm);
  51. static UINT Sec(const INT64 ctm);
  52. };
  53. //+------------------------------------------------------------------+
  54. //| |
  55. //+------------------------------------------------------------------+
  56. const __declspec(selectany) INT64 SMTTime::s_max_time64_t=0x793406fffi64;
  57. //+------------------------------------------------------------------+
  58. //| |
  59. //+------------------------------------------------------------------+
  60. const __declspec(selectany)LPCWSTR SMTTime::s_months_names[13] ={ L"January",L"February",L"March",L"April",L"May",L"June",L"July",L"August",L"September",L"October",L"November",L"December",L"Unknown" };
  61. //+------------------------------------------------------------------+
  62. //| |
  63. //+------------------------------------------------------------------+
  64. const __declspec(selectany)LPCWSTR SMTTime::s_months_short_names[13]={ L"Jan",L"Feb",L"Mar",L"Apr",L"May",L"Jun",L"Jul",L"Aug",L"Sep",L"Oct",L"Nov",L"Dec",L"Unk" };
  65. //+------------------------------------------------------------------+
  66. //| Time parsing |
  67. //+------------------------------------------------------------------+
  68. inline bool SMTTime::ParseTime(const INT64 ctm,tm *ttm)
  69. {
  70. //--- check
  71. if(!ttm) return(false);
  72. //--- check time
  73. if(ctm<0 || ctm>=s_max_time64_t)
  74. {
  75. ZeroMemory(ttm,sizeof(*ttm));
  76. return(false);
  77. }
  78. //--- parse
  79. return(_gmtime64_s(ttm,&ctm)==0);
  80. }
  81. //+------------------------------------------------------------------+
  82. //| Time conversion |
  83. //+------------------------------------------------------------------+
  84. inline INT64 SMTTime::MakeTime(tm *ttm)
  85. {
  86. //--- check
  87. if(!ttm) return(0);
  88. //--- make time
  89. return(_mkgmtime64(ttm));
  90. }
  91. //+------------------------------------------------------------------+
  92. //| Month full name |
  93. //+------------------------------------------------------------------+
  94. inline LPCWSTR SMTTime::MonthName(const UCHAR month)
  95. {
  96. if(month>11) return s_months_names[12];
  97. else return s_months_names[month];
  98. }
  99. //+------------------------------------------------------------------+
  100. //| Month short name |
  101. //+------------------------------------------------------------------+
  102. inline LPCWSTR SMTTime::MonthNameShort(const UCHAR month)
  103. {
  104. if(month>11) return s_months_short_names[12];
  105. else return s_months_short_names[month];
  106. }
  107. //+------------------------------------------------------------------+
  108. //| Week begin calculation (Sunday) |
  109. //+------------------------------------------------------------------+
  110. inline INT64 SMTTime::WeekBegin(const INT64 ctm)
  111. {
  112. if(ctm<345600) return(0);
  113. INT64 wday=(ctm%604800);
  114. wday+=(wday>=259200)?(-259200):(345600);
  115. return(ctm-wday);
  116. }
  117. //+------------------------------------------------------------------+
  118. //| Day begin calculation |
  119. //+------------------------------------------------------------------+
  120. inline INT64 SMTTime::DayBegin(const INT64 ctm)
  121. {
  122. return((ctm/SECONDS_IN_DAY)*SECONDS_IN_DAY);
  123. }
  124. //+------------------------------------------------------------------+
  125. //| Month begin calculation |
  126. //+------------------------------------------------------------------+
  127. inline INT64 SMTTime::MonthBegin(const INT64 ctm)
  128. {
  129. tm ttm;
  130. //--- parse datetime
  131. ParseTime(ctm,&ttm);
  132. //---
  133. ttm.tm_mday=1;
  134. ttm.tm_hour=ttm.tm_min=ttm.tm_sec=0;
  135. //--- calc time
  136. return(_mkgmtime64(&ttm));
  137. }
  138. //+------------------------------------------------------------------+
  139. //| Year begin calculation |
  140. //+------------------------------------------------------------------+
  141. inline INT64 SMTTime::YearBegin(const INT64 ctm)
  142. {
  143. tm ttm;
  144. //--- parse datetime
  145. ParseTime(ctm,&ttm);
  146. //---
  147. ttm.tm_mday=1;
  148. ttm.tm_mon =0;
  149. ttm.tm_hour=ttm.tm_min=ttm.tm_sec=0;
  150. //--- calc time
  151. return(_mkgmtime64(&ttm));
  152. }
  153. //+------------------------------------------------------------------+
  154. //| UNIX time to SYSTEMTIME conversion |
  155. //+------------------------------------------------------------------+
  156. inline SYSTEMTIME& SMTTime::TimeToST(INT64 ctm,SYSTEMTIME& st)
  157. {
  158. tm ttm={};
  159. //--- clean
  160. ZeroMemory(&st,sizeof(st));
  161. //--- convert
  162. if(SMTTime::ParseTime(ctm,&ttm))
  163. {
  164. st.wYear =WORD(1900+ttm.tm_year);
  165. st.wMonth =WORD(1+ttm.tm_mon);
  166. st.wDayOfWeek=WORD(ttm.tm_wday);
  167. st.wDay =WORD(ttm.tm_mday);
  168. st.wHour =WORD(ttm.tm_hour);
  169. st.wMinute =WORD(ttm.tm_min);
  170. st.wSecond =WORD(ttm.tm_sec);
  171. }
  172. //---
  173. return(st);
  174. }
  175. //+------------------------------------------------------------------+
  176. //| SYSTEMTIME to UNIX time conversion |
  177. //+------------------------------------------------------------------+
  178. inline INT64 SMTTime::STToTime(const SYSTEMTIME &st)
  179. {
  180. INT64 ctm;
  181. tm ttm={};
  182. //---
  183. ttm.tm_year=st.wYear-1900;
  184. ttm.tm_mon =st.wMonth-1;
  185. ttm.tm_mday=st.wDay;
  186. ttm.tm_wday=st.wDayOfWeek;
  187. ttm.tm_hour=st.wHour;
  188. ttm.tm_min =st.wMinute;
  189. ttm.tm_sec =st.wSecond;
  190. ttm.tm_isdst=0;
  191. //---
  192. if((ctm=_mkgmtime64(&ttm))<0) ctm=0;
  193. return(ctm);
  194. }
  195. //+------------------------------------------------------------------+
  196. //| Year by datetime |
  197. //+------------------------------------------------------------------+
  198. inline UINT SMTTime::Year(const INT64 ctm) { tm ttm; ParseTime(ctm,&ttm); return(UINT(ttm.tm_year+1900)); }
  199. //+------------------------------------------------------------------+
  200. //| Month by datetime |
  201. //+------------------------------------------------------------------+
  202. inline UINT SMTTime::Month(const INT64 ctm){ tm ttm; ParseTime(ctm,&ttm); return(UINT(ttm.tm_mon+1)); }
  203. //+------------------------------------------------------------------+
  204. //| Day by datetime |
  205. //+------------------------------------------------------------------+
  206. inline UINT SMTTime::Day(const INT64 ctm) { tm ttm; ParseTime(ctm,&ttm); return(UINT(ttm.tm_mday)); }
  207. //+------------------------------------------------------------------+
  208. //| Hour by datetime |
  209. //+------------------------------------------------------------------+
  210. inline UINT SMTTime::Hour(const INT64 ctm) { tm ttm; ParseTime(ctm,&ttm); return(UINT(ttm.tm_hour)); }
  211. //+------------------------------------------------------------------+
  212. //| Minute by datetime |
  213. //+------------------------------------------------------------------+
  214. inline UINT SMTTime::Min(const INT64 ctm) { tm ttm; ParseTime(ctm,&ttm); return(UINT(ttm.tm_min)); }
  215. //+------------------------------------------------------------------+
  216. //| Second by datetime |
  217. //+------------------------------------------------------------------+
  218. inline UINT SMTTime::Sec(const INT64 ctm) { tm ttm; ParseTime(ctm,&ttm); return(UINT(ttm.tm_sec)); }
  219. //+------------------------------------------------------------------+