TimeRange.cpp
Go to the documentation of this file.
/****************************************************************************
** Copyright (c) 2001-2014
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/
#ifdef _MSC_VER
#include "stdafx.h"
#else
#include "config.h"
#endif
#include "TimeRange.h"
#include "Utility.h"
namespace FIX
{
TimeRange::TimeRange( const UtcTimeOnly& startTime,
const UtcTimeOnly& endTime,
int startDay,
int endDay )
: m_startTime( startTime ), m_endTime( endTime ),
m_startDay( startDay ), m_endDay( endDay ),
m_useLocalTime( false )
{
if( startDay > 0
&& endDay > 0
&& startDay == endDay
&& endTime > startTime )
{ m_endTime = m_startTime; }
}
TimeRange::TimeRange( const LocalTimeOnly& startTime,
const LocalTimeOnly& endTime,
int startDay,
int endDay )
: m_startTime( startTime ), m_endTime( endTime ),
m_startDay( startDay ), m_endDay( endDay ),
m_useLocalTime( true )
{
if( startDay > 0
&& endDay > 0
&& startDay == endDay
&& endTime > startTime )
{ m_endTime = m_startTime; }
}
bool TimeRange::isInRange( const DateTime& start,
const DateTime& end,
const DateTime& time )
{
UtcTimeOnly timeOnly (time);
if( start < end )
return( timeOnly >= start && timeOnly <= end );
else
return( timeOnly >= start || timeOnly <= end );
}
bool TimeRange::isInRange( const DateTime& startTime,
const DateTime& endTime,
int startDay,
int endDay,
const DateTime& time,
int day )
{
UtcTimeOnly timeOnly (time);
if( startDay == endDay )
{
if( day != startDay )
return true;
return isInRange( startTime, endTime, time );
}
else if( startDay < endDay )
{
if( day < startDay || day > endDay )
return false;
else if( day == startDay && timeOnly < startTime )
return false;
else if( day == endDay && timeOnly > endTime )
return false;
}
else if( startDay > endDay )
{
if( day < startDay && day > endDay )
return false;
else if( day == startDay && timeOnly < startTime )
return false;
else if( day == endDay && timeOnly > endTime )
return false;
}
return true;
}
bool TimeRange::isInRange( const DateTime& startTime,
const DateTime& endTime,
int startDay,
int endDay,
const DateTime& time )
{
return isInRange( startTime, endTime, startDay, endDay, time, time.getWeekDay() );
}
bool TimeRange::isInSameRange( const DateTime& start,
const DateTime& end,
const DateTime& time1,
const DateTime& time2 )
{
if( !isInRange( start, end, time1 ) ) return false;
if( !isInRange( start, end, time2 ) ) return false;
if( time1 == time2 ) return true;
if( start < end || start == end )
{
UtcDate time1Date( time1 );
UtcDate time2Date( time2 );
return time1Date == time2Date;
}
else
{
int sessionLength = DateTime::SECONDS_PER_DAY - (start - end);
if( time1 > time2 )
{
UtcTimeOnly time2TimeOnly = UtcTimeOnly(time2);
long delta = time2TimeOnly - start;
if( delta < 0 )
delta = DateTime::SECONDS_PER_DAY - labs(delta);
return (time1 - time2) < (sessionLength - delta);
}
else
{
return (time2 - time1) < sessionLength;
}
}
}
bool TimeRange::isInSameRange( const DateTime& startTime,
const DateTime& endTime,
int startDay,
int endDay,
const DateTime& time1,
const DateTime& time2 )
{
if( !isInRange( startTime, endTime, startDay, endDay, time1, time1.getWeekDay() ) )
return false;
if( !isInRange( startTime, endTime, startDay, endDay, time2, time2.getWeekDay() ) )
return false;
int absoluteDay1 = time1.getJulianDate() - time1.getWeekDay();
int absoluteDay2 = time2.getJulianDate() - time2.getWeekDay();
return absoluteDay1 == absoluteDay2;
}
}
FIX::TimeRange::m_endDay
int m_endDay
Definition: TimeRange.h:253
FIX::UtcTimeOnly
Time only represented in UTC.
Definition: FieldTypes.h:469
Utility.h
FIX::TYPE::UtcTimeOnly
Definition: FieldTypes.h:684
FIX::TimeRange::m_endTime
UtcTimeOnly m_endTime
Definition: TimeRange.h:251
FIX::TimeRange::m_useLocalTime
bool m_useLocalTime
Definition: TimeRange.h:254
FIX::TimeRange::isInRange
static bool isInRange(const UtcTimeOnly &start, const UtcTimeOnly &end, const DateTime &time)
Definition: TimeRange.h:43
FIX::TimeRange::m_startTime
UtcTimeOnly m_startTime
Definition: TimeRange.h:250
FIX::LocalTimeOnly
Time only represented in local time.
Definition: FieldTypes.h:510
FIX
Definition: Acceptor.cpp:34
FIX::UtcDate
Date only represented in UTC.
Definition: FieldTypes.h:551
FIX::DateTime::getJulianDate
int getJulianDate() const
Return the internal julian date.
Definition: FieldTypes.h:117
FIX::DateTime::SECONDS_PER_DAY
Definition: FieldTypes.h:58
FIX::TimeRange::m_startDay
int m_startDay
Definition: TimeRange.h:252
FIX::DateTime::getWeekDay
int getWeekDay() const
Calculate the weekday of the date (Sunday is 1, Saturday is 7)
Definition: FieldTypes.h:162
FIX::DateTime
Date and Time stored as a Julian day number and number of milliseconds since midnight.
Definition: FieldTypes.h:50
FIX::TimeRange::isInSameRange
static bool isInSameRange(const UtcTimeOnly &start, const UtcTimeOnly &end, const DateTime &time1, const DateTime &time2)
Definition: TimeRange.h:76
FIX::TimeRange::TimeRange
TimeRange(const UtcTimeOnly &startTime, const UtcTimeOnly &endTime, int startDay=-1, int endDay=-1)
Definition: TimeRange.cpp:31
TimeRange.h