Lean
$LEAN_TAG$
ManualTimeProvider.cs
1
/*
2
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4
*
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8
*
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
14
*
15
*/
16
17
using
System;
18
using
NodaTime;
19
using
QuantConnect
.
Util
;
20
21
namespace
QuantConnect.Lean.Engine.DataFeeds
22
{
23
/// <summary>
24
/// Provides an implementation of <see cref="ITimeProvider"/> that can be
25
/// manually advanced through time
26
/// </summary>
27
public
class
ManualTimeProvider
:
ITimeProvider
28
{
29
private
volatile
ReferenceWrapper<DateTime>
_currentTime;
30
private
readonly DateTimeZone _setCurrentTimeTimeZone;
31
32
/// <summary>
33
/// Initializes a new instance of the <see cref="ManualTimeProvider"/>
34
/// </summary>
35
/// <param name="setCurrentTimeTimeZone">Specify to use this time zone when calling <see cref="SetCurrentTime"/>,
36
/// leave null for the default of <see cref="TimeZones.Utc"/></param>
37
public
ManualTimeProvider
(DateTimeZone setCurrentTimeTimeZone =
null
)
38
{
39
_setCurrentTimeTimeZone = setCurrentTimeTimeZone ??
TimeZones
.
Utc
;
40
}
41
42
/// <summary>
43
/// Initializes a new instance of the <see cref="ManualTimeProvider"/> class
44
/// </summary>
45
/// <param name="currentTime">The current time in the specified time zone, if the time zone is
46
/// null then the time is interpreted as being in <see cref="TimeZones.Utc"/></param>
47
/// <param name="setCurrentTimeTimeZone">Specify to use this time zone when calling <see cref="SetCurrentTime"/>,
48
/// leave null for the default of <see cref="TimeZones.Utc"/></param>
49
public
ManualTimeProvider
(DateTime currentTime, DateTimeZone setCurrentTimeTimeZone =
null
)
50
: this(setCurrentTimeTimeZone)
51
{
52
_currentTime =
new
ReferenceWrapper<DateTime>
(currentTime.ConvertToUtc(_setCurrentTimeTimeZone));
53
}
54
55
/// <summary>
56
/// Gets the current time in UTC
57
/// </summary>
58
/// <returns>The current time in UTC</returns>
59
public
DateTime
GetUtcNow
()
60
{
61
return
_currentTime.
Value
;
62
}
63
64
/// <summary>
65
/// Sets the current time interpreting the specified time as a UTC time
66
/// </summary>
67
/// <param name="time">The current time in UTC</param>
68
public
void
SetCurrentTimeUtc
(DateTime time)
69
{
70
_currentTime =
new
ReferenceWrapper<DateTime>
(time);
71
}
72
73
/// <summary>
74
/// Sets the current time interpeting the specified time as a local time
75
/// using the time zone used at instatiation.
76
/// </summary>
77
/// <param name="time">The local time to set the current time time, will be
78
/// converted into UTC</param>
79
public
void
SetCurrentTime
(DateTime time)
80
{
81
SetCurrentTimeUtc
(time.ConvertToUtc(_setCurrentTimeTimeZone));
82
}
83
84
/// <summary>
85
/// Advances the current time by the specified span
86
/// </summary>
87
/// <param name="span">The amount of time to advance the current time by</param>
88
public
void
Advance
(TimeSpan span)
89
{
90
_currentTime =
new
ReferenceWrapper<DateTime>
(_currentTime.
Value
+ span);
91
}
92
93
/// <summary>
94
/// Advances the current time by the specified number of seconds
95
/// </summary>
96
/// <param name="seconds">The number of seconds to advance the current time by</param>
97
public
void
AdvanceSeconds
(
double
seconds)
98
{
99
Advance
(TimeSpan.FromSeconds(seconds));
100
}
101
}
102
}
Engine
DataFeeds
ManualTimeProvider.cs
Generated by
1.8.17