TimeRange.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* -*- C++ -*- */
  2. /****************************************************************************
  3. ** Copyright (c) 2001-2014
  4. **
  5. ** This file is part of the QuickFIX FIX Engine
  6. **
  7. ** This file may be distributed under the terms of the quickfixengine.org
  8. ** license as defined by quickfixengine.org and appearing in the file
  9. ** LICENSE included in the packaging of this file.
  10. **
  11. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  12. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. **
  14. ** See http://www.quickfixengine.org/LICENSE for licensing information.
  15. **
  16. ** Contact ask@quickfixengine.org if any conditions of this licensing are
  17. ** not clear to you.
  18. **
  19. ****************************************************************************/
  20. #ifndef FIX_TIMERANGE_H
  21. #define FIX_TIMERANGE_H
  22. #ifdef _MSC_VER
  23. #pragma warning( disable : 4503 4355 4786 4290 )
  24. #endif
  25. #include "FieldTypes.h"
  26. namespace FIX
  27. {
  28. /// Keeps track of when session is active
  29. class TimeRange
  30. {
  31. public:
  32. TimeRange( const UtcTimeOnly& startTime, const UtcTimeOnly& endTime,
  33. int startDay = -1, int endDay = -1 );
  34. TimeRange( const LocalTimeOnly& startTime, const LocalTimeOnly& endTime,
  35. int startDay = -1, int endDay = -1 );
  36. static bool isInRange( const UtcTimeOnly& start,
  37. const UtcTimeOnly& end,
  38. const DateTime& time )
  39. {
  40. return isInRange
  41. ( (DateTime)start, (DateTime)end, (DateTime)time );
  42. }
  43. static bool isInRange( const UtcTimeOnly& startTime,
  44. const UtcTimeOnly& endTime,
  45. int startDay,
  46. int endDay,
  47. const DateTime& time,
  48. int day )
  49. {
  50. return isInRange
  51. ( (DateTime)startTime, (DateTime)endTime,
  52. startDay, endDay,
  53. (DateTime)time, day );
  54. }
  55. static bool isInRange( const UtcTimeOnly& startTime,
  56. const UtcTimeOnly& endTime,
  57. int startDay,
  58. int endDay,
  59. const DateTime& time )
  60. {
  61. return isInRange
  62. ( (DateTime)startTime, (DateTime)endTime,
  63. startDay, endDay,
  64. (DateTime)time );
  65. }
  66. static bool isInSameRange( const UtcTimeOnly& start,
  67. const UtcTimeOnly& end,
  68. const DateTime& time1,
  69. const DateTime& time2 )
  70. {
  71. return isInSameRange
  72. ( (DateTime)start, (DateTime)end,
  73. (DateTime)time1, (DateTime)time2 );
  74. }
  75. static bool isInSameRange( const UtcTimeOnly& startTime,
  76. const UtcTimeOnly& endTime,
  77. int startDay,
  78. int endDay,
  79. const DateTime& time1,
  80. const DateTime& time2 )
  81. {
  82. return isInSameRange
  83. ( (DateTime)startTime, (DateTime)endTime,
  84. startDay, endDay,
  85. (DateTime)time1, (DateTime)time2 );
  86. }
  87. static bool isInRange( const LocalTimeOnly& start,
  88. const LocalTimeOnly& end,
  89. const DateTime& time )
  90. {
  91. return isInRange
  92. ( (DateTime)start, (DateTime)end, (DateTime)time );
  93. }
  94. static bool isInRange( const LocalTimeOnly& startTime,
  95. const LocalTimeOnly& endTime,
  96. int startDay,
  97. int endDay,
  98. const DateTime& time,
  99. int day )
  100. {
  101. return isInRange
  102. ( (DateTime)startTime, (DateTime)endTime,
  103. startDay, endDay,
  104. (DateTime)time, day );
  105. }
  106. static bool isInRange( const LocalTimeOnly& startTime,
  107. const LocalTimeOnly& endTime,
  108. int startDay,
  109. int endDay,
  110. const DateTime& time )
  111. {
  112. return isInRange
  113. ( (DateTime)startTime, (DateTime)endTime,
  114. startDay, endDay,
  115. (DateTime)time );
  116. }
  117. static bool isInSameRange( const LocalTimeOnly& start,
  118. const LocalTimeOnly& end,
  119. const DateTime& time1,
  120. const DateTime& time2 )
  121. {
  122. return isInSameRange
  123. ( (DateTime)start, (DateTime)end,
  124. (DateTime)time1, (DateTime)time2 );
  125. }
  126. static bool isInSameRange( const LocalTimeOnly& startTime,
  127. const LocalTimeOnly& endTime,
  128. int startDay,
  129. int endDay,
  130. const DateTime& time1,
  131. const DateTime& time2 )
  132. {
  133. return isInSameRange
  134. ( (DateTime)startTime, (DateTime)endTime,
  135. startDay, endDay,
  136. (DateTime)time1, (DateTime)time2 );
  137. }
  138. private:
  139. static bool isInRange( const DateTime& start,
  140. const DateTime& end,
  141. const DateTime& time );
  142. static bool isInRange( const DateTime& startTime,
  143. const DateTime& endTime,
  144. int startDay,
  145. int endDay,
  146. const DateTime& time,
  147. int day );
  148. static bool isInRange( const DateTime& startTime,
  149. const DateTime& endTime,
  150. int startDay,
  151. int endDay,
  152. const DateTime& time );
  153. static bool isInSameRange( const DateTime& start,
  154. const DateTime& end,
  155. const DateTime& time1,
  156. const DateTime& time2 );
  157. static bool isInSameRange( const DateTime& startTime,
  158. const DateTime& endTime,
  159. int startDay,
  160. int endDay,
  161. const DateTime& time1,
  162. const DateTime& time2 );
  163. public:
  164. bool isInRange( const DateTime& dateTime, int day )
  165. {
  166. if( m_startDay < 0 && m_endDay < 0 )
  167. return isInRange( m_startTime, m_endTime, dateTime );
  168. else
  169. return isInRange
  170. ( m_startTime, m_endTime, m_startDay, m_endDay, dateTime, day );
  171. }
  172. bool isInRange( const UtcTimeStamp& dateTime )
  173. {
  174. if( m_useLocalTime )
  175. {
  176. LocalTimeStamp localDateTime( dateTime.getTimeT() );
  177. return isInRange( localDateTime, localDateTime.getWeekDay() );
  178. }
  179. return isInRange( dateTime, dateTime.getWeekDay() );
  180. }
  181. bool isInRange( const LocalTimeStamp& dateTime )
  182. {
  183. if( !m_useLocalTime )
  184. {
  185. LocalTimeStamp utcDateTime( dateTime.getTimeT() );
  186. return isInRange( utcDateTime, utcDateTime.getWeekDay() );
  187. }
  188. return isInRange( dateTime, dateTime.getWeekDay() );
  189. }
  190. bool isInSameRange( const UtcTimeStamp& time1, const UtcTimeStamp& time2 )
  191. {
  192. if( m_useLocalTime )
  193. {
  194. LocalTimeStamp localTime1( time1.getTimeT() );
  195. LocalTimeStamp localTime2( time2.getTimeT() );
  196. return isInSameRange( (DateTime)localTime1, (DateTime)localTime2 );
  197. }
  198. return isInSameRange( (DateTime)time1, (DateTime)time2 );
  199. }
  200. bool isInSameRange( const LocalTimeStamp& time1, const LocalTimeStamp& time2 )
  201. {
  202. if( !m_useLocalTime )
  203. {
  204. UtcTimeStamp utcTime1( time1.getTimeT() );
  205. UtcTimeStamp utcTime2( time2.getTimeT() );
  206. return isInSameRange( (DateTime)utcTime1, (DateTime)utcTime2 );
  207. }
  208. return isInSameRange( (DateTime)time1, (DateTime)time2 );
  209. }
  210. private:
  211. bool isInSameRange( const DateTime& time1, const DateTime& time2 )
  212. {
  213. if( m_startDay < 0 && m_endDay < 0 )
  214. return isInSameRange( m_startTime, m_endTime, time1, time2 );
  215. else
  216. return isInSameRange
  217. ( m_startTime, m_endTime, m_startDay, m_endDay, time1, time2 );
  218. }
  219. UtcTimeOnly m_startTime;
  220. UtcTimeOnly m_endTime;
  221. int m_startDay;
  222. int m_endDay;
  223. bool m_useLocalTime;
  224. };
  225. }
  226. #endif