TimeRange.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (c) 2001-2014
3 **
4 ** This file is part of the QuickFIX FIX Engine
5 **
6 ** This file may be distributed under the terms of the quickfixengine.org
7 ** license as defined by quickfixengine.org and appearing in the file
8 ** LICENSE included in the packaging of this file.
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 **
13 ** See http://www.quickfixengine.org/LICENSE for licensing information.
14 **
15 ** Contact ask@quickfixengine.org if any conditions of this licensing are
16 ** not clear to you.
17 **
18 ****************************************************************************/
19 
20 #ifdef _MSC_VER
21 #include "stdafx.h"
22 #else
23 #include "config.h"
24 #endif
25 
26 #include "TimeRange.h"
27 #include "Utility.h"
28 
29 namespace FIX
30 {
31  TimeRange::TimeRange( const UtcTimeOnly& startTime,
32  const UtcTimeOnly& endTime,
33  int startDay,
34  int endDay )
35  : m_startTime( startTime ), m_endTime( endTime ),
36  m_startDay( startDay ), m_endDay( endDay ),
37  m_useLocalTime( false )
38  {
39  if( startDay > 0
40  && endDay > 0
41  && startDay == endDay
42  && endTime > startTime )
43  { m_endTime = m_startTime; }
44  }
45 
46  TimeRange::TimeRange( const LocalTimeOnly& startTime,
47  const LocalTimeOnly& endTime,
48  int startDay,
49  int endDay )
50  : m_startTime( startTime ), m_endTime( endTime ),
51  m_startDay( startDay ), m_endDay( endDay ),
52  m_useLocalTime( true )
53  {
54  if( startDay > 0
55  && endDay > 0
56  && startDay == endDay
57  && endTime > startTime )
58  { m_endTime = m_startTime; }
59  }
60 
61  bool TimeRange::isInRange( const DateTime& start,
62  const DateTime& end,
63  const DateTime& time )
64  {
65  UtcTimeOnly timeOnly (time);
66 
67  if( start < end )
68  return( timeOnly >= start && timeOnly <= end );
69  else
70  return( timeOnly >= start || timeOnly <= end );
71  }
72 
73  bool TimeRange::isInRange( const DateTime& startTime,
74  const DateTime& endTime,
75  int startDay,
76  int endDay,
77  const DateTime& time,
78  int day )
79  {
80  UtcTimeOnly timeOnly (time);
81 
82  if( startDay == endDay )
83  {
84  if( day != startDay )
85  return true;
86  return isInRange( startTime, endTime, time );
87  }
88  else if( startDay < endDay )
89  {
90  if( day < startDay || day > endDay )
91  return false;
92  else if( day == startDay && timeOnly < startTime )
93  return false;
94  else if( day == endDay && timeOnly > endTime )
95  return false;
96  }
97  else if( startDay > endDay )
98  {
99  if( day < startDay && day > endDay )
100  return false;
101  else if( day == startDay && timeOnly < startTime )
102  return false;
103  else if( day == endDay && timeOnly > endTime )
104  return false;
105  }
106  return true;
107  }
108 
109  bool TimeRange::isInRange( const DateTime& startTime,
110  const DateTime& endTime,
111  int startDay,
112  int endDay,
113  const DateTime& time )
114  {
115  return isInRange( startTime, endTime, startDay, endDay, time, time.getWeekDay() );
116  }
117 
118  bool TimeRange::isInSameRange( const DateTime& start,
119  const DateTime& end,
120  const DateTime& time1,
121  const DateTime& time2 )
122  {
123  if( !isInRange( start, end, time1 ) ) return false;
124  if( !isInRange( start, end, time2 ) ) return false;
125 
126  if( time1 == time2 ) return true;
127 
128  if( start < end || start == end )
129  {
130  UtcDate time1Date( time1 );
131  UtcDate time2Date( time2 );
132 
133  return time1Date == time2Date;
134  }
135  else
136  {
137  int sessionLength = DateTime::SECONDS_PER_DAY - (start - end);
138 
139  if( time1 > time2 )
140  {
141  UtcTimeOnly time2TimeOnly = UtcTimeOnly(time2);
142 
143  long delta = time2TimeOnly - start;
144  if( delta < 0 )
145  delta = DateTime::SECONDS_PER_DAY - labs(delta);
146 
147  return (time1 - time2) < (sessionLength - delta);
148  }
149  else
150  {
151  return (time2 - time1) < sessionLength;
152  }
153  }
154  }
155 
156  bool TimeRange::isInSameRange( const DateTime& startTime,
157  const DateTime& endTime,
158  int startDay,
159  int endDay,
160  const DateTime& time1,
161  const DateTime& time2 )
162  {
163  if( !isInRange( startTime, endTime, startDay, endDay, time1, time1.getWeekDay() ) )
164  return false;
165 
166  if( !isInRange( startTime, endTime, startDay, endDay, time2, time2.getWeekDay() ) )
167  return false;
168 
169  int absoluteDay1 = time1.getJulianDate() - time1.getWeekDay();
170  int absoluteDay2 = time2.getJulianDate() - time2.getWeekDay();
171  return absoluteDay1 == absoluteDay2;
172  }
173 }
FIX::DateTime
Definition: FieldTypes.h:77
FIX::TimeRange::m_startTime
UtcTimeOnly m_startTime
Definition: TimeRange.h:284
FIX::TimeRange::TimeRange
TimeRange(const UtcTimeOnly &startTime, const UtcTimeOnly &endTime, int startDay=-1, int endDay=-1)
Definition: TimeRange.cpp:48
FIX::TYPE::UtcTimeOnly
@ UtcTimeOnly
Definition: FieldTypes.h:950
FIX::TimeRange::m_endTime
UtcTimeOnly m_endTime
Definition: TimeRange.h:285
FIX::TimeRange::isInSameRange
static bool isInSameRange(const UtcTimeOnly &start, const UtcTimeOnly &end, const DateTime &time1, const DateTime &time2)
Definition: TimeRange.h:110
FIX
Definition: Acceptor.cpp:34
FIX::TimeRange::isInRange
static bool isInRange(const UtcTimeOnly &start, const UtcTimeOnly &end, const DateTime &time)
Definition: TimeRange.h:77
FIX::UtcDate
Date only represented in UTC.
Definition: FieldTypes.h:817
FIX::DateTime::SECONDS_PER_DAY
static const int64_t SECONDS_PER_DAY
Magic numbers.
Definition: FieldTypes.h:83
TimeRange.h
FIX::UtcTimeOnly
Time only represented in UTC.
Definition: FieldTypes.h:701
Utility.h

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001