Index  Source Files  Annotated Class List  Alphabetical Class List  Class Hierarchy  Graphical Class Hierarchy 

TimeRange.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) quickfixengine.org  All rights reserved.
00003 **
00004 ** This file is part of the QuickFIX FIX Engine
00005 **
00006 ** This file may be distributed under the terms of the quickfixengine.org
00007 ** license as defined by quickfixengine.org and appearing in the file
00008 ** LICENSE included in the packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **
00013 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00014 **
00015 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00016 ** not clear to you.
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   {
00039     if( startDay > 0
00040         && endDay > 0
00041         && startDay == endDay
00042         && endTime > startTime )
00043     { m_endTime = m_startTime; }
00044   }
00045 
00046   TimeRange::TimeRange( const LocalTimeOnly& startTime,
00047                         const LocalTimeOnly& endTime,
00048                         int startDay,
00049                         int endDay )
00050   : m_startTime( startTime ), m_endTime( endTime ),
00051     m_startDay( startDay ), m_endDay( endDay )
00052   {
00053     if( startDay > 0
00054         && endDay > 0
00055         && startDay == endDay
00056         && endTime > startTime )
00057     { m_endTime = m_startTime; }
00058   }
00059 
00060   bool TimeRange::isInRange( const DateTime& start,
00061                                    const DateTime& end,
00062                                    const DateTime& time )
00063   { QF_STACK_PUSH(TimeRange::isInRange)
00064 
00065     UtcTimeOnly timeOnly (time);
00066 
00067     if( start < end )
00068       return( timeOnly >= start && timeOnly <= end );
00069     else
00070       return( timeOnly >= start || timeOnly <= end );
00071 
00072     QF_STACK_POP
00073   }
00074 
00075   bool TimeRange::isInRange( const DateTime& startTime,
00076                                    const DateTime& endTime,
00077                                    int startDay,
00078                                    int endDay,
00079                                    const DateTime& time )
00080   { QF_STACK_PUSH(TimeRange::isInRange)
00081 
00082     int currentDay = time.getWeekDay();
00083     UtcTimeOnly timeOnly (time);
00084 
00085     if( startDay == endDay )
00086     {
00087       if( currentDay != startDay )
00088         return true;
00089       return isInRange( startTime, endTime, time );
00090     }
00091     else if( startDay < endDay )
00092     {
00093       if( currentDay < startDay || currentDay > endDay )
00094         return false;
00095       else if( currentDay == startDay && timeOnly < startTime )
00096         return false;
00097       else if( currentDay == endDay && timeOnly > endTime )
00098         return false;
00099     }
00100     else if( startDay > endDay )
00101     {
00102       if( currentDay < startDay && currentDay > endDay )
00103         return false;
00104       else if( currentDay == startDay && timeOnly < startTime )
00105         return false;
00106       else if( currentDay == endDay && timeOnly > endTime )
00107         return false;
00108     }
00109     return true;
00110     QF_STACK_POP
00111   }
00112 
00113   bool TimeRange::isInSameRange( const DateTime& start,
00114                                    const DateTime& end,
00115                                    const DateTime& time1,
00116                                    const DateTime& time2 )
00117   { QF_STACK_PUSH(TimeRange::isInSameRange)
00118 
00119     if( !isInRange( start, end, time1 ) ) return false;
00120     if( !isInRange( start, end, time2 ) ) return false;
00121 
00122     if( time1 == time2 ) return true;
00123 
00124     if( start < end || start == end )
00125     {
00126       UtcDate time1Date( time1 );
00127       UtcDate time2Date( time2 );
00128  
00129       return time1Date == time2Date;
00130     }
00131     else
00132     {
00133       int sessionLength = DateTime::SECONDS_PER_DAY - (start - end);
00134 
00135       if( time1 > time2 )
00136       {
00137         UtcTimeOnly time2TimeOnly = UtcTimeOnly(time2);
00138                   
00139         long delta = time2TimeOnly - start;
00140         if( delta < 0 )
00141           delta = DateTime::SECONDS_PER_DAY - labs(delta);
00142 
00143         return (time1 - time2) < (sessionLength - delta);
00144       }
00145       else
00146       {
00147         return (time2 - time1) < sessionLength;
00148       }
00149     }
00150         
00151     QF_STACK_POP
00152   }
00153 
00154   bool TimeRange::isInSameRange( const DateTime& startTime,
00155                                  const DateTime& endTime,
00156                                  int startDay,
00157                                  int endDay,
00158                                  const DateTime& time1,
00159                                  const DateTime& time2 )
00160   { QF_STACK_PUSH(TimeRange::isInSameRange)
00161 
00162     if( !isInRange( startTime, endTime, startDay, endDay, time1 ) )
00163       return false;
00164 
00165     if( !isInRange( startTime, endTime, startDay, endDay, time2 ) )
00166       return false;
00167 
00168     int absoluteDay1 = time1.getJulianDate() - time1.getWeekDay();
00169     int absoluteDay2 = time2.getJulianDate() - time2.getWeekDay();
00170     return absoluteDay1 == absoluteDay2;
00171 
00172     QF_STACK_POP
00173   }
00174 }

Generated on Mon Apr 5 20:59:51 2010 for QuickFIX by doxygen 1.6.1 written by Dimitri van Heesch, © 1997-2001