TimeRange.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 #include "CallStack.h"
00026
00027 #include "TimeRange.h"
00028 #include "Utility.h"
00029
00030 namespace FIX
00031 {
00032 TimeRange::TimeRange( const UtcTimeOnly& startTime,
00033 const UtcTimeOnly& endTime,
00034 int startDay,
00035 int endDay )
00036 : m_startTime( startTime ), m_endTime( endTime ),
00037 m_startDay( startDay ), m_endDay( endDay ),
00038 m_useLocalTime( false )
00039 {
00040 if( startDay > 0
00041 && endDay > 0
00042 && startDay == endDay
00043 && endTime > startTime )
00044 { m_endTime = m_startTime; }
00045 }
00046
00047 TimeRange::TimeRange( const LocalTimeOnly& startTime,
00048 const LocalTimeOnly& endTime,
00049 int startDay,
00050 int endDay )
00051 : m_startTime( startTime ), m_endTime( endTime ),
00052 m_startDay( startDay ), m_endDay( endDay ),
00053 m_useLocalTime( true )
00054 {
00055 if( startDay > 0
00056 && endDay > 0
00057 && startDay == endDay
00058 && endTime > startTime )
00059 { m_endTime = m_startTime; }
00060 }
00061
00062 bool TimeRange::isInRange( const DateTime& start,
00063 const DateTime& end,
00064 const DateTime& time )
00065 { QF_STACK_PUSH(TimeRange::isInRange)
00066
00067 UtcTimeOnly timeOnly (time);
00068
00069 if( start < end )
00070 return( timeOnly >= start && timeOnly <= end );
00071 else
00072 return( timeOnly >= start || timeOnly <= end );
00073
00074 QF_STACK_POP
00075 }
00076
00077 bool TimeRange::isInRange( const DateTime& startTime,
00078 const DateTime& endTime,
00079 int startDay,
00080 int endDay,
00081 const DateTime& time )
00082 { QF_STACK_PUSH(TimeRange::isInRange)
00083
00084 int currentDay = time.getWeekDay();
00085 UtcTimeOnly timeOnly (time);
00086
00087 if( startDay == endDay )
00088 {
00089 if( currentDay != startDay )
00090 return true;
00091 return isInRange( startTime, endTime, time );
00092 }
00093 else if( startDay < endDay )
00094 {
00095 if( currentDay < startDay || currentDay > endDay )
00096 return false;
00097 else if( currentDay == startDay && timeOnly < startTime )
00098 return false;
00099 else if( currentDay == endDay && timeOnly > endTime )
00100 return false;
00101 }
00102 else if( startDay > endDay )
00103 {
00104 if( currentDay < startDay && currentDay > endDay )
00105 return false;
00106 else if( currentDay == startDay && timeOnly < startTime )
00107 return false;
00108 else if( currentDay == endDay && timeOnly > endTime )
00109 return false;
00110 }
00111 return true;
00112 QF_STACK_POP
00113 }
00114
00115 bool TimeRange::isInSameRange( const DateTime& start,
00116 const DateTime& end,
00117 const DateTime& time1,
00118 const DateTime& time2 )
00119 { QF_STACK_PUSH(TimeRange::isInSameRange)
00120
00121 if( !isInRange( start, end, time1 ) ) return false;
00122 if( !isInRange( start, end, time2 ) ) return false;
00123
00124 if( time1 == time2 ) return true;
00125
00126 if( start < end || start == end )
00127 {
00128 UtcDate time1Date( time1 );
00129 UtcDate time2Date( time2 );
00130
00131 return time1Date == time2Date;
00132 }
00133 else
00134 {
00135 int sessionLength = DateTime::SECONDS_PER_DAY - (start - end);
00136
00137 if( time1 > time2 )
00138 {
00139 UtcTimeOnly time2TimeOnly = UtcTimeOnly(time2);
00140
00141 long delta = time2TimeOnly - start;
00142 if( delta < 0 )
00143 delta = DateTime::SECONDS_PER_DAY - labs(delta);
00144
00145 return (time1 - time2) < (sessionLength - delta);
00146 }
00147 else
00148 {
00149 return (time2 - time1) < sessionLength;
00150 }
00151 }
00152
00153 QF_STACK_POP
00154 }
00155
00156 bool TimeRange::isInSameRange( const DateTime& startTime,
00157 const DateTime& endTime,
00158 int startDay,
00159 int endDay,
00160 const DateTime& time1,
00161 const DateTime& time2 )
00162 { QF_STACK_PUSH(TimeRange::isInSameRange)
00163
00164 if( !isInRange( startTime, endTime, startDay, endDay, time1 ) )
00165 return false;
00166
00167 if( !isInRange( startTime, endTime, startDay, endDay, time2 ) )
00168 return false;
00169
00170 int absoluteDay1 = time1.getJulianDate() - time1.getWeekDay();
00171 int absoluteDay2 = time2.getJulianDate() - time2.getWeekDay();
00172 return absoluteDay1 == absoluteDay2;
00173
00174 QF_STACK_POP
00175 }
00176 }