/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using NodaTime;
using QuantConnect.Util;
namespace QuantConnect.Lean.Engine.DataFeeds
{
///
/// Provides an implementation of that can be
/// manually advanced through time
///
public class ManualTimeProvider : ITimeProvider
{
private volatile ReferenceWrapper _currentTime;
private readonly DateTimeZone _setCurrentTimeTimeZone;
///
/// Initializes a new instance of the
///
/// Specify to use this time zone when calling ,
/// leave null for the default of
public ManualTimeProvider(DateTimeZone setCurrentTimeTimeZone = null)
{
_setCurrentTimeTimeZone = setCurrentTimeTimeZone ?? TimeZones.Utc;
}
///
/// Initializes a new instance of the class
///
/// The current time in the specified time zone, if the time zone is
/// null then the time is interpreted as being in
/// Specify to use this time zone when calling ,
/// leave null for the default of
public ManualTimeProvider(DateTime currentTime, DateTimeZone setCurrentTimeTimeZone = null)
: this(setCurrentTimeTimeZone)
{
_currentTime = new ReferenceWrapper(currentTime.ConvertToUtc(_setCurrentTimeTimeZone));
}
///
/// Gets the current time in UTC
///
/// The current time in UTC
public DateTime GetUtcNow()
{
return _currentTime.Value;
}
///
/// Sets the current time interpreting the specified time as a UTC time
///
/// The current time in UTC
public void SetCurrentTimeUtc(DateTime time)
{
_currentTime = new ReferenceWrapper(time);
}
///
/// Sets the current time interpeting the specified time as a local time
/// using the time zone used at instatiation.
///
/// The local time to set the current time time, will be
/// converted into UTC
public void SetCurrentTime(DateTime time)
{
SetCurrentTimeUtc(time.ConvertToUtc(_setCurrentTimeTimeZone));
}
///
/// Advances the current time by the specified span
///
/// The amount of time to advance the current time by
public void Advance(TimeSpan span)
{
_currentTime = new ReferenceWrapper(_currentTime.Value + span);
}
///
/// Advances the current time by the specified number of seconds
///
/// The number of seconds to advance the current time by
public void AdvanceSeconds(double seconds)
{
Advance(TimeSpan.FromSeconds(seconds));
}
}
}