TimeRange.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) 2001-2014
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 
00026 #include "TimeRange.h"
00027 #include "Utility.h"
00028 
00029 namespace FIX
00030 {
00031   TimeRange::TimeRange( const UtcTimeOnly& startTime,
00032                         const UtcTimeOnly& endTime,
00033                         int startDay,
00034                         int endDay )
00035   : m_startTime( startTime ), m_endTime( endTime ),
00036     m_startDay( startDay ), m_endDay( endDay ),
00037     m_useLocalTime( false )
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     m_useLocalTime( true )
00053   {
00054     if( startDay > 0
00055         && endDay > 0
00056         && startDay == endDay
00057         && endTime > startTime )
00058     { m_endTime = m_startTime; }
00059   }
00060 
00061   bool TimeRange::isInRange( const DateTime& start,
00062                              const DateTime& end,
00063                              const DateTime& time )
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 
00073   bool TimeRange::isInRange( const DateTime& startTime,
00074                              const DateTime& endTime,
00075                              int startDay,
00076                              int endDay,
00077                              const DateTime& time,
00078                              int day )
00079   {
00080     UtcTimeOnly timeOnly (time);
00081 
00082     if( startDay == endDay )
00083     {
00084       if( day != startDay )
00085         return true;
00086       return isInRange( startTime, endTime, time );
00087     }
00088     else if( startDay < endDay )
00089     {
00090       if( day < startDay || day > endDay )
00091         return false;
00092       else if( day == startDay && timeOnly < startTime )
00093         return false;
00094       else if( day == endDay && timeOnly > endTime )
00095         return false;
00096     }
00097     else if( startDay > endDay )
00098     {
00099       if( day < startDay && day > endDay )
00100         return false;
00101       else if( day == startDay && timeOnly < startTime )
00102         return false;
00103       else if( day == endDay && timeOnly > endTime )
00104         return false;
00105     }
00106     return true;
00107   }
00108 
00109   bool TimeRange::isInRange( const DateTime& startTime,
00110                              const DateTime& endTime,
00111                              int startDay,
00112                              int endDay,
00113                const DateTime& time ) 
00114   {
00115     return isInRange( startTime, endTime, startDay, endDay, time, time.getWeekDay() );
00116   }
00117 
00118   bool TimeRange::isInSameRange( const DateTime& start,
00119                                  const DateTime& end,
00120                                  const DateTime& time1,
00121                                  const DateTime& time2 )
00122   {
00123     if( !isInRange( start, end, time1 ) ) return false;
00124     if( !isInRange( start, end, time2 ) ) return false;
00125 
00126     if( time1 == time2 ) return true;
00127 
00128     if( start < end || start == end )
00129     {
00130       UtcDate time1Date( time1 );
00131       UtcDate time2Date( time2 );
00132  
00133       return time1Date == time2Date;
00134     }
00135     else
00136     {
00137       int sessionLength = DateTime::SECONDS_PER_DAY - (start - end);
00138 
00139       if( time1 > time2 )
00140       {
00141         UtcTimeOnly time2TimeOnly = UtcTimeOnly(time2);
00142 
00143         long delta = time2TimeOnly - start;
00144         if( delta < 0 )
00145           delta = DateTime::SECONDS_PER_DAY - labs(delta);
00146 
00147         return (time1 - time2) < (sessionLength - delta);
00148       }
00149       else
00150       {
00151         return (time2 - time1) < sessionLength;
00152       }
00153     }
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   {
00163     if( !isInRange( startTime, endTime, startDay, endDay, time1, time1.getWeekDay() ) )
00164       return false;
00165 
00166     if( !isInRange( startTime, endTime, startDay, endDay, time2, time2.getWeekDay() ) )
00167       return false;
00168 
00169     int absoluteDay1 = time1.getJulianDate() - time1.getWeekDay();
00170     int absoluteDay2 = time2.getJulianDate() - time2.getWeekDay();
00171     return absoluteDay1 == absoluteDay2;
00172   }
00173 }

Generated on Mon Jun 23 2014 23:49:38 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001