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

SessionTime.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 "SessionTime.h"
00028 #include "Utility.h"
00029 
00030 namespace FIX
00031 {
00032   SessionTime::SessionTime( 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   }
00040 
00041   bool SessionTime::isSessionTime( const UtcTimeOnly& start,
00042                                    const UtcTimeOnly& end,
00043                                    const UtcTimeStamp& time )
00044   { QF_STACK_PUSH(SessionTime::isSessionTime)
00045 
00046     UtcTimeOnly timeOnly (time);
00047 
00048     if( start < end )
00049       return( timeOnly >= start && timeOnly <= end );
00050     else
00051       return( timeOnly >= start || timeOnly <= end );
00052 
00053     QF_STACK_POP
00054   }
00055 
00056   bool SessionTime::isSessionTime( const UtcTimeOnly& startTime,
00057                                    const UtcTimeOnly& endTime,
00058                                    int startDay,
00059                                    int endDay,
00060                                    const UtcTimeStamp& time )
00061   { QF_STACK_PUSH(SessionTime::isSessionTime)
00062 
00063     int currentDay = time.getWeekDay();
00064     UtcTimeOnly timeOnly (time);
00065 
00066     if (startDay == endDay)
00067     {
00068         if (currentDay == startDay)
00069         {
00070             return isSessionTime(startTime, endTime, time);
00071         }
00072         if (startTime < endTime)
00073         {
00074             return false;
00075         }
00076         return true;
00077     }
00078 
00079     if (startDay < endDay)
00080     {
00081         if (currentDay < startDay || endDay < currentDay ||
00082             (currentDay == startDay && timeOnly < startTime) ||
00083             (currentDay == endDay && endTime < timeOnly))
00084         {
00085             return false;
00086         }
00087         return true;
00088     }
00089     
00090     if ((endDay < currentDay && currentDay < startDay) ||
00091         (currentDay == startDay && timeOnly < startTime) ||
00092         (currentDay == endDay && endTime < timeOnly))
00093     {
00094         return false;
00095     }
00096     return true;
00097 
00098     QF_STACK_POP
00099   }
00100 
00101   bool SessionTime::isSameSession( const UtcTimeOnly& start,
00102                                    const UtcTimeOnly& end,
00103                                    const UtcTimeStamp& time1,
00104                                    const UtcTimeStamp& time2 )
00105   { QF_STACK_PUSH(SessionTime::isSameSession)
00106 
00107     if( !isSessionTime( start, end, time1 ) ) return false;
00108     if( !isSessionTime( start, end, time2 ) ) return false;
00109 
00110     if( time1 == time2 ) return true;
00111 
00112     if( start < end || start == end )
00113     {
00114       UtcDate time1Date( time1 );
00115       UtcDate time2Date( time2 );
00116  
00117       return time1Date == time2Date;
00118     }
00119     else
00120     {
00121       int sessionLength = DateTime::SECONDS_PER_DAY - (start - end);
00122 
00123       if( time1 > time2 )
00124       {
00125         UtcTimeOnly time2TimeOnly = UtcTimeOnly(time2);
00126                   
00127         long delta = time2TimeOnly - start;
00128         if( delta < 0 )
00129           delta = DateTime::SECONDS_PER_DAY - labs(delta);
00130 
00131         return (time1 - time2) < (sessionLength - delta);
00132       }
00133       else
00134       {
00135         return (time2 - time1) < sessionLength;
00136       }
00137     }
00138         
00139     QF_STACK_POP
00140   }
00141 
00142   bool SessionTime::isSameSession( const UtcTimeOnly& startTime,
00143                                    const UtcTimeOnly& endTime,
00144                                    int startDay,
00145                                    int endDay,
00146                                    const UtcTimeStamp& time1,
00147                                    const UtcTimeStamp& time2 )
00148   { QF_STACK_PUSH(SessionTime::isSameSession)
00149 
00150     if( !isSessionTime( startTime, endTime, startDay, endDay, time1 ) )
00151       return false;
00152 
00153     if( !isSessionTime( startTime, endTime, startDay, endDay, time2 ) )
00154       return false;
00155 
00156     int absoluteDay1 = time1.getJulianDate() - time1.getWeekDay();
00157     int absoluteDay2 = time2.getJulianDate() - time2.getWeekDay();
00158     return absoluteDay1 == absoluteDay2;
00159 
00160     QF_STACK_POP
00161   }
00162 }
00163 
00164                  

Generated on Mon Mar 1 13:41:38 2010 for QuickFIX by doxygen 1.5.8 written by Dimitri van Heesch, © 1997-2001